+ Almost all quality improvement comes via simplification of design, manufacturing... layout, processes, and procedures.

November 18, 2008, 3:29 am

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

March 17, 2008

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

  • php 4 : No action neccesarry it should work fine
  • php 5.0 - 5.1.2 = Upgrade to the latest php 5
  • php 5.1.3 - current = Declare this variable in your file before calling imagecreatefromjpeg()   

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.

Posted by Anthony LeBoeuf under PHP |

9 Comments »

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

    Comment by rhand — April 17, 2008 @ 6:39 am

  2. My PHP version is 5.2.3 by the way….

    Comment by rhand — April 17, 2008 @ 7:12 am

  3. Hi there you want to put that in your php file, let me know if you have any problems.

    Comment by Anthony LeBoeuf — April 17, 2008 @ 7:59 am

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

    Comment by Vitaliy — April 24, 2008 @ 3:50 pm

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

    Comment by rob — May 30, 2008 @ 12:15 pm

  6. yeah your right thanks for mentioning that, I guess wordpress converts quotes into backticks. why not just use add and stripslashses? ! lol

    Comment by Anthony LeBoeuf — June 18, 2008 @ 3:15 pm

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

    Comment by Dan — July 29, 2008 @ 1:52 pm

  8. Thx for this article.

    Works fine for me now!!

    Cheers,
    Seb

    Comment by Seb — September 7, 2008 @ 5:04 am

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

    Comment by Marc Wickens — October 1, 2008 @ 12:22 pm

RSS feed for comments on this post. | Trackback

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> .

*
To prove that you're not a bot, enter this code
Anti-Spam Image