On Mon, Dec 6, 2010 at 5:26 PM, Daevid Vincent <dae...@daevid.com> wrote:

>
> I got a little 'hack' further, but not loving it. Maybe I'll move the image
> to a $_SESSION variable and then have the "gdtest.php" pull it and echo it
> that way....
>

well i think you may be in the right direction, however, id be interested to
see what others on the list think about this particular approach.
 personally, i wouldnt store an image in the session.. but i would certainly
provision a temporary directory inside the webroot where you could put these
images.

perhaps if the images are tied to sessions, throw cleanup in a custom
session cleanup cron, or queue the images for deletion when the user logs
out.  seems to me like the session is an area of abuse in many php apps ive
seen.  even though the standard store is file based, i would still recommend
a separate store for images.


>
>        public function render_image()
>        {
>                $my_img = imagecreate( 200, 80 );
>                $background = imagecolorallocate( $my_img, 0, 0, 255 );
>                $text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
>                $line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
>                imagestring( $my_img, 4, 30, 25, "Test Image", $text_colour
> );
>                imagesetthickness ( $my_img, 5 );
>                imageline( $my_img, 30, 45, 165, 45, $line_colour );
>
>                 ob_start();
>                 header( "Content-type: image/png" );
>                header('Content-Length: ' . strlen($my_img));
>                imagepng( $my_img );
>                 $final_image_data = ob_get_contents();
>                ob_end_clean();
>
>                imagecolordeallocate( $line_color );
>                imagecolordeallocate( $text_color );
>                imagecolordeallocate( $background );
>                imagedestroy( $my_img );
>
>                 echo
> 'data:image/png;base64,'.base64_encode($final_image_data);
>        }
>
> <img src="<?php $my_lopa->render_image(); ?>" width="200" height="80">
>

i dunno, why not just have something a bit simpler, where you generate an
image and store it to disk.  the function could return the path where the
image was saved.  then when the browser loads that resource php doesnt even
fire up, the webserver just sends back the image directly.

not sure on whether you would pass in the location or if you would have
internal logic cook up the path, but im sure you could determine that based
on the rest of the code .. lets suppose your class computes it internally,

something like

<?php
class LOPA
{
  private function get_temp_path()
  {
     // some session specific logic to cook up a temporary path
     // something relative to the web root ...
  }

  public function render_image()
  {
    $my_img = imagecreate( 200, 80 );

    $background = imagecolorallocate( $my_img, 0, 0, 255 );
    $text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
    $line_colour = imagecolorallocate( $my_img, 128, 255, 0 );

    imagestring( $my_img, 4, 30, 25, "Test Image", $text_colour);
    imagesetthickness ( $my_img, 5 );
    imageline( $my_img, 30, 45, 165, 45, $line_colour );

    // now saving to a temp location inside the webroot
    $temp_path = $this->get_temp_path();
    imagepng( $my_img, $temp_path );

    imagecolordeallocate( $line_color );
    imagecolordeallocate( $text_color );
    imagecolordeallocate( $background );
    imagedestroy( $my_img );

    // returning path to temp location
    return $temp_path;
  }
}
?>

<!-- now this just has a path which points right back to an image, served
directly -->
<img src="<?php $my_lopa->render_image(); ?>" width="200" height="80">

-nathan

Reply via email to