Link to home
Create AccountLog in
PHP

PHP

--

Questions

--

Followers

Top Experts

Avatar of iDeej
iDeej🇦🇺

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.
<?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);
	
 
?>

Open in new window

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Mahdii7Mahdii7🇺🇸

Copy + Paste of your snippet generates a blue image with a diagonal line, on the bottom it reads: "Sales".

Do you get any output? Are there any messages in your PHP error log?

Avatar of iDeejiDeej🇦🇺

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

Open in new window


Avatar of Mahdii7Mahdii7🇺🇸

Thats the problem, you can output headers after HTML is sent to the browser.

1 sec and I clean it up for you.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of iDeejiDeej🇦🇺

ASKER

Here is the error message get:

The image "http://localhost/php_sandbox/ch22/" cannot be displayed, because it contains errors.


ASKER CERTIFIED SOLUTION
Avatar of Mahdii7Mahdii7🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of Mahdii7Mahdii7🇺🇸

And for the record, you can still pass variables (get) like this:

<img src="image.php?var=something" />

Avatar of Ray PaseurRay Paseur🇺🇸

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

Open in new window


Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Mahdii7Mahdii7🇺🇸

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.

Avatar of iDeejiDeej🇦🇺

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.

Avatar of iDeejiDeej🇦🇺

ASKER

Thanks for the extra info.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Mahdii7Mahdii7🇺🇸

Always glad to help, thanks for the points.
PHP

PHP

--

Questions

--

Followers

Top Experts

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.