Jump to content

PHP GD: Resizing a PNG image while keeping its background transparent


Some Poster55

Recommended Posts

Hi,

 

Basically, I'm looking for some help with resizing a png image that has a transparent background.

 

Here's what I'm currently working with:

<?php
$image1 = imagecreatefrompng("first_image.png");

$width = 59;
$height = 112;

$newwidth=70;
$newheight=112;

$image2=imagecreatefrompng("blank_background.png");

imagecopyresampled($image2,$image1,0,0,0,0,$newwidth,$newheight,$width,$height);

imagepng($image2,"result.png",9);

imagedestroy($image1);
imagedestroy($image2);
?>

 

The "first_image.png" file is the image with a transparent background which I need to resize. The "blank_background.png" is a completely transparent and blank image which I'm using to resize the first image using the imagecopyresampled() function.

 

However, upon running the script, I end up with my original image perfectly resized - but with a white background instead of a transparent background.

 

How can I resize the original image, but with retaining the transparent background?

 

Help would be greatly appreciated, thanks!

Archived

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

×
×
  • Create New...

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.