I'm using GD to dynamically draw a rectangle on an image I have of a map. I've got that part working fine.

Here's my question:
I know I need to set the headers of the page to content-type: image/png , otherwise I get a bunch of characters instead of the picture.
But what if I want to place this dynamically created image inside a PHP or HTML document so that I can place text and other data around it? Is there any way to do this, or does the GD-generated image have to stay on its own page?

- EF

8 Years
Discussion Span
Last Post by EvolutionFallen

If you want to place it dynamically inside another page you would have one PHP file generate the image and then point to that in an <img> tag.

But to set the content-type just use:

header('Content-Type: image/png');

I'm clear on how to set the content-type header. My problem is that I don't want to save the dynamically created image. Here's an example of what I'm trying to accomplish.

Dynamic image creation:

	// File: image.php
	// Description: Displays the map and highlights the appropriate aisle
	header("content-type: image/png");
	$mapimg = @imagecreatefromgif('3fl-stacks.gif') or die("Cannot initialize new GD image stream");
	$hl_color = imagecolorallocate($mapimg, 255, 255, 0);
	imagefilledrectangle($mapimg, 300, 300, 350, 310, $hl_color);


Website to display the image:

<!-- File: index.php -->

<!-- Dynamically created image should appear here -->
<p class="caption">Description of image</p>

Edited by EvolutionFallen: n/a


Exactly, like I said. You would have one file say image.php generate the image and in the file you wanted to show the image you'd do

<img src="image.php?someparam=somevalue" />

Oh, great! For some reason I didn't think I could put the php file as the src value of the img tag. No idea why. Thanks Shawn!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.