Jump to content

Transparent Image


The Little Guy

Recommended Posts

OK... I have this function, it looks like this:

 

<?php
function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality){
$details = getimagesize("$imageDirectory/$imageName") or die('<div class="container">
	<div class="content">
		Please only upload images.
	</div>
</div>');
$type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']);
eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");');
$thumbHeight = $details[1] * ($thumbWidth / $details[0]);
$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecolortransparent($thumbImg);
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $details[0], $details[1]);
eval('image'.$type.'($thumbImg, "$thumbDirectory/$imageName"'.(($type=='jpeg')?', $quality':'').');');
imagedestroy($srcImg);
imagedestroy($thumbImg);
}
?>

 

If I use this code:

imagecolortransparent($thumbImg);

To make the transparent stuff transparent, it doesn't work, but if I change it to this:

imagecolortransparent($thumbImg,imagecolorallocate(0,0,0));

it will make the black transparent, but... then I get a weird image, where not only the background is transparent, but the black parts in the foreground of the image are transparent as well.

 

How can I make just the background transparent?

Link to comment
https://forums.phpfreaks.com/topic/70192-transparent-image/
Share on other sites

Oh yeah.... Here is my new code:

 

<?php
$location = "users/ryan/1190402980image.gif";
$details = getimagesize($location);
$thumbWidth = 50;
$type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']);
$thumbHeight = $details[1] * ($thumbWidth / $details[0]);
$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);


$background_color = imagecolorallocate($thumbImg, 0, 0, 0);
$tans = imagecolortransparent($thumbImg,$background_color);
$image = imagecreatefromgif($location);
imagepalettecopy($image, $thumbImg);


imagegif($thumbImg,NULL);
header('Content-Type: image/gif');
imagegif($thumbImg);
imagedestroy($thumbImg);
?>

Link to comment
https://forums.phpfreaks.com/topic/70192-transparent-image/#findComment-352683
Share on other sites

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.