“gd-jpeg, libjpeg: recoverable error: Premature en

2019-03-27 14:04发布

问题:

I am using CodeIgniter and trying to create thumbs of images. I was successful for some but failed for in some cases. I am getting this following error -

<< A PHP Error was encountered
Severity: Notice

Message: imagecreatefromjpeg(): gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file

Filename: libraries/Image_lib.php

Line Number: 1155 >>

i used this code after 'image_lib' library load.

ini_set('gd.jpeg_ignore_warning', 1);

any solution? Thanks in advance!

回答1:

The problem is that error suppression is not turned on for the function imagecreatefromjpeg

The best option is to extend the base library and overload the image_create_gd method

Create a new file ./application/libraries/MY_Image_lib.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Class MY_Image_lib extends CI_Image_Lib {

function image_create_gd($path = '', $image_type = '')
{
    if ($path == '')
        $path = $this->full_src_path;

    if ($image_type == '')
        $image_type = $this->image_type;


    switch ($image_type)
    {
        case     1 :
                    if ( ! function_exists('imagecreatefromgif'))
                    {
                        $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
                        return FALSE;
                    }

                    return @imagecreatefromgif($path);
            break;
        case 2 :
                    if ( ! function_exists('imagecreatefromjpeg'))
                    {
                        $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
                        return FALSE;
                    }

                    return @imagecreatefromjpeg($path);
            break;
        case 3 :
                    if ( ! function_exists('imagecreatefrompng'))
                    {
                        $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
                        return FALSE;
                    }

                    return @imagecreatefrompng($path);
            break;

    }

    $this->set_error(array('imglib_unsupported_imagecreate'));
    return FALSE;
}

}


回答2:

I was having the exactly the same issue. I was first storing the image data on a local HDD before using the function imagecreatefromjpeg() to post-process the image further.

At certain times the locally stored image was corrupted during the store process and when called by imagecreatefromjpeg($imagePath); the PHP Warning: imagecreatefromjpeg(): gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file occured. showed up.

So to resolve the PHP warnings and to make up for the corrupted image I used a clearly defined solution in the PHP manual (http://php.net/manual/en/function.imagecreatefromjpeg.php) see the function LoadJpeg($imgname)

To prevent the further failures I have focused on the reason why the data supplied to imagecreatefromjpeg() function was not integrious in the first place. In my case it was the network flood issue which was causing some images to arrive corrupted.

Long story short, you may want to check what exactly is being supplied into imagecreatefromjpeg() function before trying to extend the base library etc..



回答3:

Try Adding @imagecreatefromjpeg($img) instead of imagecreatefromjpeg($img)

here @ is error suppressor