imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error - Laravel

user3767643 picture user3767643 · Mar 20, 2015 · Viewed 12.6k times · Source

I have 2 below routes :-

Route::get('resize/avatar', function() {
    $image = 'avatar.jpg';
    $target_filename_here = 'thumbnail_'.$image;
    $ffs = imagecreatefromjpeg($image);
    $size = getimagesize($image);
    $dst = imagecreatetruecolor(100,100);
    $dds = imagecopyresampled($dst,$ffs,0,0,0,0,100,100,$size[0],$size[1]);
    $dn = imagepng($dst,$target_filename_here); // adjust format as needed
    imagedestroy($ffs);
    imagedestroy($dst);
    if($dds) {
        return Redirect::to('color/');
    } else {
        return 'Failed to load the Profile Picture';
    }
});


Route::get('color/', function() {
    if(file_exists('thumbnail_avatar.jpg')) {
        $dest = imagecreatefrompng('transcript.png');
        $fn = imagecreatefromjpeg('thumbnail_avatar.jpg');
        imagecopy($dest, $fn, 550, 830, 0, 0, imagesx($fn), imagesy($fn));
        imagejpeg($dest,"test4.jpg",90);
        imagedestroy($dest);
        imagedestroy($fn);
        return HTML::image('test4.jpg');
    } else {
        return Redirect::to('resize/avatar');
    }
});

And i am getting error as title, I am trying to figure it out, where i am creating an issue, but cannot find it.

My Framework, that i am using is "Laravel 4.2" , but i am pretty sure, it has nothing to do with the framework, as functions are pure php.

for TL;DR

I am trying to create thumbnail of profile picture, and than merging it to another image. While doing that, I am getting :- imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error:

I have already searched stackoverflow and google for error like mine, but i am unable to find and debug.

Update : enter image description here

Thanks

FAQ :-

  1. Memory Limit ? ==> I have sufficient memory.

  2. Using "@" in function to hide the error ? ==> It doesn't matter. I am still facing this issue.

  3. Have you tried another images ? ==> Yes, I have tried other images, and i am successful in doing that, it only creates the problem with "generated thumbnail".

My Question Kindly try yourself, using below script for non-laravel :-

function resizeImage() {
    $image = 'avatar.jpg';
    $target_filename_here = 'thumbnail_'.$image;
    $ffs = imagecreatefromjpeg($image);
    $size = getimagesize($image);
    $dst = imagecreatetruecolor(100,100);
    $dds = imagecopyresampled($dst,$ffs,0,0,0,0,100,100,$size[0],$size[1]);
    $dn = imagepng($dst,$target_filename_here); // adjust format as needed
    imagedestroy($ffs);
    imagedestroy($dst);
    if($dds) {
        color();
    } else {
        return 'Failed to load the Profile Picture';
    }
}

function colorCheck() {
    if(file_exists('thumbnail_avatar.jpg')) {
        $dest = imagecreatefrompng('transcript.png');
        $fn = imagecreatefromjpeg('thumbnail_avatar.jpg');
        imagecopy($dest, $fn, 550, 830, 0, 0, imagesx($fn), imagesy($fn));
            imagejpeg($dest,"test4.jpg",90);
            imagedestroy($dest);
            imagedestroy($fn);
        return HTML::image('test4.jpg');
    } else {
        resizeImage();
    }
}

And you can see the error.

Thanks

Answer

user4504661 picture user4504661 · Feb 9, 2017

I know I am a bit late but I just had a similar problem and it was driving me crazy.

To answer your question " ...I am trying to figure it out, where I am creating an issue"... The error is being created at

$ffs = imagecreatefromjpeg($image);

This error came from executing this line of code, when uploading an image jpg and then working with it with the PHP GD2 Library

I followed the solution in the link below and it solved my issue.

http://anvilstudios.co.za/blog/2010/02/23/imagecreatefromjpeg-gd-jpeg-library-reports-unrecoverable-error/

I hope somebody will find this useful and save some time. Kudos to Nico from anvilstudios

Update For those who cant access the link here is the code

$file_tempname = null;
if (is_uploaded_file($FILE['tmp_name'])) {
    $file_tempname = $FILE['tmp_name'];
}
else{
    exit('Wrong file type');
}

$file_dimensions = getimagesize($file_tempname);
$file_type = strtolower($file_dimensions['mime']);

if ($file_type=='image/jpeg'||$file_type=='image/pjpeg'){
    if(imagecreatefromjpeg($file_tempname)){
        $ffs = imagecreatefromjpeg($file_tempname);
        return $ffs;    
    } 
}