我正在尝试从我无法控制的外部网站加载图片。
大部分时间它工作正常(到目前为止测试了数百张图片)。
现在它给了我一个特定图像的错误:
imagecreatefromstring():gd-jpeg,libjpeg:可恢复的错误:损坏的JPEG数据:数据段的过早结束
从这一行:
$im = @imagecreatefromstring( $imageString );
到目前为止,我读到的建议建议添加:
ini_set("gd.jpeg_ignore_warning", true);
但这没有效果,我仍然得到错误。我正在打电话之前做ini_set。这有关系吗?
我真的不知道如何忽略这个错误并继续。
答案 0 :(得分:6)
问题是由于我的错误处理。我设置了一个错误处理程序,所以我打电话给
$im = @imagecreatefromstring( $imageString );
没有压制错误。
通过以下方式修改我的错误处理程序:
if (error_reporting() === 0)
{
// This copes with @ being used to suppress errors
// continue script execution, skipping standard PHP error handler
return false;
}
我现在可以正确地抑制选定的错误。
我在这里找到了信息:http://anvilstudios.co.za/blog/php/how-to-ignore-errors-in-a-custom-php-error-handler/
答案 1 :(得分:2)
这可以通过以下方式解决:
ini_set ('gd.jpeg_ignore_warning', 1);
答案 2 :(得分:0)
如果您只是显示图像,那么我建议您只需阅读内容并按如下方式显示图像:
$img = "http://path/to/image";
$contents = file_get_contents($img);
header("Content-Type: image/jpeg");
print($contents);
如果您想将图片复制到服务器,可以选择一些选项,其中两个是copy()
功能或上面使用的方法,然后fwrite()
:
选项1 - {4}提供的copy()
功能
$file1 = "http://path/to/file";
$dest = "/path/to/yourserver/lcoation";
$docopy = copy($file1, $dest);
选项2 - 使用file_get_contents()
和fwrite()
$img = "http://path/to/image";
$contents = file_get_contents($img);
$newfile = "path/to/file.ext";
$fhandler = fopen($newfile, 'w+'); //create if not exists, truncate to 0 length
$write = fwrite($fhandler, $contents); //write image data
$close = fclose($fhandler); //close stream
chmod(0755, $newfile); //make publically readable if you want
我希望你能在上面找到一些用途
答案 3 :(得分:0)
考虑到你想制作一个缩略图并保存它,你可以实现一个方便的调整大小功能,如下所示:
<?php
function resize($sourcefile, $endfile, $thumbwidth, $thumbheight, $quality){
$ext1 = explode(".",trim($sourcefile));
$ext = strtolower(trim(array_slice($sext1,-1)));
switch($ext):
case 'jpg' or 'jpeg':
$img = imagecreatefromjpeg($sourcefile);
break;
case 'gif':
$img = imagecreatefromgif($sourcefile);
break;
case 'png':
$img = imagecreatefrompng($sourcefile);
break;
endswitch;
$width = imagesx( $img );
$height = imagesy( $img );
if ($width > $height) {
$newwidth = $thumbwidth;
$divisor = $width / $thumbwidth;
$newheight = floor( $height / $divisor);
}
else {
$newheight = $thumbheight;
$divisor = $height / $thumbheight;
$newwidth = floor( $width / $divisor );
}
// Create a new temporary image.
$tmpimg = imagecreatetruecolor( $newwidth, $newheight );
// Copy and resize old image into new image.
imagecopyresampled( $tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
// Save thumbnail into a file.
switch($ext):
case 'jpg' or 'jpeg':
$makeimg = imagejpeg($tmpimg, $endfile, $quality);
break;
case 'gif':
$makeimg = imagegif($tmpimg, $endfile, $quality);
break;
case 'png':
$makeimg = imagepng($tmpimg, $endfile, $quality);
break;
endswitch;
// release the memory
imagedestroy($tmpimg);
imagedestroy($img);
if($makeimg){
chmod($endfile,0755);
return true;
}else{
return false;
}
}
?>
然后,在我上面的答案中使用我的一种方法将文件复制到您的服务器后,您可以简单地应用如下函数:
$doresize = resize($sourcefile, $endfile, $thumbwidth, $thumbheight, $quality);
echo ($doresize == true ? "IT WORKED" : "IT FAILED");
这个功能很适合我。我每天将它应用于1000张图像,它就像一个魅力。