How to deal with this error caused by loading problem JPEG

I have a website where the public can upload JPEG files.

Someone from the public uploaded an invalid JPEG, because of which the site crashed into them.

PHP said ...

imagecreatefromjpeg () [function.imagecreatefromjpeg]: gd-jpeg, libjpeg: error being restored: premature end of JPEG file

I was not sure how to get around this, so I googled and found this site . He told me to add ...

ini_set('gd.jpeg_ignore_warning', 1); 

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

This does not seem to fix it.

How can I handle this invalid jpeg case? Am I doing something with the INI kit? I am on a shared host, so I cannot directly modify php.ini .

I use Kohana 2.3 and its Image Library , but I do not think this is really relevant here.

thanks

+6
php error-handling gd
source share
2 answers

Try sticking the @ symbol before the command:

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

It is dirty and maybe outdated (not to mention slow), but it will probably make your code not work.

Hope this helps!

+9
source share

Usually you work with imagecreatefromjpeg as follows:

 $img = @imagecreatefromjpeg($file); if (!$img) { // handle error yourself } 

Pay attention to @ before imagecreatefromjpeg , which is used to suppress errors. Unfortunately, I cannot tell you how Kohana does this internally, and if it can be persuaded to do the same.

+5
source share

All Articles