我有以下代码
// load image and get image size
$img = imagecreatefrompng( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $imageWidth;
$new_height = 500;
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
它在某些images..but上工作得很好,它显示一个错误,比如
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error:
Warning: imagesx() expects parameter 1 to be resource, boolean given
Warning: imagesy() expects parameter 1 to be resource, boolean given
我还启用了
gd.jpeg_ignore_warning =1
在php.ini中
感谢您的帮助。
发布于 2012-07-14 19:07:43
根据a blog post from (Feb 2010)的说法,这是imagecreatefromjpeg
实现中的一个错误,它本应返回false
,但却抛出了一个错误。
解决方案是检查镜像的文件类型(我删除了对imagecreatefromjpeg
的重复调用,因为它完全是多余的;我们之前已经检查过正确的文件类型,如果由于其他原因发生错误,imagecreatefromjpeg
将正确地返回false
):
function imagecreatefromjpeg_if_correct($file_tempname) {
$file_dimensions = getimagesize($file_tempname);
$file_type = strtolower($file_dimensions['mime']);
if ($file_type == 'image/jpeg' || $file_type == 'image/pjpeg'){
$im = imagecreatefromjpeg($file_tempname);
return $im;
}
return false;
}
然后你可以像这样写你的代码:
$img = imagecreatefrompng_if_correct("{$pathToImages}{$fname}");
if ($img == false) {
// report some error
} else {
// enter all your other functions here, because everything is ok
}
当然,如果您想要打开一个png文件(如您的代码所示),您也可以对png执行相同的操作。实际上,通常你会检查你的文件到底是哪种文件类型,然后调用三者之间的正确函数(jpeg,png,gif)。
发布于 2019-02-25 00:54:29
我对这个问题的解决方案是:检测imagecreatefromjpeg是否返回'false‘,在这种情况下,在file_get_contents上使用imagecreatefromstring。对我很管用。请参见下面的代码示例:
$ind=0;
do{
if($mime == 'image/jpeg'){
$img = imagecreatefromjpeg($image_url_to_upload);
}elseif ($mime == 'image/png') {
$img = imagecreatefrompng($image_url_to_upload);
}
if ($img===false){
echo "imagecreatefromjpeg error!\n";
}
if ($img===false){
$img = imagecreatefromstring(file_get_contents($image_url_to_upload));
}
if ($img===false){
echo "imagecreatefromstring error!\n";
}
$ind++;
}while($img===false&&$ind<5);
发布于 2012-07-14 19:08:52
你能给出一些文件工作/不工作的例子吗?根据http://www.php.net/manual/en/function.imagecreatefromjpeg.php#100338的说法,远程文件名中的空白可能会导致问题。
https://stackoverflow.com/questions/11483212
复制相似问题