Jump to content

Imagecopyresampled and Transparency


corbin

Recommended Posts

So some of y'all may have seen my thumbnail generator thing the other day in the beta testing section...  I lost interest a while back after  I failed at the transparency aspect, but today I found a new strand of motivation.  Anyways what it does (non essential steps not included):

-Make a new true color image with the desired width and height (referred to as img1 from now on)

-Create an image of the image desired to be a thumb (img2)

-Make img1 transparent using the first pixel in it

-Copy img2 onto img2

This creates the effect of having transparency around the thumbnail....  I only have one problem.  Img2 inherits the transparency setting from img1, and this makes img2 have transparency if it has the color from the first pixel of img1.

 

My first thought was, "OK I'll just copy the transparent one over img1, restraining img1's size and making img2 increase the image size."  Sounded good, until I tried to do it and couldn't get it to work:(.  Anyway, my question is what is the best method to merge two images and retain transparency without making the image that was merged go transparent?

 

I don't think my code is really necessary to solve the problem, but of course someone will think it is, I'll attach it.

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/38706-imagecopyresampled-and-transparency/
Share on other sites

Set your transparency to a color or set of colors that don't exist in the image you want to make transparent. You do that by reducing the colors on your image, if it's a true color image, than cycle through the colors until you find one that doesn't exist, then use that color to set your transparency.

Hmmm, so I came up with

<?php
function FindColor() {
$r = 0;
$g = 0;
$b = 0;
$match = 0;
$i = 0;
while($match < 1 && $i <= 16581375) {
//while($match < 1 && $i <= 375) {
if(imagecolorexact($image, $r, $g, $b) < 0) {
	$return = array($r, $g, $b);
	$match = 1;
}
else {
	if($r == 255 && $g == 0 && $b == 0) {
		$rchecked = true;
		$r = 0;
		$g = 1;
	}
	if($bchecked != true && $rchecked == true && $gchecked == true && $b == 0) {
		$r = 0;
		$g = 0;
		$b = 1;
	}
	/*
	if($g == 255 && $b == 0) {
		$gchecked = true;
		$r = 0;
		$g = 0;
	}
	*/
	if($rchecked != true) {
		$r++;
		if($r == 256) {
			$rchecked = true;
		}
	}
	elseif($rchecked == true && $gchecked != true) {
		$r++;
		if($r == 256) {
			$g++;
			$r = 0;
			if($g == 256) {
				$g = 0;
				$gchecked = true;
			}
		}
	}
	elseif($rchecked == true && $gchecked == true && $bchecked != true) {
		$r++;
		if($r == 256) {
			$r = 0;
			$g++;
		}
		if($g == 256) {
			$g = 0;
			$b++;
		}
		if($b == 256) {
			$bchecked = true;
		}
	}
	elseif($bchecked == true && $match < 1) {
		$return = false;
	}
	else {
		$return = false;
	}
}
$i++;
}
return $return;
}
$image = imagecreatefromjpeg("white.jpg");
global $image;
if(!$image) { 
echo "die bitch";
}
$hmm = FindColor($image);
//$hmm = imagecolorexact($image, 255, 255, 255);
print_r($hmm);
//echo $hmm;
?>

But I didn't read the note that imagecolorexact doesn't work with jpg...  Now I don't know what to do lol...  I'll be readin through the GD functions more and messin around with em, but I'm open to suggestions.

You don't have to do all that. Can you zip up a a full image and also a thumbnail of that image demonstrating what you want to do. If I see the resulting thumbnail image, I can show you how to do what you want to do. Just remember JPG/JPEG doesn't support transparency, only PNG and GIF support layering alpha channel transparencies.

Files are attached... Desired.gif is what I'm trying to achieve.  The file in /thumbs/ is what's being created by thumb.php.  Index.php simply has a img tag to thumb.php for the image.  In images/ is the image the thumb is being created from....

 

I'm pretty sure what I'm trying to do is easy, but I'm over looking something lol....  I finally decide to look into the GD library and simple stuff stops me >.< lol

 

By the way, thanks a lot for helping!

 

[attachment deleted by admin]

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.