imagepng() and transparency in GD library with PHP - php

When using the function imagepng() in PHP, how can I make sure the images that I save are saved with a transparent background?

Simply do this:
imagealphablending($img, false);
imagesavealpha($img, true);
Before outputting. Make sure that all source files (if you used any) are set to PNG 32-bit with transparency - if not the output may differ with black background or transparency does not comply.

Here is the example
$newimage = imagecreatetruecolor($dst_w, $dst_h);
imagealphablending($newimage, false);
imagesavealpha($newimage, true);
$transparentindex = imagecolorallocatealpha($newimage, 255, 255, 255, 127);
imagefill($newimage, 0, 0, $transparentindex);

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

There's a function called imagecolortransparent that allows you to set which color is made transparent. I don't know if this answers your question.

Related

How to make an Image to show only black text

I am trying to do the following:
I Have a picture I want to make that only text which is written in black remains visible and rest of every colour gets transparent.
I tried to do this using PHP imagecolortransparent function but I am not able to figure out how to make it work.
Any help will be great.
Thanks In advance
Updated Answer
There must be an easier way, but until I think of it, this should work:
// Read original image
$start = imagecreatefrompng("input.png");
// Make true colour image same size
$im = imagecreatetruecolor(imagesx($start),imagesy($start));
$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
$red=imagecolorallocate($im,255,0,0);
imagesavealpha($im, TRUE);
// Fill the new image with transparency
imagefill($im, 0, 0, $transparent);
// Copy only black pixels across from original to new
for($x=0;$x<imagesx($im);$x++){
for($y=0;$y<imagesy($im);$y++){
$rgb = imagecolorat($start,$x,$y);
if($rgb==0){
imagesetpixel($im,$x,$y,$black);
}
}
}
imagepng($im,"result.png");
Original Answer
I would do this:
// Read original image
$start = imagecreatefrompng("input.png");
// Create a proper truecolour image the same size that can support transparency
$im = imagecreatetruecolor(imagesx($start),imagesy($start));
$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $transparent);
imagesavealpha($im, TRUE);
$black = imagecolorallocate($im, 0, 0, 0);
$text="This is some quite black text";
imagestring($im,5,0,75,$text,$black);
imagepng($im,"result.png");
input.png
result.png

Changing signature color to white in png picture in php

I am having user signature in png format. I want to change the color of the signature from black to white. I am using the following code
$image = imagecreatefrompng('signature.png');
imagefilter($image, IMG_FILTER_COLORIZE, 255, 255, 255); // make it blue!
imagepng($image, 'signature.png');
But the background changed to black, I think because of using imagecreatefrompng how can i only change the color of signature.
i used this code it worked ....
$img = imagecreatefrompng('signature.png');
$background = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $background);
imagealphablending($img, false);
imagesavealpha($img, true);
imagefilter($img, IMG_FILTER_COLORIZE, 255, 255, 255); // make it blue!
imagepng($img, 'signature.png')

I have code that works, with picture uploaded from gimp. But image created with gd not

My problem is this, I want to create image thats combination of 3 image, first is yellow background, then I use PHP and GD to write some text on it, text is black, so I made it transparent in next step, then I want to put that picture over background picture, so text has texture on it. It works fine, if I upload PNG from my computer created in Gimp, but picture created with gd has transparency on it but result is again yellow background with black letters.
how it should be
good result
what I get now from code:
enter link description here
how it gets now with image created from gd
<?php
header('Content-Type: image/png');
$title = "PULEŽANI";
$im = imagecreatetruecolor(1200, 320);
//$im = imagecreatetruecolor(1200, 320);
$white = imagecolorallocate($im, 255, 255, 255);
$crna = imagecolorallocate($im, 0, 0, 0);
$black = imagecolorallocatealpha($im, 255, 255, 255, 127);
$yellow = imagecolorallocate($im, 251, 189, 8);
// kreiram kvadrat sa žutom pozadinom
imagefill($im, 0, 0, $yellow);
$font = "/AlrightSans-Ultra-v3.ttf";
//dodajem text na žutu pozadinu
imagettftext($im, 122, 0, 40, 160, $crna, $font, $title);
//kreiram sliku crni tekst na žutoj pozadini
imagepng($im, 'sl.png');
imagedestroy($im);
//ovaj dio bi trebao napraviti da crna slova postanu prozirna
$image = imagecreatefrompng('sl.png');
$odabirprozirne = imagecolorallocatealpha($image, 0, 0, 0,127);
imagealphablending($image, true);
imagecolortransparent($image,$odabirprozirne);
imagepng($image, 'sl114.png');
imagedestroy($image);
/* dodaj zvijezde odispod */
$image_1 = imagecreatefrompng('TexturaZvijezde.png');
$image_2 = imagecreatefrompng('sl114.png');
imagesavealpha($image_1, true);
imagecopy($image_1, $image_2, 0, 0, 0, 0, 1200, 120);
imagepng($image_1);
imagedestroy($image_1);
I haven't tested this, but according to the manual for imagesavealpha function,
You have to unset alphablending (imagealphablending($im, false)), to use it.
Example:
// Turn off alpha blending and set alpha flag
imagealphablending($png, false);
imagesavealpha($png, true);

I can't use transparent background with imagecopymerge

I am calling imagecopymerge($dst_r, $logo, 0, 0, 0, 0, $LogoX, $LogoY, 100); where $logo is a png file with transparent background. From some reason the background comes out white instead.
What am I doing wrong?
Thanks.
You need to use imagealphablending($dst_r, TRUE); to allow copying with retaining the transparent colors. Many more comments (...) in the manual suggest using imagecopy instead, because imagecopymerge was never intended to be used with transparency. If you use pct=100 anyway, then the normal imagecopy might be an option.
This is for text, but you can get the point. It would be more helpful if you post entire code.
$font = 25;
$string = "Hello";
$im = #imagecreatetruecolor(strlen($string) * $font / 1.5, $font);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
$lime = imagecolorallocate($im, 204, 255, 51);
imagettftext($im, $font, 0, 0, $font - 3, $lime, "font.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);

Image Overlay in PHP; Black Background?

When I perform and image overlay in php using the GD library, I always get a black background, however, all the images overlay correctly. Can someone help?
<?php
$images = array( $_GET['color'], $_GET['face'], $_GET['hat'] );
$img = imagecreatetruecolor(58, 75);
imagealphablending($img, true);
imagesavealpha($img, true);
imagecolorallocate($img, 255, 205, 255);
imagecolorallocate($img, 255, 255, 255);
imagecolortransparent($img, $white);
imagefilledrectangle($img, 0, 0, $imgWidth, $imgHeight, $white);
foreach($images as $fn) {
$cur = imagecreatefrompng($fn);
imagealphablending($cur, true);
imagesavealpha($cur, true);
imagecopy($img, $cur, 0, 0, 0, 0, 58, 75);
imagedestroy($cur);
}
header('Content-Type: image/png');
imagepng($img);
?>
// Create an image
$img = imagecreatetruecolor($imgWidth, $imgHeight);
$white = imagecolorallocate($img, 255, 255, 255);
// Make the background white
imagefilledrectangle($img, 0, 0, $imgWidth, $imgHeight, $white);
...could help.
This is a common problem, and the answer is already available on stack overflow; the answer there fixes the problem perfectly. You may want to try searching harder :)
I would suggest that you can make your life easier by using the vastly more powerful (but unfortunately poorly documented) imagick library if you're going to try to do anything more than the most basic image manipulation; it's faster, easier (again, once you get past the documentation) and more powerful.

Categories