SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question printing a base64 encoded image with IE

    in a nutshell, i'm displaying a base64 encoded image (that is being parsed from a remote xml file) in the browser window doing a simple header output:
    PHP Code:
    header('Content-Length: '.$length);
    header('Content-Type: image/gif');
    echo 
    base64_decode($image); 
    it will print fine with Firefox but nothing in IE (just a box with an x in the corner). didn't realize that IE wouldn't print these. am i stuck with having to save the image to the server for IE to be able to print it? i didn't really want to have to save these images out as they are temporary.

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    I think the length header might be wrong. Try this:

    PHP Code:

    $image 
    base64_decode($image);
    $length strlen($image);

    header('Content-Length: '.$length);
    header('Content-Type: image/gif');
    echo 
    $image
    And even then, I'm not sure strlen is the best way of getting the image's size. But give it a try, it might work

  3. #3
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think i had seen it done that way on an example. but it actually seems the only way to get it work for IE is to temporarily write the image out to the server then delete it when done with it. didn't really want to have to do it that way, but I can't seem to find any other way to do it.

  4. #4
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry 1 down, 1 to go...

    and of course, this isn't working in IE6! I can view it in both IE6-7, but it only prints in IE7. here's what i'm doing...
    PHP Code:
    //save the file
    $outputfile $shipid.'.gif';
    $ifp fopen('tmp/'.$outputfile"wb");
    fwrite($ifpbase64_decode($image));
    fclose($ifp);
    echo 
    '<img src="fetch.php?id='.$outputfile.'" />'
    fetch.php is thus, read file and delete
    PHP Code:
    header('Content-Type: image/gif');
    readfile('image/'.$_GET['id']);
    unlink('image/'.$_GET['id']); 
    anyone have any other ideas?

  5. #5
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Going back to your original attempt at directly displaying the image, what does the rest of your script look like? You're not trying to output HTML and the image in the same script, are you? If so, it won't work (although that it works in FF suggests that you're not). Are you sure the image is a GIF, and are you sure you're providing the correct file size? (And yes, when your data is in a string like in Immerse's example, using strlen is perfectly fine for getting the file size.)
    PHP questions? RTFM
    MySQL questions? RTFM

  6. #6
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep, it's a gif and no, not outputting any other html. do you think i should i get the length before or after decoding it?

  7. #7
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bbolte View Post
    do you think i should i get the length before or after decoding it?
    After. The length should be the length of the data you send.
    <.smarter.web.development.>
    PHP Stuff: Plexus | Chocolate (BDD Framework... coming soon)
    Graphite

  8. #8
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i changed it to that and still no change. i verified that i'm getting something in the length. here's i did:
    PHP Code:
    $length strlen(base64_decode($image));
    header('Content-Type: image/gif');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.$length);
    echo 
    base64_decode($image); 
    i echo'd the $length var out and it shows 32880

  9. #9
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is that the correct size for your image, 32,880 bytes? Try leaving out the Content-Transfer-Encoding header.
    PHP questions? RTFM
    MySQL questions? RTFM

  10. #10
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kromey View Post
    Is that the correct size for your image, 32,880 bytes? Try leaving out the Content-Transfer-Encoding header.
    not sure, i'll have to check on the size. but it didn't matter whether i had the transfer-encoding in or out. did it both ways. i've given up on it for now, wasted too much time and have to move on. i wrote a routine to delete the gif images from the previous day when someone hits the first part of module. thanks for your help though.

  11. #11
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Is there any output after the image?

    Also, it's coming from an XML file?
    Are there CDATA tags around the data in the node? I don't think so, otherwise Firefox probably wouldn't display it either, but I tried the following and it worked fine in all the browsers I tested it in:

    PHP Code:
    <?php
    $image 
    base64_encode(file_get_contents('apps/icms/styles/default/images/logo.png'));
    $image base64_decode($image);
    header('Content-Type: image/png');
    header('Content-Length: '.strlen($image));
    echo 
    $image;
    ?>
    (I put the encoding and decoding in to mimic what you were attempting).

    One more suggestion, should you decide to try this again, is to write the image out to a file, and then to open that in an image viewing tool (ACDSee, IrfanView, Photoshop etc). See if that can open the image...


Bookmarks

Posting Permissions

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