Results 1 to 2 of 2

Thread: Tip for PHP/GD programmers: Outputting images and debug information together

  1. #1
    HEXUS webmaster Steve's Avatar
    Join Date
    Nov 2003
    Posts
    14,283
    Thanks
    293
    Thanked
    841 times in 476 posts

    Cool Tip for PHP/GD programmers: Outputting images and debug information together

    Hi guys,

    Been working on some image related stuff today, and when using libraries that spit out PNGs, it can be annoying when the PNG data gets corrupted by warning messages, stray echo/print statements etc. Of course, such things are very useful for debugging, so how do we view them without killing the image data integrity?

    The obvious way is to output the image to file, but checking that file takes time. Here's how to get debug output and the resultant image on a single web page in one hit:
    PHP Code:
    /* Code that might have warnings, print statements etc */

    //Now catch the image output:
    ob_start();
    functionthatprintsimagestream();
    $im base64_encode(ob_get_contents());
    ob_end_clean();
    //Set content type in case output hasn't been flushed yet and image function has set type.
    header('Content-type: text/html',true);
    echo 
    '<img src="data:image/png;base64,'.$im.'" alt="Thanks steve" />';

    /* Anything else... */ 
    Be sure to set the data mimetype up appropriately in the <img /> tag.

    Obviously if you can get your image data without having to catch it using output buffers, that's great, and you can still base64_encode for inline output. I couldn't do this, however (easily).

    The key here is the use of encoded data in the image tag, so in theory you could do this in other scripting languages supporting image manipulation.
    Last edited by Steve; 09-09-2009 at 07:26 PM.
    PHP Code:
    $s = new signature();
    $s->sarcasm()->intellect()->font('Courier New')->display(); 

  2. #2
    Almost Ex-HEXUS Staff Jonatron's Avatar
    Join Date
    Sep 2009
    Location
    London
    Posts
    719
    Thanks
    48
    Thanked
    285 times in 175 posts

    Re: Tip for PHP/GD programmers: Outputting images and debug information together

    Thanks Steve, I'll use this.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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