Png Transparency with PHP

Create a transparent png file using PHP

To 1)
imagecreatefrompng('test.png') tries to open the file test.png which then can be edited with GD functions.

To 2)
To enable saving of the alpha channel imagesavealpha($img, true); is used.
The following code creates a 200x200px sized transparent image by enabling alpha saving and filling it with transparency.

<?php
$img = imagecreatetruecolor(200, 200);
imagesavealpha($img, true);
$color = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $color);
imagepng($img, 'test.png');

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.

PNG file is NOT keeping transparency?

You need to enabled alpha blending and save alpha. I found this after a 10 sec google search:
http://www.php.net/manual/en/function.imagecreatefrompng.php#43024

How can PNG image transparency be preserved when using PHP code for Thumbnail Generation

I think your problem is in the line below:

$thumbnail_gd_image = imagecreatetruecolor($real_width, $real_height);

According to http://www.php.net/manual/en/function.imagecreatetruecolor.php

imagecreatetruecolor() returns an image identifier representing a black image of the specified size.

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);

php GD create a transparent png image

Set imagealphablending($image,true); on each new layer.

Try this:

<?php
$image = imagecreatetruecolor(485, 500);
imagealphablending($image, false);
$col=imagecolorallocatealpha($image,255,255,255,127);
imagefilledrectangle($image,0,0,485, 500,$col);
imagealphablending($image,true);

/* add door glass */
$img_doorGlass = imagecreatefrompng("glass/$doorStyle/$doorGlass.png");
imagecopyresampled($image, $img_doorGlass, 106, 15, 0, 0, 185, 450, 185, 450);
imagealphablending($image,true);

/* add door */
$img_doorStyle = imagecreatefrompng("door/$doorStyle/$doorStyle"."_"."$doorColor.png");
imagecopyresampled($image, $img_doorStyle, 106, 15, 0, 0, 185, 450, 185, 450);
imagealphablending($image,true);

$fn = md5(microtime()."door_builder").".png";

imagealphablending($image,false);
imagesavealpha($image,true);
if(imagepng($image, "user_doors/$fn", 1)){
echo "user_doors/$fn";
}
imagedestroy($image);

?>

PHP make transparent png with text

You should send the correct header with your data:

header('Content-Type: image/png');

Or, if you're trying from a console, and you wish to direct the output to a PNG file you should either run it as php <yourscript>.php > filename.png or change imagepng($im); to imagepng($im, 'filename.png'); What your script is currently doing is outputting the PNG to the output, and you see the raw PNG data.

Crop a png image with PHP, remove empty transparency

If you read and follow the php php-gd documentation, you'll find a function called imagecropauto which does exactly what you want, it crops the alpha channel of the image.

Crop an PNG image with alpha channel

$im = imagecreatefrompng("./star-with-alpha.png");
$cropped = imagecropauto($im, IMG_CROP_DEFAULT);

if ($cropped !== false) { // in case a new image resource was returned
imagedestroy($im); // we destroy the original image
$im = $cropped; // and assign the cropped image to $im
}

imagepng($im, "./star-with-alpha-crop.png");
imagedestroy($im);

You can try it dirrectly to a php page using this code:

<body>

<img src="star-with-alpha.png">

<?php

$im = imagecreatefrompng("./star-with-alpha.png");
$cropped = imagecropauto($im, IMG_CROP_DEFAULT);

if ($cropped !== false) { // in case a new image resource was returned
imagedestroy($im); // we destroy the original image
$im = $cropped; // and assign the cropped image to $im
}

imagepng($im, "./star-with-alpha-crop.png");
imagedestroy($im);

?>

<img src="star-with-alpha-crop.png">

</body>

The result

http://zikro.gr/dbg/php/crop-png/

Cropped image demo

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. :)



Related Topics



Leave a reply



Submit