1

I need to create thumbnails from blob and stored in database, so I have the following function

function createSquareImage($imgResource, $square_size = 96) {

    // get width and height of original image        
    $originalWidth = imagesx($imgResource);
    $originalHeight = imagesy($imgResource);

    if ($originalWidth > $originalHeight) {
        $thumbHeight = $square_size;
        $thumbWidth = $thumbHeight * ($originalWidth / $originalHeight);
    }
    if ($originalHeight > $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 > $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 > $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;
}

And I the call:

   $imgBlob = imagecreatefromstring(base64_decode($image->getContent()));

   $thumbResource = $this->createSquareImage($imgBlob, 100);

   $thumbContent = stream_get_contents($thumbResource);
   // Save the stream ($thumbContent) in database

But I get the exception

Warning: stream_get_contents(): 38 is not a valid stream resource 

What am I doing wrong?

Update 1:

If I remove the line imagedestroy($squareImg); I get a similar exception message:

Warning: stream_get_contents(): supplied resource is not a valid stream resource 
Cristhian Boujon
  • 4,060
  • 13
  • 51
  • 90

1 Answers1

1

You should use imagejpeg() to output your image data and then capture it with ob_start() and ob_get_clean().

function createSquareImage($imgResource, $square_size = 96) {

    // other code

    ob_start();

    imagejpeg($squareImg);

    $imageData = ob_get_clean();

    imagedestroy($imgResource);
    imagedestroy($thumbImg);
    imagedestroy($squareImg);

    return $imageData;
}

This will return JPEG data of your image and you won't need to use stream_get_contents.

gre_gor
  • 6,669
  • 9
  • 47
  • 52