2

我有一个名为 $png_file 的文件,它是从数据库中获取的字符串。我希望能够通过以下方式使用它:

$image = imagecreatefromstring($png_file);
$width = imagesx($image);
$height = imagesy($image);

这样我就可以获得作为字符串保存在数据库中的 png 文件的高度和宽度。

但是,每次我尝试这样做时,都会出现以下错误:

Warning: imagecreatefromstring() [function.imagecreatefromstring]: gd-png: fatal libpng error: Read Error: truncated data in  on line 39
Warning: imagecreatefromstring() [function.imagecreatefromstring]: gd-png error: setjmp returns error condition in  on line 39
Warning: imagecreatefromstring() [function.imagecreatefromstring]: Passed data is not in 'PNG' format in  on line 39
Warning: imagecreatefromstring() [function.imagecreatefromstring]: Couldn't create GD Image Stream out of Data in  on line 39
Warning: imagesx() expects parameter 1 to be resource, boolean given in  on line 40
Warning: imagesy() expects parameter 1 to be resource, boolean given in  on line 41

我怎样才能解决这个问题?并注意 png 文件是一个字符串,我无法将其作为 url

4

2 回答 2

0

检查这个。您将对“imagecreatefromstring”函数有所了解。 http://php.net/manual/en/function.imagecreatefromstring.php

于 2012-11-21T06:03:42.547 回答
0

可能会迟到,但您可以尝试:

$image = imagecreatefromstring(file_get_contents($png_file));
$width = imagesx($image);
$height = imagesy($image);

注意:file_get_contents部分。

希望能帮助到你

于 2016-04-15T19:09:58.353 回答