title: Issue with cropping picture from mysql database using PHP? Image shows up black?
body: <p>Im having a weird problem retrieving/cropping a smaller version of an original image from mysql database. Kind of new to this and need some help. </p>
  
  <p>I have a script that uploads an image to a database (the original image) as well as a smaller, cropped image to be used as a profile image and that kind of thing. Here is that code:</p>
  
  <pre><code>if(isset($_FILES["file"]["type"]))
  {
  $validextensions = array("jpeg", "jpg", "png");
  $maxsize = 99999999;
  $temporary = explode(".", $_FILES["file"]["name"]);
  $file_extension = end($temporary);
  if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg")
  ) &amp;&amp; ($_FILES["file"]["size"] &lt; $maxsize)//Approx. 100kb files can be uploaded.
  &amp;&amp; in_array($file_extension, $validextensions)) {
  
  $size = getimagesize($_FILES['file']['tmp_name']);
  $type = $size['mime'];
  $imgfp = fopen($_FILES['file']['tmp_name'], 'rb');
  $size = $size[3];
  $name = $_FILES['file']['name'];
  
  $sql = new mysqli("localhost","username","password","sqlserver");
  $imgfp64 = base64_encode(stream_get_contents($imgfp));
  $update = "UPDATE sqlserver.imageblob set image='".$imgfp64."', image_type='".$type."', image_name='".$name."', image_size='".$size."' where user_id=".$account['id'];
  $sql-&gt;query($update);
  </code></pre>
  
  <p>This is the part where scaling takes place:</p>
  
  <pre><code>//small image
      $w=50;$h=50;
      list($width, $height) = getimagesize($_FILES['file']['tmp_name']);
      $r = $width / $height;
  
          if ($w/$h &gt; $r) { 
              $newwidth = $h*$r;
              $newheight = $h;
          } else {
              $newheight = $w/$r;
              $newwidth = $w;
          }
  
      $src = imagecreatefromjpeg($_FILES['file']['tmp_name']);
      $dst = imagecreatetruecolor($newwidth, $newheight);
      imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
      ob_start();
      imagejpeg($dst,null);
      $dst=ob_get_clean();
  
      $dst = base64_encode($dst);
              $update2 = "UPDATE sqlserver.imageblob set chatimage='".$dst."' where user_id=".$account['id'];
  
              $sql-&gt;query($update2);
  </code></pre>
  
  <p>And here is the result with only SOME images - </p>
  
  <p><a href="https://i.stack.imgur.com/PRzp6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PRzp6.png" alt="enter image description here"></a></p>
  
  <p>The original is not black. I read this post <a href="https://stackoverflow.com/questions/4442418/php-black-image-when-cropping-using-php">PHP: black image when cropping using php</a> and think the script might be cropping outside the picture. What can I change in my code to mitigate this? </p>
  
  <p>How can I make it scale down and crop the image from the image not outside?</p>