I am using the imagecreatefromjpeg() function to upload images via an upload form:
$folder = '../images/';
$image = imagecreatefromjpeg($_FILES['image']['tmp_name']);
$new = imagecreatetruecolor(300, 300);
imagecopyresampled($new, $image, 0, 0, 0, 0, 300, 300, 150, 150);
imagejpeg($new, $folder, 100);
Most photos upload fine but some display this error:
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg, libjpeg: recoverable error: Corrupt JPEG data: 756 extraneous bytes before marker 0xed
It seems to occur with some, but not all, '.jpeg' files. I have not seen the problem occur with '.jpg' files, but I cannot be certain that the problem is exclusive to '.jpeg' files. I did notice that if I change the extension on the problem file from '.jpeg' to '.jpg', it works just fine.
Why do I get "Corrupt JPEG data" message when using imagecreatefromjpeg() function in php?
I did notice that if I change the extension on the problem file from '.jpeg' to '.jpg', it works just fine.
Are you just renamed it, or resaved in graphical program?
Anyway it seems that your file is broken. Look at Bug #39918 imagecreatefromjpeg doesn't work.
GD does provide a mechanism to be more tolerant with broken jpeg images.
Using error_reporting(E_ALL); while developing would have told you what's going on. GD did report some errors in the jpeg codec. Some of the jpeg errors are recoverable, like those in this image.
You can change the behaviors of the jpeg codec using gd.jpeg_ignore_warning:
ini_set("gd.jpeg_ignore_warning", 1);
$im = imagecreatefromjpeg("test.jpeg");
$im contains now your image.
this can be solved with: ini_set ('gd.jpeg_ignore_warning', 1);
Try adding @imagecreatefromjpeg($img)
instead of imagecreatefromjpeg($img)
.
Here @
is the error suppressor.