PHP Gd Create a Transparent Png Image

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/GD - transparent background

imagecolortransparent is probably not what you want here if you're merging images, as single-colour transparency is nasty.

Instead, try it with a transparent fill mask like so:

<?php
$image = imagecreatetruecolor(100, 100);

// Transparent Background
imagealphablending($image, false);
$transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparency);
imagesavealpha($image, true);

// Drawing over
$black = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 25, 25, 75, 75, $black);

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

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

make transparent png image by gd

Try this

<?php
//Canvas size 100x50
$image = imagecreatetruecolor(100, 50);
imagealphablending($image, false);
//Create alpha channel for transparent layer
$col=imagecolorallocatealpha($image,255,255,255,127);
//Create overlapping 100x50 transparent layer
imagefilledrectangle($image,0,0,100, 50,$col);
//Continue to keep layers transparent
imagealphablending($image,true);
//Insert the text
imagefttext($image,10,0,10,20,0,'octin.ttf','test sting');
//Keep trnsparent when saving
imagesavealpha($image,true);

//Save & output
if(imagepng($image, "test.png", 1)){
header("Content-Type: image/png");
readfile('test.png');
}
imagedestroy($image);
?>

Output 100x50px [test.png]

Output

whoops i forgot the r... my bad

PHP GD - transparent PNG black background

It should be something along the lines of:

switch ($source_type)
{
case IMAGETYPE_PNG:

$background = imagecolorallocate($source, 0, 0, 0);
// remove the black
imagecolortransparent($source, $background);

// turn off alpha blending
imagealphablending($source, false);

imagesavealpha($source, true);

break;
}

There is a similar question here

imagepng() and transparency in GD library with PHP

Here is an example of the imagecolortransparent function (if it helps):

<?php
// Create a 55x30 image
$im = imagecreatetruecolor(55, 30);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);

// Make the background transparent
imagecolortransparent($im, $black);

// Draw a red rectangle
imagefilledrectangle($im, 4, 4, 50, 25, $red);

// Save the image
imagepng($im, './imagecolortransparent.png');
imagedestroy($im);
?>

PHP GD turn background into transparent or alpha

The straight transparent is easy:

  1. Disable alpha blending

  2. Allocate a colour with alpha - personally I use (255,0,255,127) because magenta is the "standard" transparent colour when working with game sprites, so I just sort of stuck with it

  3. Draw the rectangle

The result will be a block of transparency.

Function reference: imagealphablending, imagecolorallocatealpha, imagefilledrectangle


The "fade by 80%" thing will be trickier.

  1. Disable alpha blending

  2. For each (x,y) in the rectangle... (ie. two nested for loops)

    1. Get the colour of that pixel (imagecolorat and imagecolorsforindex should help)

    2. Multiply $color['alpha'] by 0.2 for an 80% fade

    3. Allocate a new colour with the same RGB as the source, but with the new A value

    4. Draw the pixel

The result will be a block of 80%-faded-to-transparent pixels.

Function reference: imagealphablending, imagecolorat, imagecolorsforindex, imagecolorallocatealpha, imagesetpixel

PHP - change background color to transparent

I have solved this issue:

<? 
$picture = imagecreatefrompng("../images/chart.png");

$img_w = imagesx($picture);
$img_h = imagesy($picture);

$newPicture = imagecreatetruecolor( $img_w, $img_h );
imagesavealpha( $newPicture, true );
$rgb = imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 );
imagefill( $newPicture, 0, 0, $rgb );

$color = imagecolorat( $picture, $img_w-1, 1);

for( $x = 0; $x < $img_w; $x++ ) {
for( $y = 0; $y < $img_h; $y++ ) {
$c = imagecolorat( $picture, $x, $y );
if($color!=$c){
imagesetpixel( $newPicture, $x, $y, $c);
}
}
}

imagepng($newPicture, '../images/chart2.png');
imagedestroy($newPicture);
imagedestroy($picture);

I hope it helps someone else.



Related Topics



Leave a reply



Submit