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:
Disable alpha blending
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 itDraw the rectangle
The result will be a block of transparency.
Function reference: imagealphablending
, imagecolorallocatealpha
, imagefilledrectangle
The "fade by 80%" thing will be trickier.
Disable alpha blending
For each
(x,y)
in the rectangle... (ie. two nestedfor
loops)Get the colour of that pixel (
imagecolorat
andimagecolorsforindex
should help)Multiply
$color['alpha']
by0.2
for an 80% fadeAllocate a new colour with the same
RGB
as the source, but with the newA
valueDraw 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:
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:
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:
Related Topics
What's the Best Way to Get the Fractional Part of a Float in PHP
Best Way to Check for Positive Integer (Php)
How to Get All Checkbox Variables Even If Not Checked from HTML to PHP
But These Conflict with Your Requirements or Minimum-Stability
Prevent Session Expired in PHP Session for Inactive User
Replace Add to Cart Button with a Read More Linked to Product Page on Shop Pages in Woocommerce 3
Bulk Rename Files in a Folder - PHP
Laravel Eloquent Sort by Relationship
Can't Insert Russian Text into MySQL Database
Troubleshooting "The Use Statement with Non-Compound Name ... Has No Effect"
Multidimensional Array PHP Implode
How to Remove All Numbers from String