Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Transparent PNG image in truecolor - impossible?

Status
Not open for further replies.

LittleHavoc

IS-IT--Management
Nov 28, 2001
56
GB
Hello all,
found a major problem that is really bugging me.

I can create an image and displaying it as a PNG as follows
$width=160;
$height=120;
$image=ImageCreate($width, $height);
$transparent = ImageColorAllocate ($image, 0xFF, 0x00, 0x88);
imagefilledrectangle($image, 0, 0, $width, $height, $transparent);
imagecolortransparent($image,$transparent);
imagepng($image);

Which works great but because its paletted there are only 256 colours and I will be using it to resize true colour images. Keeping the aspect-ratio requires the image be padded out.
SO I use $image=ImageCreateTrueColour($width, $height); instead, giving a great range of colors.
BUT now the image is not transparent.

Can anyone supply some missing function or code to create a true color PNG image with a transparent background.

 
Can't see why you want to pad an image with another to keep aspect ratiio when you can do it like this:

// desired width of the thumbnail
$picsize = 100;

// grabs the height and width
$new_w = imagesx($src_img);
$new_h = imagesy($src_img);

// calculates aspect ratio
$aspect_ratio = $new_h / $new_w;

// sets new size
$new_w = $picsize;
$new_h = abs($new_w * $aspect_ratio);

// creates new image of that size
$dst_img = ImageCreate($new_w,$new_h); ***************************************
Party on, dudes!
[cannon]
 
Because all images on the site need to be the same size so there is an equal number shown on each row of the gallery.

The images being supplied are from different sources, some from people who just have a digital camera but no real knowledge of how to use it correctly. Resizing portrait and landscape images to create thumbnails has easily been achieved but padding the edges out makes all images the same size, and neatly displayed.

Please see examples at
p.s.
$dimension_limits['width']=100;
$dimension_limits['height']=75;

$imageInfo = getimagesize($_FILES['form_image']['tmp_name']);
$width = $imageInfo[0];
$height = $imageInfo[1];

$offset_X = 0;
$offset_Y = 0;
$scaling_width = $width / $dimension_limits['width'];
$scaling_height = $height / $dimension_limits['height'];

// use the maximum scaling value so both dimensions are within limits and keep aspect ratio
if ($scaling_width > $scaling_height)
{
$image_width = $width / $scaling_width;
$image_height = $height / $scaling_width;
$offset_Y = floor( ($dimension_limits['height'] - $image_height) / 2);
}
else
{
$image_width = $width / $scaling_height;
$image_height = $height / $scaling_height;
$offset_X = floor( ($dimension_limits['width'] - $image_width) / 2);
}
$image_width = floor($image_width);
$image_height = floor($image_height);
$db_image=ImageCreateTrueColor($dimension_limits['width'], $dimension_limits['height']);
///// make this true color image transparent here
imagecopyresized($db_image, $im, $offset_X, $offset_Y, 0, 0, $image_width, $image_height, $width, $height);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top