View Full Version : GD Image Problems - Transparent Background

01-06-2007, 11:59 PM
I'm trying to create a PHP script to take a large image (2.1 Mb) (http://web.originallyorange.com/img/orn-ball.png) and cut it in half and resize it. I don't really know what I'm doing, this is the first time I've used these functions. The problem I'm having is that is has a drop shadow and in the process of keeping the background transparent it gets removed badly and I end up with this:


My code (from tutorials and php manual):

$width = 58;
$height = 142;
$canvas = imagecreatetruecolor($width,$height);
$piece = imagecreatefrompng('orn-ball.png');
$transp = imagecolorallocate($piece,0,0,0);
$newwidth = 500;
$newheight = 1000;
$cropLeft = 7;
$cropHeight = 7;
imagecopyresized($canvas, $piece, 0,0, $cropLeft, $cropHeight, $width, $height, $newwidth, $newheight);
if (imagepng($canvas,'test.png',90)) {
echo 'Image crop successful';
} else {
echo 'Image crop failed';

01-09-2007, 03:22 AM

01-09-2007, 03:45 AM
Looks fine to me (in FF).
Hint, IE doesn't know how to handle the alpha layer for the png images. Unfortunatly, the gif is your next best bet, but I'm not sure how the colour allocation will look with the 256 colours available.
Does that answer your question?

01-09-2007, 04:16 AM
I am using FF 2.0, but I don't know if this is a limitation of the resizing, but the edges aren't smooth and the drop shadow is removed. I understand that this is because of imagecreatetruecolor() creates a black base image, is there any way to get around this? or is it something else.

01-10-2007, 02:59 AM

02-23-2007, 12:11 AM
Here is a snapshot of the critical code...

// Create the destination image
$oDestinationImage = imagecreatetruecolor($width, $height);

// Create the palette
$nrcolors = imagecolorstotal($oSourceImage);
imagetruecolortopalette($oDestinationImage, true, $nrcolors);

// Create and set the transparent background color
$color = imagecolortransparent($oSourceImage);
$color = imagecolorallocate($oDestinationImage,
0xFF & ($color >> 0x10),
0xFF & ($color >> 0x8),
0xFF & $color);
$color = imagecolortransparent($oDestinationImage, $color);

// Fill the destination image with the transparent color
imagefill($oDestinationImage, 0,0, $color);