Page 1 of 1

help....wrapping the GD imageCreate in a class [solved]

Posted: Wed Jun 15, 2005 12:19 pm
by pthomas
I'm trying to create a class that will create an image from text. I've got it all working up to the point where it goes to actually display the image. Here's how I'd like the PHP file to look that calls the class

Code: Select all

$image = new Text2Image();
$image-> Set_Text ( "oh mighty test page 34 + 234 - 16 (513) *" );
$image->Set_Font_Type ( "luximbi" );
$image->Set_Font_Size ( 24 );
$image->Create_Image();
echo '<IMG SRC="' . $image->Show_Image() . '">';
That actually kinda works, its throwing back the binary image data onto the page instead of displaying the actual image. The Show_Image():

Code: Select all

function Show_Image()
{ 				
	header("Content-type: image/png");		//Set the header
	imagePNG($this->Image);				//Send the image to the browser
	imageDestroy($this->Image);				//throw away our canvas	
}
My question is how can I call a class function so that it sends the headers and such as many people have done using something like:

Code: Select all

<IMG SRC="text_to_image.php<?php echo $URL_stuff; ?>">
Thanks,
Paul

Re: help....wrapping the GD imageCreate in a class

Posted: Wed Jun 15, 2005 1:49 pm
by timvw
blah.html

Code: Select all

&lt;img src=&quote;image_generator.php&quote;/&gt;
image_generator.php

Code: Select all

include("image_class.php");
$image = new Text2Image();
$image-> Set_Text ( "oh mighty test page 34 + 234 - 16 (513) *" );
$image->Set_Font_Type ( "luximbi" );
$image->Set_Font_Size ( 24 );
$image->Create_Image();
$image->Show_Image();
image_class.php

Code: Select all

function Show_Image()
{ 				
	header("Content-type: image/png");		//Set the header
	imagePNG($this->Image);				//Send the image to the browser
	imageDestroy($this->Image);				//throw away our canvas	
}

Posted: Wed Jun 15, 2005 2:21 pm
by pthomas
Thanks for the reply, but I still need some help!

With the setup you mentionted below, I wasn't able to display the image on the blah.php page and I'm not sure why. I'm able to right click on the broken image icon and go to view image, but I get a "cannont modify header error" followed by the image jibberish (raw data).

If I follow the setup you mentioned, the problem I'm going to run into is that from the blah.php page (blah.html) I now have no way of changing the text, font size or anything else on the fly as it will now be hardcoded into the image_generator.php file. I'd like to later be able to use this class to create images based on data thats pulled from a database. and so I'd have to be able to specify the objects attributes in the blah.php page. Passing items via GET is out of the question due to design criteria :(

Paul

Posted: Wed Jun 15, 2005 2:27 pm
by timvw
you can also use $_SESSION to pass stuff around

blah.php

Code: Select all

<?php
session_start();
$_SESSION['text'] = 'blahblah';
?>
<img src="image_generator.php" />
image_generator.php

Code: Select all

session_start();
$text = $_SESSION['text'];

....

Posted: Wed Jun 15, 2005 2:45 pm
by pthomas
Ok, yeah I think using session variables is the best way to go then. Unfortunatley, I'm currently stuck, I can't get the image to display.

for the blah.php page

Code: Select all

<?php
session_start();
?>
<HTML>
<H3> This is the text-to-image test page </H3>
<?php
	$_SESSION['text'] = "print me";
	$_SESSION['font_type'] = "luximbi";
	$_SESSION['font_size'] = 12;
?>
	<IMG SRC="image_generator.php"/>
</HTML>
and for the image_generator.php page:

Code: Select all

<?php
include ("text2image-class.php");

session_start();

$image = new Text2Image();
$image->Set_Text ( $_SESSION['text'] );
$image->Set_Font_Type ( $_SESSION['font_type'] );
$image->Set_Font_Size ( $_SESSION['font_size'] );
$image->Create_Image();
$image->Show_Image();
?>
and in the class for the Show_image function is the only place where I send the browser any info and nothing is printed in the class by any echo commands:

Code: Select all

function Show_Image()
{ 				
	header("Content-type: image/png");		//Set the header
	imagePNG($this->Image);				//Send the image to the browser
	imageDestroy($this->Image);				//throw away our canvas	
}
But still the images doesn't show, any ideas?
Paul

Posted: Wed Jun 15, 2005 2:55 pm
by pthomas
I tried changing the image_generator.php to

Code: Select all

include ("text2image-class.php");

$image = new Text2Image();
$image->Set_Text ( "print me" );
$image->Set_Font_Type ( "luximbi" );
$image->Set_Font_Size ( 12 );
$image->Create_Image();
$image->Show_Image();
So that I could at least verify that my class is working, and something is screwed up... When I browse to image_generator.php I get:
Warning: Cannot modify header information - headers already sent by (output started at
.................../text2image-class.php:508) in
............../text2image-class.php on line 503
‰PNG the png image jibberish
line 508 is the very last line in my class file and line 503 is

Code: Select all

imagePNG($this->Image);
?
Paul