transparency images

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
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

transparency images

Post by psychotomus »

I got 3 images and I want to merge them. None of the images should overlap except for the color I want transparent. I want the body first, bottom 2nd and top go 3rd. what should functions should I use? =)
Attachments
top 3rd
top 3rd
beard.png (233 Bytes) Viewed 300 times
bottom 2nd
bottom 2nd
blue armour.png (287 Bytes) Viewed 300 times
body first
body first
greenish-body.png (247 Bytes) Viewed 300 times
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: transparency images

Post by pickle »

I've never done anything like this before, but there are 2 possible methods I can think of.

1) Set that green as a transparent colour, then copy the entirety of one image onto the other. I'm not sure if that will overwrite legitimate pixel colours or not.

2) Create an image object in PHP. For each of those image, iterate through each pixel. If the pixel isn't that magic transparent colour, create a matching pixel at the same co-ordinates in the newly created object. After all images are iterated through, you should have a combination of all three in one image.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Re: transparency images

Post by psychotomus »

pickle wrote:I've never done anything like this before, but there are 2 possible methods I can think of.

1) Set that green as a transparent colour, then copy the entirety of one image onto the other. I'm not sure if that will overwrite legitimate pixel colours or not.

How do I set a pixel as transparent?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: transparency images

Post by pickle »

Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Re: transparency images

Post by psychotomus »

Thanks. How I get the rgb of my green pixel? ;']
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: transparency images

Post by pickle »

Sounds like you need to do some investigation into the GD functions available in PHP. Or bring the image into an image editor & click on the pixel with an eyedropper.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Re: transparency images

Post by psychotomus »

Ive done little work with GD in the past. Been a year since ive touched GD though.

Anyway, here's what I got:

Code: Select all

<?php
session_start();


//body is never empty
$im = imagecreatefrompng("images/chars/body" . str_replace(' ', '',$_SESSION['body'] . ".png"));
//set pixel color for transparent
$green = imagecolorallocate($im, 32, 156, 0);

// Make the background transparent
imagecolortransparent($im, $green);

//bottom
if(!empty($_SESSION['bot']))
{
	$newImage = imagecreatefrompng("images/chars/bot" . str_replace(' ', '',$_SESSION['bot']) . ".png");
	//set pixel color for transparent
	$green = imagecolorallocate($newImage, 32, 156, 0);

	// Make the background transparent
	imagecolortransparent($newImage, $green);
	
	imagecopymerge($newImage, $im, 0, 0, 0, 0, 48, 64, 0);
}

//top
if(!empty($_SESSION['top']))
{
	if(!empty($_SESSION['bot']))
	{
		$newImage = imagecreatefrompng("images/chars/top" . str_replace(' ', '',$_SESSION['top']) . ".png");
		//set pixel color for transparent
		$green = imagecolorallocate($newImage, 32, 156, 0);

		// Make the background transparent
		imagecolortransparent($newImage, $green);
	
		// Copy and merge
		imagecopymerge($newImage, $im, 0, 0, 0, 0, 48, 64, 0);
		
		// Content type
		header('Content-type: image/png');
		imagepng($newImage);
	}
	else
	{
		$newImager = imagecreatefrompng("images/chars/bot" . str_replace(' ', '',$_SESSION['bot']) . ".png");
		//set pixel color for transparent
		$green = imagecolorallocate($newImager, 32, 156, 0);

		// Make the background transparent
		imagecolortransparent($newImager, $green);
		
		imagecopymerge($newImager, $im, 0, 0, 0, 0, 48, 64, 0);
		
		// Content type
		header('Content-type: image/png');
		imagepng($newImager);
	}
}
?>
located at: http://www.phpengines.info/tactical/indexcreate.php It don't work =0
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Re: transparency images

Post by psychotomus »

Ive made some progress but not what i am looking for still.

http://phpengines.info/tactical/indexcreate.php

Code: Select all

<?php
session_start();

$rgb = "32, 156, 0";

//body is never empty
$im = imagecreatefrompng("images/chars/body" . str_replace(' ', '',$_SESSION['body'] . ".png"));


//set pixel color for transparent
$green = imagecolorallocate($im, 32, 156, 0);

// Make the background transparent
imagecolortransparent($im, $green);

//bottom
if(!empty($_SESSION['bot']))
{
	$newImage = imagecreatefrompng("images/chars/bot" . str_replace(' ', '',$_SESSION['bot']) . ".png");
	//set pixel color for transparent
	$green = imagecolorallocate($newImage, 32, 156, 0);

	// Make the background transparent
	imagecolortransparent($newImage, $green);
	
	imagecopymerge($im, $newImage, 0, 0, 0, 0, 48, 64, 75);
	
	//set pixel color for transparent
	$green = imagecolorallocate($im, 32, 156, 0);

	// Make the background transparent
	imagecolortransparent($im, $green);
}


//top
if(!empty($_SESSION['top']))
{
	$newImager = imagecreatefrompng("images/chars/top" . str_replace(' ', '',$_SESSION['top']) . ".png");
	//set pixel color for transparent
	$green = imagecolorallocate($newImager, 32, 156, 0);

	// Make the background transparent
	imagecolortransparent($newImager, $green);

	imagecopymerge($im, $newImager, 0, 0, 0, 0, 48, 64, 50);
}

// Content type
header('Content-type: image/png');
imagepng($im);

?>
Post Reply