Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 988
user3767643 Avatar asked Mar 17 '23 16:03

user3767643


2 Answers

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;    
    } 
}
like image 183
user4504661 Avatar answered Mar 21 '23 22:03

user4504661


the solution I found is the following:

$file_dimensions = getimagesize($_SERVER['DOCUMENT_ROOT'] . $fileUrl);
$ImageType = strtolower($file_dimensions['mime']);
switch(strtolower($ImageType))

{
 case 'image/png':
  $img_r = imagecreatefrompng($_SERVER['DOCUMENT_ROOT'] . $fileUrl);
 break;
 case 'image/jpeg':
  $img_r = imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT'] . $fileUrl);
break;
 default:
  die('Unsupported File!'); //output error
}
like image 41
Migue Cortés Avatar answered Mar 21 '23 20:03

Migue Cortés