0

I have a small class which handles image manipulation.

I use following to resize a image

$this->image = imagecreatefrompng($filename);
....
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
...
$this->image = $new_image; 
imagepng($this->image,$filename)) { return true; }

But the resized image is not preserving transparency, instead black is comming, how can i preserve the transparency.

Update

After, using @Manuel's code, black portion has decreased, but still black background are still present. The source image and the resulting image are

Source & Sub corresponding

main http://www.freeimagehosting.net/newuploads/820a0.png sub http://www.freeimagehosting.net/newuploads/30526.png

mrN
  • 3,734
  • 15
  • 58
  • 82
  • you should check [this post](http://stackoverflow.com/questions/313070/png-transparency-with-php) –  Oct 18 '11 at 11:56

2 Answers2

3

The newest comment, posted on the 8th of May, on the manual page for imagecopyresampled, tells you how to do this.

imagecolortransparent($new_image, imagecolorallocatealpha($new_image, 0, 0, 0, 127));
imagealphablending($new_image, false);
imagesavealpha($new_image, true);

Put that right after creating $new_image.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

add this before the imagecopyresampled(...)

// preserve transparency
imagecolortransparent($new_image , imagecolorallocatealpha($new_image , 0, 0, 0, 127));
imagealphablending($new_image , false);
imagesavealpha($new_image , true);
Manuel van Rijn
  • 10,170
  • 1
  • 29
  • 52