#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    40
    Rep Power
    10

    inline images in <img> tag -how can I base64 encode a image created with gd libriary?


    Hello Devshed,

    I've have a function with dynamically creates a png image using php's GD library. Currently I am using a script which just outputs the image to the browser, Like so.
    Code:
    Header("Content-type: image/png");
    ImagePng($image);
    This is called from a php generated html page by the tag,

    <img src='map.php' width='600' height='600' alt='' border=0>.

    This work fine but I would like to place the image data inline (or within) the original html page. I've been googling heavily and there seems to be a way to do this using a <img> tag like so
    Code:
    <img src="data:image/png;base64, ..." width='600' height='600' alt='' border=0>
    The ... in the src represent the image base64 encoded.

    This is working great but I can't turn my GD image resource ($image) into a base64 encoded string. Using the command base64_encode($image) throws the error "Warning: base64_encode() expects parameter 1 to be string".

    Does anyone know how exactly the GD libriary is storing images and how do you convert these into a base64 encoded string?
    Thanks in advance for your time.

    Pete

    PS, The application will be used by mozilla users only and so ie quirks do not apply.
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Posts
    1,742
    Rep Power
    15
    I did a little experimenting and I can encode the image fine. It's reading it back that seems to be the problem.

    I doubt you'll be able to base64_encode the image in memory.
    It'll be something like "resource ID#23" and that's all you'll be encoding is the resource id or array name.

    Here's some stuff to play with anyway
    PHP Code:
    <html>
    <head>
      <title></title>
    </head>
    <body>
    <?php

     $time 
    time();

     
    $image imagecreatefromPNG("clock_bg.png");
     
    ImagePng($image"$time.png"100);
     
    $img base64_encode("$time.png");
     echo 
    $img;
     echo 
    "<p>&nbsp;</p>";
     
    $img base64_decode($img);
     echo 
    $img;
     echo 
    "<p>&nbsp;</p>";

     
    $fp fopen ("$time.png""rb");
     
    $bytes 10240;
     
    $buffer fread($fp$bytes);
     
    fclose ($fp);
     
    $base base64_encode($buffer);

     print (
    $base);
    ?>
    <img src="<?php echo"$img"?>">
    </body>
    </html>
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    40
    Rep Power
    10
    Thanks balamm,

    That's got me going again and trying some new stuff. Just a couple of questions with regards to some of the problems that have come up using a file based approach.
    Is there a good way of deleting the images after a certain amount of time as they will be out of date immediatly after they have been displayed.
    How do you handle the hypothetical situation of concurrent users using the function at the exact same time, thus mixing up the image files on the server.

    Thanks again,
    Pete

    Originally Posted by balamm
    I did a little experimenting and I can encode the image fine. It's reading it back that seems to be the problem.

    I doubt you'll be able to base64_encode the image in memory.
    It'll be something like "resource ID#23" and that's all you'll be encoding is the resource id or array name.

    Here's some stuff to play with anyway
    PHP Code:
    <html>
    <head>
      <title></title>
    </head>
    <body>
    <?php

     $time 
    time();

     
    $image imagecreatefromPNG("clock_bg.png");
     
    ImagePng($image"$time.png"100);
     
    $img base64_encode("$time.png");
     echo 
    $img;
     echo 
    "<p>&nbsp;</p>";
     
    $img base64_decode($img);
     echo 
    $img;
     echo 
    "<p>&nbsp;</p>";

     
    $fp fopen ("$time.png""rb");
     
    $bytes 10240;
     
    $buffer fread($fp$bytes);
     
    fclose ($fp);
     
    $base base64_encode($buffer);

     print (
    $base);
    ?>
    <img src="<?php echo"$img"?>">
    </body>
    </html>
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Posts
    1,742
    Rep Power
    15
    Using Unix time(); for the filename, chances are real slim that any 2 users would ever get the same file.
    You could add sessions, microtime, or something similar to the filename as well I guess.

    to delete the temp file, just add
    PHP Code:
    unlink("$time.png"); 
    to the end of the script

IMN logo majestic logo threadwatch logo seochat tools logo