0

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.

Community
  • 1
  • 1
Sonny D
  • 897
  • 9
  • 29
  • try changing the order. apply transparency after you create the image – raam86 Sep 15 '13 at 23:31
  • 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 Sep 16 '13 at 00:03
  • @ 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 Sep 16 '13 at 12:33

1 Answers1

0

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.

MahanGM
  • 2,352
  • 5
  • 32
  • 45
  • @Havenard He is trying to use alpha blending functions which I found them useless so I cut the unnecessary stuff and put it here. As I said, I've had the same trouble with alpha blending so I came up with this code which is working for me. – MahanGM Sep 16 '13 at 00:08
  • @MahanGM Your code works. But I'm not pleased from solution. I don't like trick or hack. For me something is wrong if you need write you own function even if there is scale function in library, also everytime you need creating new image there. In my opinion library is rubbish :D I have choice (Imagick, ImageMagick). I feel compassion to people who do not have one (free hosting or something). Is just my grumble. Thank a lot, and have a nice day. – Sonny D Sep 16 '13 at 12:34
  • 1
    @SonnyD in case you haven't noticed his function is basically a shortcut to run two functions from the library, it has no tricks or hack and using them outside the function should have the same effect. – Havenard Sep 16 '13 at 13:56
  • @SonnyD As Havenard mentioned, there is no trick in here. I can't remember what was the reason that I put the other functions away and just tried this, and that's why I have no explanation for it right now because I may have been working on this around ten months ago. – MahanGM Sep 16 '13 at 15:16