Premature end of JPEG file

hashmi picture hashmi · Sep 15, 2012 · Viewed 18k times · Source

I am getting this Premature end of JPEG file error while resizing some of the images. Interesting and strange part is that i am getting this error only when i upload any camera taken images, like from mobile, other than those every thing works great. I thought this could be because of the chunk size used in plupload. So, i uploaded with larger sized image to somewhat like 3mb to test. Works fine with images other than camera taken images. So, whenever i upload camera pics i get this error.

Further elaborations on the error: php function imagecreatefromjpeg is throwing an error "imgname.jpg is not a valid JPEG file".

To resize images i am using Codeigniter's image Manipulation Class.

Answer

rationalboss picture rationalboss · Sep 15, 2012

Based on what you have provided, I can only give you my deductions.

Camera images are usually very big. I suggest that you try to resize the camera images and see if it works.

What is your PHP version? There is a bug related to this: https://bugs.php.net/bug.php?id=29878

Please also check if your JPEG files are in RGB format. Somewhere in the manual mentioned that it could not properly load CMYK for certain versions of the GD library.

Are you open to using another class? I use this class to resize images and have not encountered any problems with it for years.

Resizing images is as easy as:

<?php
   include('SimpleImage.php');
   $image = new SimpleImage();
   $image->load('picture.jpg');
   $image->resizeToHeight(500);
   $image->save('picture2.jpg');
   $image->resizeToHeight(200);
   $image->save('picture3.jpg');
?>

If all the suggestions did not work out, you can try using ImageMagick.