The Dreaded "Warning: Imagecreatefromjpeg():'/Tmp/Filename' Is Not a Valid Jpeg File in /Phpfile.PHP on Line Xxx"

the dreaded Warning: imagecreatefromjpeg() : '/tmp/filename' is not a valid JPEG file in /phpfile.php on line xxx

After a little digging around on Google I found this bug report. It seems that the GD library is less tolerant of buggy JPEG files than other programs. The solution suggested was to set GD to ignore JPEG error's before processing the image, like this:

ini_set("gd.jpeg_ignore_warning", 1);

Hopefully that will work for you. One other potential problem you may run into is to do with memory. It seems that GD holds all images in memory as bitmaps once they've been opened. This means that a 5MB image can actually consume more memory than a single PHP thread is allowed, resulting in a fatal error. I had this problem with some image uploads and had to reduce the maximum file size I allowed to get around the problem.

Good luck and hope that helps.

In PHP, why has imagecreatefromjpeg not a jpeg file changed to a fatal error?

I guess I can answer my own question. It's a known bug. See. PHP bugs #73514 and #73479. And actually the bug is in libgd, and has been reported there too. A fix was made last November, but still hasn't made it to a release. So, yeah... no luck. I'll try to use getimagesize() instead to determine the type first and use the appropriate function instead. That still won't help in the case of a genuinely corrupt file, but it's something.

imagecreatefromjpeg() returns a black image when resized

According to a blog post from (Feb 2010) its a bug in the implementation of imagecreatefromjpeg which should return false but throws an error instead.

The solution is to check for the filetype of your image (I removed the duplicate call to imagecreatefromjpeg because its totally superfluous; we already check for right file type before and if an error occurs due to some 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 like this:

$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 (like your code suggests). Actually, usually you will check which filetype your file really has and then call the correct function between the three (jpeg, png, gif).

Imagecreatefromjpeg returns a black image after resize

According to Marc B's answer you could probably make a check if the file is a JPG file. (JPEG, JPG, jpg, jpeg extensions).

it could be something like:

$file = explode(".", $_POST['file']);
$file_ext = $file[count($file)]; // Get the last thing in the array - in this way the filename can containg dots (.)
$allowed_ext = array('jpg', 'JPG', 'jpeg', 'jpg');

if( in_array($file_ext, $allowed_ext )
{
// The code for creating the image here.
}


Related Topics



Leave a reply



Submit