简体   繁体   English

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

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

I'm trying to load an image from an external site over which I have no control. 我正在尝试从我无法控制的外部网站加载图像。

Most of the time it works fine (hundreds of images tested so far). 大部分时间它工作正常(到目前为止测试了数百个图像)。

It's now giving me this error for one particular image: 它现在给了我一个特定图像的错误:

imagecreatefromstring(): gd-jpeg, libjpeg: recoverable error: Corrupt JPEG data: premature end of data segment imagecreatefromstring():gd-jpeg,libjpeg:可恢复的错误:损坏的JPEG数据:数据段的过早结束

from this line: 从这一行:

$im = @imagecreatefromstring( $imageString );

The advice I'd read so far suggests adding: 到目前为止我读到的建议建议添加:

ini_set("gd.jpeg_ignore_warning", true);

but that's had no effect and I'm still getting the error. 但那没有效果,我仍然得到错误。 I'm doing the ini_set just before the call. 我正在打电话之前做ini_set。 Is that relevant? 这有关系吗?

I'm really stuck on how to ignore this error and carry on. 我真的被困在如何忽略这个错误并继续。

The problem was due to my error handling. 问题是由于我的错误处理。 I'd set up an error handler so my call to 我设置了一个错误处理程序,所以我的调用

$im = @imagecreatefromstring( $imageString );

wasn't suppressing errors. 没有压制错误。

By modifying my error handler with: 通过修改我的错误处理程序:

   if (error_reporting() === 0)
   {
        // This copes with @ being used to suppress errors
        // continue script execution, skipping standard PHP error handler
        return false;
   }

I can now correctly suppress selected errors. 我现在可以正确地抑制选定的错误。

I found the info here: http://anvilstudios.co.za/blog/php/how-to-ignore-errors-in-a-custom-php-error-handler/ 我在这里找到了这些信息: http//anvilstudios.co.za/blog/php/how-to-ignore-errors-in-a-custom-php-error-handler/

这可以解决:

ini_set ('gd.jpeg_ignore_warning', 1);

If you are just showing the image, then I suggest simply reading the contents and display the image as follows: 如果您只是显示图像,那么我建议只需阅读内容并显示如下图像:

$img = "http://path/to/image";
$contents = file_get_contents($img);
header("Content-Type: image/jpeg");
print($contents);

If you are wanting to copy the image to your server, you have a few options, two of which are the copy() function or the method used above and then fwrite() : 如果您想将图像复制到服务器,您有几个选项,其中两个是copy()函数或上面使用的方法,然后是fwrite()

Option 1 - The copy() function, available from PHP 4 选项1 - copy()函数,可从PHP 4获得

$file1 = "http://path/to/file";
$dest = "/path/to/yourserver/lcoation";
$docopy = copy($file1, $dest);

Option 2 - Using file_get_contents() and fwrite() 选项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

I hope you find some use in the above 我希望你能在上面找到一些用处

Considering that you want to make a thumbnail and save it, you could implement a handy resize function like the following: 考虑到您想制作缩略图并保存它,您可以实现一个方便的调整大小功能,如下所示:

<?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;
        }
    }
?>

Then, after you've copied the file to your server using one of my methods in my answer above this, you could simply apply the function as follows: 然后,在我上面的答案中使用我的一种方法将文件复制到服务器之后,您可以简单地应用如下函数:

$doresize = resize($sourcefile, $endfile, $thumbwidth, $thumbheight, $quality);
echo ($doresize == true ? "IT WORKED" : "IT FAILED");

This function serves me pretty well. 这个功能很适合我。 I apply it to 1000's of images a day and it works like a charm. 我每天将它应用于1000张图像,它就像一个魅力。

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

相关问题 imagecreatefromstring(): gd-jpeg, libjpeg: 可恢复错误: JPEG 文件过早结束 - imagecreatefromstring(): gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file Codeigniter 中的“gd-jpeg、libjpeg:可恢复错误:JPEG 文件过早结束” - “gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file” in Codeigniter imagecreatefromjpeg():gd-jpeg:JPEG库报告不可恢复的错误 - Laravel - imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error - Laravel 收到错误警告:imagejpeg()[function.imagejpeg]:gd-jpeg:JPEG库报告了不可恢复的错误 - Getting Error Warning: imagejpeg() [function.imagejpeg]: gd-jpeg: JPEG library reports unrecoverable error 警告:imagejpeg()[function:imagejpeg]:gd-jpeg:JPEG库报告不可恢复的错误 - Warning: imagejpeg() [function:imagejpeg]: gd-jpeg: JPEG library reports unrecoverable error PHP GD imagecreatefromstring 丢弃透明度 - PHP GD imagecreatefromstring discards transparency 使用GD和libjpeg支持编译PHP - Compiling PHP with GD and libjpeg support 使用 PHP GD `imagecreatefromstring` 函数创建图像的问题 - Issue with creating an image with PHP GD `imagecreatefromstring` function php gd imagecreatefromstring() 和图像 mime 类型 - php gd imagecreatefromstring() and image mime type GD-使用imagecreatefromstring()的无效PNG文件错误 - GD - Invalid PNG file error using imagecreatefromstring()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM