"gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file" in Codeigniter

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

}

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

here @ is error suppressor


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..