PHP imagecreatefromstring (): gd-jpeg, libjpeg: error being recovered

I am trying to download an image from an external site that I have no control over.

In most cases, it works great (hundreds of images verified so far).

Now he gives me this error for one specific image:

imagecreatefromstring (): gd-jpeg, libjpeg: error being recovered: corrupted JPEG data: premature end of data segment

from this line:

$im = @imagecreatefromstring( $imageString ); 

The tip I've read so far suggests adding:

 ini_set("gd.jpeg_ignore_warning", true); 

but it didn’t affect, and I still get the error. I do ini_set before calling. It is important?

I really got stuck on how to ignore this error and continue.

+6
source share
4 answers

The problem was related to my error handling. I installed an error handler, so my call

 $im = @imagecreatefromstring( $imageString ); 

did not suppress errors.

By changing 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; } 

Now I can correctly suppress selected errors.

I found the information here: http://anvilstudios.co.za/blog/php/how-to-ignore-errors-in-a-custom-php-error-handler/

+6
source

this can be solved with:

 ini_set ('gd.jpeg_ignore_warning', 1); 
+2
source

If you just show the image, I suggest just reading the contents and displaying the image as follows:

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

If you want to copy the image to your server, you have several options, two of which are the copy() function or the method used above, and then fwrite() :

Option 1 - copy() function available with 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

0
source

Given that you want to make a thumbnail and save it, you can implement a convenient resize function, for example:

 <?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 copied the file to your server using one of my methods in my answer above this, you can simply apply the function as follows:

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

This feature serves me pretty well. I apply it to 1000 images a day and it works like a charm.

0
source

Source: https://habr.com/ru/post/885484/


All Articles