Can Png Image Transparency Be Preserved When Using PHP'S Gdlib Imagecopyresampled

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

imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );

did it for me. Thanks ceejayoz.

note, the target image needs the alpha settings, not the source image.

Edit:
full replacement code. See also answers below and their comments. This is not guaranteed to be be perfect in any way, but did achieve my needs at the time.

$uploadTempFile = $myField[ 'tmp_name' ]
list( $uploadWidth, $uploadHeight, $uploadType )
= getimagesize( $uploadTempFile );

$srcImage = imagecreatefrompng( $uploadTempFile );

$targetImage = imagecreatetruecolor( 128, 128 );
imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );

imagecopyresampled( $targetImage, $srcImage,
0, 0,
0, 0,
128, 128,
$uploadWidth, $uploadHeight );

imagepng( $targetImage, 'out.png', 9 );

Keep image transparent to end

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.

How to keep transparent background on PNG when resizing with PHP?

I found another post with same question, according to that post following code can be used to preserve transparancy.

imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );

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

Please vote-up if it helps you. :)

PNG - preserving transparency

You need to call imagealphablending() on your destination image BEFORE you resample/resize the image:

//copy and resize old image to new image
imagealphablending($tmp_img, false);
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// ...

PHP - How can I copy the image as transparent png

You have to enable saving the alpha channel.

It can be done with imagesavealpha(), e.g.:

// As per the manual, alpha blending must be disabled
imagealphablending($dst_img, false);
imagesavealpha($dst_img, true);

How do I resize pngs with transparency in PHP?

From what I can tell, you need to set the blending mode to false, and the save alpha channel flag to true before you do the imagecolorallocatealpha()

<?php
/**
* https://stackoverflow.com/a/279310/470749
*
* @param resource $image
* @param int $newWidth
* @param int $newHeight
* @return resource
*/
public function getImageResized($image, int $newWidth, int $newHeight) {
$newImg = imagecreatetruecolor($newWidth, $newHeight);
imagealphablending($newImg, false);
imagesavealpha($newImg, true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $newWidth, $newHeight, $transparent);
$src_w = imagesx($image);
$src_h = imagesy($image);
imagecopyresampled($newImg, $image, 0, 0, 0, 0, $newWidth, $newHeight, $src_w, $src_h);
return $newImg;
}
?>

UPDATE : This code is working only on background transparent with opacity = 0. If your image have 0 < opacity < 100 it'll be black background.

GDlib PHP - png to gif returns background transparent to black

I think you need to call imagecolorallocate to get the color reference and pass that to imagecolortransparent:

$black = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($img, $black);

imagecopyresampled() introduces artifacts in transparent PNG

As I wrote in my comment in response to Søren Løvborgs answer it looks like it's simply [an issue with GD/imagecopyresampled()][1] that can not be easily avoided. GD Quality Issue with Transparent PNGs has the same issue.

It's possible to use Søren Løvborgs suggested workaround, just keep in mind that it may introduce noticable quality reduction due to resizing the original image twice.

I suggest using a photo editor to resize the watermark before overlaying instead. This isn't as flexible, but it will keep the image quality and not add any noise.



Related Topics



Leave a reply



Submit