Php/Gd - Transparent Background

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

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

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

GD Image Library to create transparent GIF after manipulation

The solution proposed by @astrangeloop takes about 2.5 seconds to render on my laptop. As OP has indicated that speed is a factor I don't think this solution works.

I propose:

  • Manually optimise the assets on the server
  • convert those assets to true color when opening in PHP
  • render as GIF

By way of example, I optimised templateNoBG.png using tinypng.com, reducing the file size from 1.02MiB to 227KiB:

Sample Image

Since the image is now 8-bit (256 colour) we need to make it true colour when opening so that the merged images retain their colours:

$destination = imagecreatefrompng($style);
imagepalettetotruecolor($destination);

When the image is output using imagegif it is again reduced to 8-bit, with a much better palette selection, albeit with apparent jagged edges due to GIF only supporting binary transparency:

Sample Image

The resulting image is only 395KiB and generates in about 120ms on my laptop.

This is probably about the best trade-off between speed, quality and size that can be expected.

PHP-GD Transparency of watermark PNG not correctly merged with JPEG

I usually create a new true colour resource and copy everything into it. This ensures GD doesn't get too quirky. It's a little bit more resource intensive, but should be negligible for most cases.

Below is your code modified to create a new image, copy in the jpeg, and then overlay the partially transparent watermark:

<?php
$image = imagecreatefromjpeg('http://www.sideshowtoy.com/wp-content/uploads/2016/03/dc-comics-batman-v-superman-woner-woman-sixth-scale-hot-toys-feature-902687.jpg');
$img_w = imagesx($image);
$img_h = imagesy($image);
$new = imagecreatetruecolor($img_w, $img_h);
imagecopy($new, $image, 0, 0, 0, 0, $img_w, $img_h);
imagedestroy($image);

$watermark = imagecreatefrompng('https://d5odq6jbm6umf.cloudfront.net/assets/img/video-play-button-transparent.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$dest_x = ($img_w - $watermark_width) / 2;
$dest_y = ($img_h - $watermark_height) / 2;
imagecopy($new, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);

header('Content-type: image/png');
imagejpeg($new);
imagedestroy($new);
imagedestroy($watermark);

Result:

Wonder Woman! Na na na na na!



Related Topics



Leave a reply



Submit