title: Get stream from imagecopyresampled
body: <p>I need to create thumbnails from blob and stored in database, so I have the following function</p>
  
  <pre><code>function createSquareImage($imgResource, $square_size = 96) {
  
      // get width and height of original image        
      $originalWidth = imagesx($imgResource);
      $originalHeight = imagesy($imgResource);
  
      if ($originalWidth &gt; $originalHeight) {
          $thumbHeight = $square_size;
          $thumbWidth = $thumbHeight * ($originalWidth / $originalHeight);
      }
      if ($originalHeight &gt; $originalWidth) {
          $thumbWidth = $square_size;
          $thumbHeight = $thumbWidth * ($originalHeight / $originalWidth);
      }
      if ($originalHeight == $originalWidth) {
          $thumbWidth = $square_size;
          $thumbHeight = $square_size;
      }
  
      $thumbWidth = round($thumbWidth);
      $thumbHeight = round($thumbHeight);
  
      $thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
      $squareImg = imagecreatetruecolor($square_size, $square_size);
  
      imagecopyresampled($thumbImg, $imgResource, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $originalWidth, $originalHeight);
  
      if ($thumbWidth &gt; $thumbHeight) {
          $difference = $thumbWidth - $thumbHeight;
          $halfDifference = round($difference / 2);
          imagecopyresampled($squareImg, $thumbImg, 0 - $halfDifference + 1, 0, 0, 0, $square_size + $difference, $square_size, $thumbWidth, $thumbHeight);
      }
      if ($thumbHeight &gt; $thumbWidth) {
          $difference = $thumbHeight - $thumbWidth;
          $half_difference = round($difference / 2);
          imagecopyresampled($squareImg, $thumbImg, 0, 0 - $half_difference + 1, 0, 0, $square_size, $square_size + $difference, $thumbWidth, $thumbHeight);
      }
      if ($thumbHeight == $thumbWidth) {
          imagecopyresampled($squareImg, $thumbImg, 0, 0, 0, 0, $square_size, $square_size, $thumbWidth, $thumbHeight);
      }
  
  
      imagedestroy($imgResource);
      imagedestroy($thumbImg);
      imagedestroy($squareImg);
  
      return $squareImg;
  }
  </code></pre>
  
  <p>And I the call:</p>
  
  <pre><code>   $imgBlob = imagecreatefromstring(base64_decode($image-&gt;getContent()));
  
     $thumbResource = $this-&gt;createSquareImage($imgBlob, 100);
  
     $thumbContent = stream_get_contents($thumbResource);
     // Save the stream ($thumbContent) in database
  </code></pre>
  
  <p>But I get the exception </p>
  
  <pre><code>Warning: stream_get_contents(): 38 is not a valid stream resource 
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p><strong>Update 1:</strong></p>
  
  <p>If I remove the line <code>imagedestroy($squareImg);</code> I get a similar exception message:</p>
  
  <pre><code>Warning: stream_get_contents(): supplied resource is not a valid stream resource 
  </code></pre>