php imagecreatefromstring()无效

时间:2018-02-11 09:50:18

标签: php image

我有一个脚本可以将用户上传的图像保存到MySQL数据库,然后在另一个位置可以访问它们。还有一个选项可以裁剪图像,该图像可以正常工作。但是,当我尝试渲染裁剪的图像(正常图像工作正常)时,我得到以下结果:

$imgdata = $row['original']; // Line 86
$variables = json_decode($row['cropped']); // Line 87

$im = imagecreatefromstring($imgdata); // Line 89
$im2 = imagecrop($im, ['x' => $variables->x, 'y' => $variables->y, 'width' => $variables->width, 'height' => $variables->height]); // Line 90
if ($im2 !== FALSE) { // Line 91
    header('Content-Type: image/png'); // Line 92
    imagepng($im2); // Line 93
    imagedestroy($im2); // Line 94
} else { // Line 95
    echo "Could not crop image."; // Line 96
} // Line 97

这是我的代码:

object(stdClass)#3 (4) { ["x"]=> int(87) ["y"]=> int(15) ["width"]=> int(142) ["height"]=> int(82) }

修改 这是其中一张图片的$ variable变量的var_dump

{{1}}

$ imgdata包含原始图像数据。我在下面的URL上有相同图像的base64编码版本的数据。我没有把它贴在这里,因为它太长了。

https://uploadr.retweety.deals/katherine/1041f1a2511aa8179d9809dad66418ead85a0790/75cc53e4ee0f96acd2a2876c2484f91fcfc6c43d/normal

1 个答案:

答案 0 :(得分:0)

我使用$imgdata测试了您的代码。 对我来说,它看起来像存储在数据库中的图像源是腐败的。 否则,您将能够无错误地显示原始图像。我无法通过此代码显示图像。我得到了同样的错误。

$imgdata = base64_decode($imgdata);
$im      = imagecreatefromstring($imgdata);
if ($im !== false) {
  header('Content-Type: image/png');
  imagepng($im);
  imagedestroy($im);
}