繁体   English   中英

PHP imagecreatefromstring():gd-jpeg,libjpeg:可恢复的错误

[英]PHP imagecreatefromstring(): gd-jpeg, libjpeg: recoverable error

我正在尝试从我无法控制的外部网站加载图像。

大部分时间它工作正常(到目前为止测试了数百个图像)。

它现在给了我一个特定图像的错误:

imagecreatefromstring():gd-jpeg,libjpeg:可恢复的错误:损坏的JPEG数据:数据段的过早结束

从这一行:

$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张图像,它就像一个魅力。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM