Jump to content

Questions about imagecopy()


pinacoladaxb

Recommended Posts

I made a topic earlier today called "Generate an image in php?". I thought my problems were over, but I have 2 questions.

1. I have an image that I want to paste in another image (using imagecopy() of course.) Here's what the line of code looks like:

imagecopy($im,$B1,$B1_x*20,$B1_y*20,0,0,40,40);

But it doesn't work for some reason. The image will not be inserted. When I remove the "*20"s from the line, it works fine. But then it is pasted in the wrong location. Here's the link to show you:

http://flufferfluff.com/games/battles/myimage.php?name=hgi&level=jhf

2. As you can see, there is white around the building. How do I get rid of it?

Link to comment
https://forums.phpfreaks.com/topic/115669-questions-about-imagecopy/
Share on other sites

Check your params as you go

imagecopy  ( resource $dst_im  , resource $src_im  , int $dst_x  , int $dst_y  , int $src_x  , int $src_y  , int $src_w  , int $src_h  )

 

Is $im the orignal image to be copied?

 

is B1 (A constant???) The right destination resource file???

 

is $BI_X*20 and $B1_Y*20 the proper destination coordinates (not its coordinates not size)

 

the 0 and 0 are the original image meaning start in the bottom left corner

 

40,40 means go 40 pixels right 40 pixels up from there

 

 

DO u see the problem???

 

Check your params as you go

imagecopy  ( resource $dst_im  , resource $src_im  , int $dst_x  , int $dst_y  , int $src_x  , int $src_y  , int $src_w  , int $src_h  )

 

Is $im the orignal image to be copied?

 

is B1 (A constant???) The right destination resource file???

 

is $BI_X*20 and $B1_Y*20 the proper destination coordinates (not its coordinates not size)

 

the 0 and 0 are the original image meaning start in the bottom left corner

 

40,40 means go 40 pixels right 40 pixels up from there

 

 

DO u see the problem???

 

I don't see the problem, but I think you have some things mixed up. First of all, $im is the destination and $B1 is the image to be pasted (the building). Also, doesn't it start in the upper left corner? If I'm the one mixing things up here, tell me.

I didn't see any missing dollar signs. You can see what it outputs by clicking on the second link I provided in the first post.

It may help if I just give you the php code so you can maybe see the problem without having to guess based on what I tell you. It took me long to reply because I was adding loads of comments:

<?php
//This code is used to generate a screenshot of user-created levels made in my game. They are stored on a database.
//Scroll down until you see "~~~~~PROBLEMS START HERE~~~~~" unless you want to read all of the code. I commented all of it.

//Defines which array to pull from the database.
$name = $_GET['name'];//Username
$level = $_GET['level'];//Level

//Connecting to the database:
$service = "localhost";
$username = "pauliuko";
$password = "sdasdreedf";
$database = "pauliuko_games";
$table = "battles_uploads";
mysql_connect($service, $username, $password);
@mysql_select_db($database) or die( "8Error. Could not connect to database.");

//Getting the row
$result = mysql_query("SELECT * FROM $table WHERE name='$name' and level='$level'") or die("8Error in getting data from database.");

//Gets the data that will be used to construct the image. It is a 592-digit number. Digits 1-576 are for the terrain of the level and the rest are for building placement.
$data = mysql_result($result, 0, data);

//Creates the image that I am pasting things into.
$im = imagecreate(480,480);

//Loads all images that will be used.
$image2 = imagecreatefrompng('images/vert.png');//black vertical line
$image3 = imagecreatefrompng('images/horiz.png');//black horizontal line
$B1 = imagecreatefrompng('images/B1.png');//blue square building
$F1 = imagecreatefrompng('images/F1.png');//blue rectangular building
$B2 = imagecreatefrompng('images/B2.png');//yellow square building
$F2 = imagecreatefrompng('images/F2.png');//yellow rectangular building

//This code generates the terrain.
$loopindex_x=0;
$loopindex_y=0;
while($loopindex_y<24){
while($loopindex_x<24){

	//The following is used to generate black lines between different terrain types.
	$prev_x = $data[(($loopindex_x+1)+($loopindex_y*24))-2];//The space to the left of the current location
	$prev_y = $data[(($loopindex_x+1)+(($loopindex_y-1)*24))-1];//The space above the current location
	$cur_data = $data[(($loopindex_x+1)+($loopindex_y*24))-1];//The current location

	//Loads the current terrain type and pastes it
	$image = imagecreatefrompng('images/'.$cur_data.'.png');
	imagecopy($im,$image,$loopindex_x*20,$loopindex_y*20,0,0,20,20);

	//Adds black lines where needed
	if(($prev_x !== $cur_data)||($loopindex_x == 0)){
		imagecopy($im,$image2,$loopindex_x*20,$loopindex_y*20,0,0,1,20);
		}
	if(($prev_y !== $cur_data)||($loopindex_y == 0)){
		imagecopy($im,$image3,$loopindex_x*20,$loopindex_y*20,0,0,20,1);
	}

	//Adds a border to the image
	if($loopindex_x == 23){
		imagecopy($im,$image2,$loopindex_x*20+19,$loopindex_y*20,0,0,20,20);
	}
	if($loopindex_y == 23){
		imagecopy($im,$image3,$loopindex_x*20,$loopindex_y*20+19,0,0,20,20);
	}

	$loopindex_x++;
}
$loopindex_x=0;
$loopindex_y++;
}

//Gets the coordinates of the buildings (in tiles, not pixels. They need to be multiplied my 20 to get pixels.)
$B1_x = $data['577'].$data['578'];
$B1_y = $data['579'].$data['580'];
$F1_x = $data['581'].$data['582'];
$F1_y = $data['583'].$data['584'];
$B2_x = $data['585'].$data['586'];
$B2_y = $data['587'].$data['588'];
$F2_x = $data['589'].$data['590'];
$F2_y = $data['591'].$data['592'];

//~~~~~PRPBLEMS START HERE~~~~~
//This is how I print the image of the building. I have only done one so far because it will not work.
imagecopy($im,$B1,$B1_x*20,$B1_y*20,0,0,40,40);

//This is just to finalize it so that it can be used in my game.
header("content-type: image/png");
imagepng($im);
imagedestroy($im);
imagedestroy($image);
imagedestroy($image2);
imagedestroy($image3);
imagedestroy($B1);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.