Creating an image with php_gd2.dll
06-13-09, 12:17 AM
Newbie Coder
Join Date: May 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Creating an image with php_gd2.dll
Hi,
I'm currently learning how to draw using php_gd2.dll, I've made a file that when it's just php works fine, if I attach some other code that displays the results of a database and puts the in a table with php using echos the page comes up with an error, again the grab from a database and put it into a table code works fine also, I'm wondering if when you draw that it turns the page into a complete image which would mean if i want to include the image i would have to link it or include it on the page, is that correct?
PHP Code:
<html> <head><title></title><?php require 'core/config.php' ; //Assign the query $query = "SELECT * FROM client ORDER BY clientId ASC" ; //Execute the query $result = mysql_query ( $query ); if(! $result ){ die ( "Could not query the database: <br />" . mysql_error ()); } function image ( $height , $width ) { //Set up image $im = imagecreatetruecolor ( $width , $height ); $white = imagecolorallocate ( $im , 255 , 255 , 255 ); $blue = imagecolorallocate ( $im , 0 , 0 , 64 ); //Draw image imagefill ( $im , 0 , 0 , $blue ); imageline ( $im , 0 , 0 , $width , $height , $white ); imagestring ( $im , 4 , 50 , 150 , 'Yay' , $white ); //Output file Header ( 'Content-type: image/png' ); imagepng ( $im ); //Clean up imagedestroy ( $im ); } ?> </head> <body><?php $varWidth = 200 ; $varHeight = 200 ; image ( $varWidth , $varHeight ); //Creating table echo '<table border="1">' ; echo "<tr><td>Client ID</td><td>First name</td><td>Surname</td><td>Address</td><td>Town</td><td>State</td><td>Zip code</td></tr>" ; while ( $result_row = mysql_fetch_array ( $result , MYSQL_ASSOC )){ echo "<tr><td>" ; echo $result_row [ "clientId" ] . '</td><td>' ; echo $result_row [ "firstName" ] . '</td><td>' ; echo $result_row [ "surName" ] . '</td><td>' ; echo $result_row [ "address" ] . '</td><td>' ; echo $result_row [ "town" ] . '</td><td>' ; echo $result_row [ "state" ] . '</td><td>' ; echo $result_row [ "zip" ] . '</td><td>' ; } echo ( "</table>" ); ?> </body> </html>
sorry about the formatting I'm still new to php and haven't learned the coding conventions, the strange thing is if i take it out of the html shell the image drawing works perfectly :S
Any help or advice is appreciated,
thanks : )
Last edited by DimensionX; 06-13-09 at 12:21 AM .
06-17-09, 05:10 PM
Newbie Coder
Join Date: May 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Maybe i should clarify the question a little?
ok when you make an image out of php does it treat the whole page as an image, what i mean is if your going to use a dynamically made image should you make it then include it on another page? cause as i mentioned above i've tried to draw an image and then include html commands but it came up with errors.
again, any help is appreciated
06-17-09, 09:31 PM
Community Liaison
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
You need to create all the images you want first, and store them in files.
Then display them in image elements in the HTML.
Something like this:
PHP Code:
<?php $file_name = "img1.png" ; $text = "Success!!!" ; $im = imagecreatetruecolor ( 200 , 200 ); $white = imagecolorallocate ( $im , 255 , 255 , 255 ); $blue = imagecolorallocate ( $im , 0 , 0 , 64 ); imagefill ( $im , 0 , 0 , $blue ); imageline ( $im , 0 , 0 , $width , $height , $white ); imagestring ( $im , 4 , 50 , 150 , $text , $white ); imagepng ( $im , $file_name ); imagedestroy ( $im ); ?> <html> <body> <img src="img1.png" alt="img1.png"> <div>img1.png</div> </body> </html>
__________________
Jerry Broughton
Last edited by job0107; 06-17-09 at 09:48 PM .
06-18-09, 03:56 AM
Community Leader
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Quote:
Originally Posted by
job0107
You need to create all the images you want first, and store them in files.
Either that, or you could put the image stuff in an separate file, and embed this as image instead.
06-18-09, 03:14 PM
Aspiring Coder
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
To answer your question explicitly, YES.
When you send this header
The whole page is an image.
06-19-09, 12:22 AM
Newbie Coder
Join Date: May 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
thanks!!! sooooo much : D
been checking often lol, thanks alot for the replies : )
and yes that solves my question : )
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off