GD copy an image...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Brokenhope
Forum Newbie
Posts: 6
Joined: Mon Dec 11, 2006 7:11 pm

GD copy an image...

Post by Brokenhope »

Alright, im working on a map generater code bit. It uses GD and positioning data to place objects and such on the map image. The map image is also started off by being applyed a background, from the background file. Which is looped multiple times throughout the destination image. But for now, I just need to create the map image thats 512 x 384 px, I need to place the 16x16 jpg image at position 0,0 on the map image, and I need to write the map .jpg as map.jpg on the server.

Ive been reading tutorials and keep getting stuck, heres what I have.

Code: Select all

$bg = 'tiles/littleroot/bg.jpg';

	// start gd
	header ("Content-type: image/png");
	$img = imagecreatetruecolor(512, 384) or die("Cannot Initialize new GD image stream.");
	imagecopy($img, $bg, 0, 0, 0, 0, 16, 16);
	imagejpeg($img, 'map.jpg');
	
	imagedestroy($img);
And I get the error: The image "http://localhost/map_prototype.php?do=add" cannot be displayed, because it contains errors.

Ive tried several things with no luck. I cant figure out what any of this is doing, Or even how to display my page as well as create this image, as this is ignoring the HTML completely.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I suggest sending the image/xyz header as late as possible. Errors and warnings are text messages, if you keep the content-type to text/html (default) you are able to see them in the browser.
In your case that would be

Code: Select all

$bg = 'tiles/littleroot/bg.jpg';

// start gd

$img = imagecreatetruecolor(512, 384) or die("Cannot Initialize new GD image stream.");
imagecopy($img, $bg, 0, 0, 0, 0, 16, 16);

if (! headers_sent()) {
	header ("Content-type: image/png");
	imagejpeg($img, 'map.jpg');
}
imagecopy takes two gd resources as parameter, not one resource and one path to some file. See http://de2.php.net/imagecreatefromjpeg
Why do you send image/png as content-type but then call imagejpeg?
Why do you send the content-type header in the first place? If you call imagejpeg with two parameters the image will be saved to a file not send to the browser.
Post Reply