...

View Full Version : GD error handling?



docock
06-24-2010, 08:40 AM
How can I surpress errormessages if something goes wrong in processing images with GD? Right now the errors are shown on the screen so it's visible for visitors of my site. Also I would like to know if there is a way to know if an error occured.

for example this kind of errors:

[function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error:
[phpBB Debug] PHP Notice: in file /mdwgold/scripts/mdw.php on line 29: imagecreatefromjpeg()

CyberPirate
06-24-2010, 10:44 AM
Try putting a @ synbol in front of the function, it should supress the error message.

Fou-Lu
06-24-2010, 03:52 PM
Make sure you handle your errors in another way if you do supression (which is what I'd recommend doing anyway).
ImageCreateFrom* return a resource. If you have no resource, it has failed. You can use this to determine if the script should continue processing this branch.


header('Content-type: image/jpeg;');
if (false !== @imagecreatefromjpeg('somefile.jpg'))
{
// Its ok, we keep going:
... and so forth
}
else
{
// Here, I'd use fopen / file_get_contents, and open a default image which simply shows
// that an error occured. This must be a flat file image though, and match the header specified for the image type.
}

docock
06-25-2010, 12:37 AM
Thanks for the reply, I appreciate it, and it helped me a lot.. One question though, how can I check if this line executed without problems?

$img = @imagecreatefromjpeg("$picture");

Fou-Lu
06-25-2010, 12:48 AM
The exact same way I've shown, but use the variable instead. I just noticed I kinda forgot to include a variable to capture the result lol



if (false === $img)
{
// creation has failed
}

Also, these (and every thing else really) are chainable


if (false !== ($img = @imagecreatfromjpeg($picture)))
{
// its all good in here.
}

docock
06-25-2010, 10:04 AM
Well I've tried implementing the examples given, but for some strange reason it's not working :S


if (false !== ($img = @imagecreatefromjpeg($picture))){}else{$fotofout++;}
$filter="emboss";
if ($filter == 'emboss'){ imagefilter($img,IMG_FILTER_EMBOSS);}


I'm getting an error imagefilter() expects parameter 1 to be resource, boolean given on the imagefilter line. Also later on the script I'm getting an other error message: imagecopyresized(): supplied argument is not a valid Image resource
.

Is the code
if (false !== ($img = @imagecreatefromjpeg($picture))){}else{$fotofout++;} just a check or does it execute it as well?

Fou-Lu
06-25-2010, 03:58 PM
The code is both a check and an assignment.

The imagecreation has failed, but your code allows it to slip through. You need to be very controlling of your branches especially with resources. I don't know what $fotofout is representing, so I don't know if its actually supposed to be in the failure condition but I'll leave it there anyway:


if (false !== ($img = @imagecreatefromjpeg($picture)))
{
// This can ONLY happen if $img != false. So it must be in here.
$filter="emboss";

// I have a feeling that $filter is going to be much longer than it looks.
// So I converted the if to a switch. No need for a default unless there is
// a filter you always want to apply.
switch ($filter)
{
case 'emboss':
imagefilter($img, IMG_FILTER_EMBOSS);
break;
}
}
else
{
++$fotofout;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum