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 Mon, Dec 6, 2010 at 5:01 PM, Daevid Vincent <daevid@xxxxxxxxxx> wrote:

> I have a class that does some massive computations to compute a LOPA
> (layout of passenger aircraft).
>
> I currently render it in an HTML table with little seats. I want to now
> make this using GD so I can show entire fleets of aircraft on one page.
>
> Inside my LOPA.class.php I have this method:
>
>        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 );
>
>                header( "Content-type: image/png" );
>                header('Content-Length: ' . strlen($my_img));
>                imagepng( $my_img );
>
>                imagecolordeallocate( $line_color );
>                imagecolordeallocate( $text_color );
>                imagecolordeallocate( $background );
>                imagedestroy( $my_img );
>        }
>
>
> And I'm trying to call it from a PHP page like so:
>
> <img src="<?php $my_lopa->render_image(); ?>" alt="Image created by a PHP
> script" width="200" height="80">
>
> But it doesn't show the picture. :\
>
> If I take the contents of that function and dump it into a "gdtest.php"
> file and call it like this however, it does work:
>
> <img src="images/gdtest.php" alt="Image created by a PHP script"
> width="200" height="80">
>
> So what am I doing wrong above that I can't just call it from my class?


Your approach with the class should show as src="" on your rendered web page
since the output from the function is binary meant for the browser, read:
not meant as content for an HTML attribute.

The later however is fine, since its text which references a php page.  What
happens when that is sent to the browser is it invokes images/gdtest.php on
your server, which outputs the binary directly to the browser.

If you want to use the class, you should do as your second example for the
tag itself, then inside of images/gdtest.php instantiate the class and call
the method.

gdtest.php
----
<?php
 $oImgRenderer = new LOPA(); // or whatever your class is called
 $oImgRenderer->render_image();
?>

-nathan

[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