Premature end of JPEG file

旧巷老猫 提交于 2019-12-11 03:26:20

问题


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.


回答1:


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.




回答2:


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 error
function 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;
    }
}



回答3:


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.




回答4:


You can set default value of gd.jpeg_ignore_warning=1 in php.ini

OR

You can set it like this ini_set('gd.jpeg_ignore_warning', true); in your PHP script before calling imagecreatefromjpeg()

After implementing any of the above, GD Library will ignore the error where it use to fail and imagecreatefromjpeg() will return an image resource identifier.

Note: In PHP 7.1.0 the default value of gd.jpeg_ignore_warning has been changed from 0 to 1.

Reference



来源:https://stackoverflow.com/questions/12438813/premature-end-of-jpeg-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!