简体   繁体   中英

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

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. 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/

这可以解决:

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() :

Option 1 - The copy() function, available from PHP 4

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

Option 2 - Using file_get_contents() and 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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