php 5 and imagecreatefromjpeg() recoverable error: Premature end of JPEG file

php 5 and imagecreatefromjpeg() recoverable error: Premature end of JPEG file

Ok I recently came up with a problem creating thumbnails for a project I am working on. I was stumped for hours, it seems that some files that were saved from a mac or certain photo editing programs would cause php to not recognize it a jpg thus giving the error:  

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

After researching this problem I realized it was a problem with php 5 and gd2. Heres how to fix it

ini_set(‘gd.jpeg_ignore_warning’, 1);

Doing that will cause gd2,php to ignore the error and continue where it use to just fail and do nothing.

  • rhand
    April 17, 2008 at 6:39 am

    Where do you add this code? In the PHP script or in the .htaccess?

  • rhand
    April 17, 2008 at 7:12 am

    My PHP version is 5.2.3 by the way….

  • Vitaliy
    April 24, 2008 at 3:50 pm

    Thank you, you were of great help. It does work now-))

  • rob
    May 30, 2008 at 12:15 pm

    If you copy and paste the ini_set command above, it probably won’t work, because those are backticks. You won’t get a php error on it either. Just make sure you change the backticks to either single or double quotes, and it should work fine.

  • Dan
    July 29, 2008 at 1:52 pm

    That was amazingly helpful. I was working with photos off my Canon SD1000 and never thought that the photo would have been the issue, but it worked when I put that ignore in.

    Nice Work

  • Seb
    September 7, 2008 at 5:04 am

    Thx for this article.

    Works fine for me now!!


  • Marc Wickens
    October 1, 2008 at 12:22 pm

    Thanks you saved my life! this has been wracking my brain for hours.

  • Dave
    November 7, 2008 at 7:03 pm

    i have a test im doin where u try to get past different levels. The level im stuck on requires you to view an image a jpg link. The image on the page cannot be copied or sent via webpage. I tried opening it from a URL option in GIMP and gives me premature jpg error. If anyone knows how to change the source code and reopen it with the added text let me know and i will give the address thanks.

  • farai madzima
    November 28, 2008 at 8:24 am

    Thanks, I’ve spent ages trying to figure this one out.
    Your solution lets the script continue, any idea why gd throws that error?

  • Pablo
    February 17, 2009 at 11:49 am

    Thank’s!! Gracias! Me has ayudado enormemente!!

  • mason
    July 30, 2009 at 5:44 pm

    Wow, I can’t guess at how much time you saved me. The clients files were failing but mine were ok. Thank you so much for posting this.

Post a Comment