On Mon, Dec 6, 2010 at 5:01 PM, Daevid Vincent <dae...@daevid.com> 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
$oImgRenderer = new LOPA(); // or whatever your class is called