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

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

 



 

> -----Original Message-----
> From: Daevid Vincent [mailto:daevid@xxxxxxxxxx] 
> Sent: Monday, December 06, 2010 4:02 PM
> To: php-general@xxxxxxxxxxxxx
> Subject:  How can I call GD's imagepng() directly from my class?
> 
> 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?
> 

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....

	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">



-- 
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