Jump to content

Archived

This topic is now archived and is closed to further replies.

killerb

How do I keep the transparency in resized gifs? < RESOLVED >

Recommended Posts

. . . and why can't i find anything about it already!!?

I have this process:

[code]<?php
move_uploaded_file($_FILES[$name]['tmp_name'],$dest);
$image_p = imagecreatetruecolor($tile_oblique_width, $h);
$image = imagecreatefromgif($dest);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $tile_oblique_width, $h, $dims['0'], $dims['1']);
$image = imagegif($image_p,$dest);

?>
[/code]

Problem is that I get a nasty black background where it should be transparent.

If I just move_uploaded_file without resizing, it is transparent as I want.

Is there actually a way to do this? - There must be!

Cheers.

Share this post


Link to post
Share on other sites
[code]
<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?>
[/code]

Share this post


Link to post
Share on other sites
[quote="manual"]The transparent color is a property of the image, transparency is not a property of the color.[/quote]

You need to set it for the new image

[code]<?php
$ct = imagecolorat($image_p, 0,0);
imagecolortransparent($image_p, $ct);
imagegif($image_p, $dest);

imagedestroy($image_p);  // don't forget these lines
imagedestroy($image);
?>[/code]

Share this post


Link to post
Share on other sites
Well, yeah, jpeg is working fine, of course it doesn't give me transparency.

Adding imagecolortransparent works for .png, but not for gif. As well, the png is aliased to black matt. It is pretty clear that this is a realm of PHP I need to study, so I will do that sometime soon.

For the meantime, I must get on with it. This project is getting bigger and bigger by the day.

Share this post


Link to post
Share on other sites
If creating gif or png, try imagecreate instead of imagecreatetruecolor

Share this post


Link to post
Share on other sites

×

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.