...

View Full Version : GD Image Problems - Transparent Background



Big-Pat
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:

http://web.originallyorange.com/img/test.png

My code (from tutorials and php manual):


<?php
$width = 58;
$height = 142;
$canvas = imagecreatetruecolor($width,$height);
$piece = imagecreatefrompng('orn-ball.png');
$transp = imagecolorallocate($piece,0,0,0);
imagecolortransparent($canvas,$transp);
$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';
}
imagedestroy($canvas);
imagedestroy($piece);
?>

Big-Pat
01-09-2007, 03:22 AM
bumper

Fou-Lu
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?

Big-Pat
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.

Big-Pat
01-10-2007, 02:59 AM
B-Bump

Gater
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);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum