/    Sign up×
Community /Pin to ProfileBookmark

Image resamples and transparency

Hi,

Just wondering if anyone can help me out here.
I have a function which I use to resize uploaded images, script works fine for the most part.

However when I use this with a transparent gif the image isnt sampled properly and the transparency turns black.

I have been messing about using the various image functions in php but im not gettign anywhere. Could anyone suggest what needs to be added to the funtion to handle transparent gifs.

Thanks

[code=php]
function resize($file,$path,$ext,$size)
{

list($owidth, $oheight) = getimagesize($_FILES[$file][‘tmp_name’]);

$nwidth = $size;
$nheight = $nwidth / $owidth * $oheight;
$resized = imagecreatetruecolor($nwidth, $nheight);

if($ext == ‘png’)
{$original = imagecreatefrompng($_FILES[$file][‘tmp_name’]);}

if($ext == ‘jpg’ || $ext == ‘jpeg’)
{$original = imagecreatefromjpeg($_FILES[$file][‘tmp_name’]);}

if($ext == ‘gif’)
{$original = imagecreatefromgif($_FILES[$file][‘tmp_name’]);}

imagecopyresampled($resized, $original, 0, 0, 0, 0, $nwidth, $nheight, $owidth, $oheight);

if($ext == ‘png’){
imagepng($resized,$path);}
if($ext == ‘jpg’ || $ext == ‘jpeg’)
{imagejpeg($resized,$path);}
if($ext == ‘gif’)
{imagegif($resized,$path);}

}
[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@TJ111Apr 08.2008 — Does this happen with .png images or only .gif?
Copy linkTweet thisAlerts:
@SoreheadauthorApr 08.2008 — After checking, yes it does.

However in this instance I dont need to worry about pngs.

Thanks
Copy linkTweet thisAlerts:
@TJ111Apr 08.2008 — Try adding the following above your imagecopyresampled line. It's kind of a hack, but it hopefully should work (found on the comments in the documentation). Also you must output it as a png.
[code=php]
imagecolortransparent($resized, imagecolorallocate($resized, 0, 0, 0));
imagealphablending($resized, false);
imagesavealpha($resized, true);
[/code]


Edit: I tested it and it didn't work for me, in fact the output image had worse quality then the original.
Copy linkTweet thisAlerts:
@SoreheadauthorApr 08.2008 — Thanks for your effort mate. I'll need to do a bit more digging it seems.
Copy linkTweet thisAlerts:
@TJ111Apr 08.2008 — No problem. I think the problem has to do with this:

[B]Note:[/B] There is a problem due to palette image limitations (255+1 colors). Resampling or filtering an image commonly needs more colors than 255, a kind of approximation is used to calculate the new resampled pixel and its color. With a palette image we try to allocate a new color, if that failed, we choose the closest (in theory) computed color. This is not always the closest visual color. That may produce a weird result, like blank (or visually blank) images. To skip this problem, please use a truecolor image as a destination image, such as one created by imagecreatetruecolor(). [/quote]
You can always use imagecopyresized for images that need to remain transparent, although it will have a lower quality. Let me know if you find a fix for this, I am also interested.
×

Success!

Help @Sorehead spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...