Re: How can I call GD's imagepng() directly from my class?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 7 December 2010 07:19, Nathan Nobbe <quickshiftin@xxxxxxxxx> wrote:
> On Mon, Dec 6, 2010 at 5:26 PM, Daevid Vincent <daevid@xxxxxxxxxx> 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
>
I had a similar issue in generating a badge based upon various stats.

I found that there were many individuals who shared the same stats, so
the badge was the same. Even more advanced users often shared stats.

So, rather than creating an image per individual, I based the name of
the image on $md5BadgeData = md5(serialize($badgeData)).

So, main page loads and gathers the stats for person.

If the badge doesn't exist, then creates the PNG file.

Write out the HTML using <img src="./badges/{$md5BadgeData}.png" />

If a user's stats change, they'll have a new badge. Sure, there will
be orphaned badges, but they are tiny little things and I don't have
gazillion users.




-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux