PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 9, 2022

[FIXED] How to handle this error caused by uploading a problematic JPEG?

 October 09, 2022     error-handling, gd, php     No comments   

Issue

I have a website where the public can upload JPEGs.

Someone from the public was uploading an invalid JPEG that was causing the site to crash for them.

PHP said...

imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file

I wasn't sure how to get around this, so I Googled and found this site. It told me to add...

ini_set('gd.jpeg_ignore_warning', 1);

I added that in my index.php (the bootstrap of my site, where I do other ini_set()).

This didn't seem to fix it.

How can I handle this case of invalid JPEGs? Am I doing something wrong with the INI set? I'm on a shared host so I can't change php.ini directly.

I'm using Kohana 2.3, and its Image library, but I don't think it is really relevant here.


Solution

Try sticking an @ character before the command:

$image = @imagecreatefromjpeg("file.jpg");
if(!$image) die("Sorry, bad JPEG");

It's dirty and probably obsolete (not to mention slow), but it'll probably make your code not fail.

Hope this helps!



Answered By - mattbasta
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing