0
votes

First for all, I want to say that I read lot of topic about transparent in PHP GD. Eg:

php GD create a transparent png image

Can PNG image transparency be preserved when using PHP's GDlib imagecopyresampled?

Even comment in manual. To prove that I don't just read, but try to understand, below I paste link to completely false comment which I found. He was wrote something quite opposite than is say in manual about imagesavealpha (You have to unset alpha blending (imagealphablending($i'm, false)), to use it.)

http://pl.php.net/manual/en/function.imagepng.php#85748

http://php.net/manual/en/function.imagesavealpha.php

I want keep transparent until I save image. Look at the code (it is just a eg):

$base = imagecreatetruecolor(500,500);
  imagealphablending($base, false);
  imagesavealpha($base, true);
$image = base64_decode($image);
$image = imageCreateFromString($image);
imagecopyresampled($base,$image,0,0,0,0,500,500,500,500);
//to this step I have transparent
//now if I save image everything is okay
$avatar = imagescale($avatar,101,101);//lost transpratent
imagepng($base,$savePath,0);

I know I can scale Image in imagecopyresampled() action, and don’t need 2 operactions. But I wrote above it’s just schema.

Could someone write how to preserve transparency after each operation.

1
try changing the order. apply transparency after you create the image - raam86
So, I presume the image you are creating with imagecreatefromstring($image); is transparent too, because if its not you will be covering all transparent pixels of your $base and there will be no transparency even thought it supports it. - Havenard
@ raam86 You just suggest first create $image and after that $base ? As far as use it in imagecopyresampled it’s two separate variable so how might that even help ? :D @Havenard Yes, $image is PNG image with transparent regions. As I wronte above before comment block everything is okey (I saved image there and it’s okey). After scale action transparent is lost. - Sonny D

1 Answers

0
votes

This is a function that I wrote to resize images and keep their transparency a while ago:

function resize($image, $src_width, $src_height, $width, $height)
{
      if(!$image) return FALSE;

      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $src_width, $src_height);
      return $new_image;
}

I've had my struggles with transparency too, but at the end I came up with writing above code. I think this is going to work for you.