Imagecreatefromjpeg () returns a black image when resized

I have the following code

// load image and get image size $img = imagecreatefrompng( "{$pathToImages}{$fname}" ); $width = imagesx( $img ); $height = imagesy( $img ); // calculate thumbnail size $new_width = $imageWidth; $new_height = 500; // create a new temporary image $tmp_img = imagecreatetruecolor( $new_width, $new_height ); // copy and resize old image into new image imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); 

It works fine with some images .. but with certain images it shows an error like

 Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: Warning: imagesx() expects parameter 1 to be resource, boolean given Warning: imagesy() expects parameter 1 to be resource, boolean given 

I also included

gd.jpeg_ignore_warning = 1

in php.ini

Any help was appreciated.

+4
source share
5 answers

According to a blog post from the (February 2010) of his error in the implementation of imagecreatefromjpeg , which should return false , but throws instead.

The solution is to check the file type of your image (I deleted the duplicate call to imagecreatefromjpeg because it is completely redundant: we already check the correct file type before and if the error occurs for any other reason, imagecreatefromjpeg will return false correctly):

 function imagecreatefromjpeg_if_correct($file_tempname) { $file_dimensions = getimagesize($file_tempname); $file_type = strtolower($file_dimensions['mime']); if ($file_type == 'image/jpeg' || $file_type == 'image/pjpeg'){ $im = imagecreatefromjpeg($file_tempname); return $im; } return false; } 

Then you can write your code as follows:

 $img = imagecreatefrompng_if_correct("{$pathToImages}{$fname}"); if ($img == false) { // report some error } else { // enter all your other functions here, because everything is ok } 

Of course, you can do the same for png if you want to open a png file (just like your code). In fact, usually you will check which file has its own file, and then call the correct function between the three (jpeg, png, gif).

+4
source

See PHP Error # 39918 imagecreatefromjpeg not working . The suggestion is to change the GD setting for loading the jpeg image:

 ini_set("gd.jpeg_ignore_warning", 1); 

Then you also need to check the return value from the imagecreatefromjpeg Docs call:

 $img = imagecreatefromjpeg($file); if (!$img) { printf("Failed to load jpeg image \"%s\".\n", $file); die(); } 

Also see a potentially recurring question:

It has almost the same error description as yours.

+1
source

Could you give any examples of files that work / do not work? According to http://www.php.net/manual/en/function.imagecreatefromjpeg.php#100338 spaces in remote filenamnes can cause problems.

0
source

My solution to this problem is to determine if imagecreatefromjpeg returns false and use imagecreatefromstring for file_get_contents instead. Worked for me. See the code example below:

 $ind=0; do{ if($mime == 'image/jpeg'){ $img = imagecreatefromjpeg($image_url_to_upload ); }elseif ($mime == 'image/png') { $img = imagecreatefrompng($image_url_to_upload ); } if ($img===false){ echo "imagecreatefromjpeg error!\n "); } if ($img===false){ $img = imagecreatefromstring(file_get_contents($image_url_to_upload)); } if ($img===false){ echo "imagecreatefromstring error!\n "); } $ind++; }while($img===false&&$ind<5); 
0
source

If you use the URL as the image source, you need to make sure that the php allow_url_fopen parameter is enabled.

0
source

All Articles