iDeej
asked on
PHP: GD2 image support problem
Can you see any problems with my code below?
So far I have tried this to test whether gd2 support is set up.
echo function_exists('gd_info') ; // returned true.
phpinfo(); // gd section had gd support enabled.
So I'm Assuming I have it installed correctly.
This is my first attempt with working with images with php so any help would be much appreciated.
So far I have tried this to test whether gd2 support is set up.
echo function_exists('gd_info')
phpinfo(); // gd section had gd support enabled.
So I'm Assuming I have it installed correctly.
This is my first attempt with working with images with php so any help would be much appreciated.
<?php
// Set up the image.
$height = 200;
$width = 200;
$im = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($im, 255, 255, 255);
$blue = imagecolorallocate($im, 0, 0, 64);
// Draw on the Image.
imagefill($im, 0, 0, $blue);
imageline($im, 0, 0, $width, $height, $white);
imagestring($im, 4, 50, 150, 'Sales', $white);
// Output image
Header ('Content-type: image/png');
imagepng ($im);
// Clean up
imagedestroy($im);
?>
ASKER
I have just tried the code by itself and it works, however I have the page as part of an HTML page in which it does not work.
Could this be something to do with the header location. I have tried to put it at the top of the page but this did not work.
Here is the complete page:
Could this be something to do with the header location. I have tried to put it at the top of the page but this did not work.
Here is the complete page:
<?php
// I have tried to put the header here and it did not work.
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?php
// I have also tried to put the header here and it did not work.
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<title>Generating Images</title>
<link rel="stylesheet" type="text/css" href="" />
<script type="text/javascript" src=""></script>
</head>
<body>
<div id="header">
</div>
<div id="container">
<div id="content">
<?php
// Set up the image.
$height = 200;
$width = 200;
$im = imagecreatetruecolor($width, $height);
$white = imageColorAllocate($im, 255, 255, 255);
$blue = imageColorAllocate($im, 0, 0, 64);
// Draw on the Image.
imagefill($im, 0, 0, $blue);
imageline($im, 0, 0, $width, $height, $white);
imagestring($im, 4, 50, 150, 'Sales', $white);
// Output image
Header ('Content-type: image/png'); // The header I have tried in other locations at top of page
imagepng ($im);
// Clean up
imagedestroy($im);
?>
</div>
<div id="navCol">
</div>
<div id="footer">
</div>
</div>
</body>
</html>
Thats the problem, you can output headers after HTML is sent to the browser.
1 sec and I clean it up for you.
1 sec and I clean it up for you.
ASKER
Here is the error message get:
The image "http://localhost/php_sandbox/ch22/" cannot be displayed, because it contains errors.
The image "http://localhost/php_sandbox/ch22/" cannot be displayed, because it contains errors.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
And for the record, you can still pass variables (get) like this:
<img src="image.php?var=somethi ng" />
<img src="image.php?var=somethi
Inside a web page it is appropriate to write the PNG image to the server and use the HTML <img> tag to retrieve the image and insert it into the web page. Something like this...
$imgfile = getcwd() . 'my.png';
imagepng ($im, $imgfile);
echo "<img src=\"my.png" />\n";
One thing should be noted about Ray's method, is that when you write the PNG image to the server, you can no longer send it variables. Usually the reasoning for using PHP for images is for dynamic content. However, since the PHP file sends out image headers (Header ('Content-type: image/png');), it can be read like a PNG image and still keep it's flexibility.
ASKER
Thank you kindly Mahdii7. Complete, accurate, and easy to understand. I don't know whether there is much use or power in generating images with php this way but I am once again excited for now anyway.
ASKER
Thanks for the extra info.
Always glad to help, thanks for the points.
Do you get any output? Are there any messages in your PHP error log?