Php 从imagecopyresampled获取流

Php 从imagecopyresampled获取流,php,php-gd,Php,Php Gd,我需要从blob创建缩略图并存储在数据库中,所以我有以下功能 function createSquareImage($imgResource, $square_size = 96) { // get width and height of original image $originalWidth = imagesx($imgResource); $originalHeight = imagesy($imgResource); if ($ori

我需要从blob创建缩略图并存储在数据库中,所以我有以下功能

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;
}
我接到了电话:

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

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

   $thumbContent = stream_get_contents($thumbResource);
   // Save the stream ($thumbContent) in database
但我有个例外

Warning: stream_get_contents(): 38 is not a valid stream resource 
我做错了什么

更新1:

如果我删除行
imagedestroy($squareImg)我收到一条类似的异常消息:

Warning: stream_get_contents(): supplied resource is not a valid stream resource 
您应该使用来输出图像数据,然后使用和进行捕获


这将返回图像的JPEG数据,您不需要使用
stream\u get\u contents

我怀疑
图像资源标识符
是否为
流资源
。您希望以什么格式获得图像?png、jpeg、gif?您也已经销毁了该图像资源。@gre\u gor请查看我的更新1。还有,我应该在哪里给出格式?
function createSquareImage($imgResource, $square_size = 96) {

    // other code

    ob_start();

    imagejpeg($squareImg);

    $imageData = ob_get_clean();

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

    return $imageData;
}