Current location: Hot Scripts Forums » Programming Languages » PHP » Creating an image with php_gd2.dll


Creating an image with php_gd2.dll

Reply
  #1 (permalink)  
Old 06-13-09, 12:17 AM
DimensionX DimensionX is offline
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 ($im255255255);
        
$blue imagecolorallocate ($im0064);
            
        
//Draw image
        
imagefill($im00$blue);
        
imageline($im00$width$height$white);
        
imagestring ($im450150'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($resultMYSQL_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.
Reply With Quote
  #2 (permalink)  
Old 06-17-09, 05:10 PM
DimensionX DimensionX is offline
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
Reply With Quote
  #3 (permalink)  
Old 06-17-09, 09:31 PM
job0107's Avatar
job0107 job0107 is offline
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(200200);
        
$white imagecolorallocate($im255255255);
        
$blue imagecolorallocate($im0064);
        
imagefill($im00$blue);
        
imageline($im00$width$height$white);
        
imagestring($im450150$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.
Reply With Quote
  #4 (permalink)  
Old 06-18-09, 03:56 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Quote:
Originally Posted by job0107 View Post
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.

HTML Code:
<img src="image.php" />
Reply With Quote
  #5 (permalink)  
Old 06-18-09, 03:14 PM
Jcbones Jcbones is offline
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
PHP Code:

Header ('Content-type: image/png'); 

The whole page is an image.
Reply With Quote
  #6 (permalink)  
Old 06-19-09, 12:22 AM
DimensionX DimensionX is offline
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 : )
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
div css theighost CSS 11 09-14-08 02:30 AM
displaying text over image ketanco CSS 10 09-11-08 07:39 PM
Dynamically image button creating nitesh98 ASP.NET 5 06-20-08 03:31 AM
image creating problem zodehala PHP 1 12-01-07 01:03 PM
Creating an Image from Jtable Smurfet Everything Java 5 12-06-06 09:05 PM


All times are GMT -5. The time now is 08:38 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.