Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Feb 2007
    Posts
    217
    Thanks
    25
    Thanked 1 Time in 1 Post

    GD error handling?

    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()

  • #2
    Regular Coder
    Join Date
    Jan 2009
    Location
    Norway
    Posts
    118
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Try putting a @ synbol in front of the function, it should supress the error message.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    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.

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Regular Coder
    Join Date
    Feb 2007
    Posts
    217
    Thanks
    25
    Thanked 1 Time in 1 Post
    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");

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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

    PHP Code:
    if (false === $img)
    {
        
    // creation has failed

    Also, these (and every thing else really) are chainable
    PHP Code:
    if (false !== ($img = @imagecreatfromjpeg($picture)))
    {
        
    // its all good in here.

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #6
    Regular Coder
    Join Date
    Feb 2007
    Posts
    217
    Thanks
    25
    Thanked 1 Time in 1 Post
    Well I've tried implementing the examples given, but for some strange reason it's not working :S

    PHP Code:
    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
    PHP Code:
    if (false !== ($img = @imagecreatefromjpeg($picture))){}else{$fotofout++;} 
    just a check or does it execute it as well?

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    PHP Code:
    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($imgIMG_FILTER_EMBOSS);
                break;
        }
    }
    else
    {
        ++
    $fotofout;

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •