Premature end of JPEG file Premature end of JPEG file codeigniter codeigniter

Premature end of JPEG file


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.


Also "Premature end of JPEG file" it's a general software error if image content is not complete. Software determines it by color of last pixel.

I've get 'Premature end of JPEG file' from tesseract(Open Source OCR Engine) because file was not copied properly through network.


did you found that sometimes it hang the php when imagecreatefromjpeg() run on bad JPEG. I found that this is cause by the JPEG file U used dont have EOI (end of image) the FF D9 at the end of your JPEG

JPEG image should start with 0xFFD8 and end with 0xFFD9

// this may help to fix the errorfunction check_jpeg($f, $fix=false ){    # check for jpeg file header and footer - also try to fix it    if ( false !== (@$fd = fopen($f, 'r+b' )) ){        if ( fread($fd,2)==chr(255).chr(216) ){            fseek ( $fd, -2, SEEK_END );            if ( fread($fd,2)==chr(255).chr(217) ){                fclose($fd);                return true;            }else{            if ( $fix && fwrite($fd,chr(255).chr(217)) ){return true;}                fclose($fd);                return false;            }        }else{fclose($fd); return false;}    }else{        return false;    }}