我正在尝试从外部网站加载我无法控制的图像。
大多数情况下它工作得很好(到目前为止测试了数百个图像)。
它现在给我这个错误对于一个特定的图像:
imagecreatefromstring(): gd-jpeg, libjpeg:可恢复错误:损坏的JPEG数据:数据段过早结束
from this行:
$im = @imagecreatefromstring( $imageString );
目前为止我读到的建议是添加:
ini_set("gd.jpeg_ignore_warning", true);
但这没有效果,我仍然得到错误。我在调用之前做了ini_set。这有关系吗?
问题是由于我的错误处理。我设置了一个错误处理程序所以我对
的调用$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/
这可以通过:
ini_set ('gd.jpeg_ignore_warning', 1);
如果您只是显示图像,那么我建议简单地阅读内容并按如下方式显示图像:
$img = "http://path/to/image";
$contents = file_get_contents($img);
header("Content-Type: image/jpeg");
print($contents);
如果您想将图像复制到您的服务器,您有几个选项,其中两个是copy()
函数或上面使用的方法,然后是fwrite()
:
选项1 - copy()
函数,可从PHP 4
$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
我希望你在上面找到一些用处
考虑到您想要制作一个缩略图并保存它,您可以实现一个方便的大小调整函数,如下所示:
<?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张图片上,效果非常好。