PHP's imagettftext creates black edges on transparent background
Asked Answered
G

2

5

I have this code:

$im = imagecreatetruecolor(70, 25);

$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);

imagecolortransparent($im, imagecolorallocate($im, 0,0,0));

$font = 'font.ttf';

imagettftext($im, 20, 0, 3, 22, $white, $font, $randomnr);

header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

header ("Content-type: image/png");
imagepng($im);
imagedestroy($im);

And like I said in the title it creates some black edges around the text. I also tried with the imagealphablending/imagesavealpha and i've had the same result(i used the white text on transparent background so that you can see what i am talking about):

black edges

UPDATE: the solution is:

$im = imagecreatetruecolor(70, 25);
$font = 'font.ttf';
//antialiasing:
$almostblack = imagecolorallocate($im,254,254,254); 
imagefill($im,0,0,$almostblack);
$black = imagecolorallocate($im,0,0,0); 
imagecolortransparent($im,$almostblack);

imagettftext($im, 20, 0, 3, 22, $white, $font, $randomnr);
...
Galling answered 1/10, 2012 at 12:5 Comment(0)
G
2

That is what you have specified here:

imagecolortransparent($im, imagecolorallocate($im, 0,0,0));

If you want to use some other color for transparent, choose some other color. Right now you're using black.

See imagecolortransparentDocs.

Also take note of this user's note on that same page:

A transparent background with text doesn't seem to work very well because of the antialiasing .

Goodloe answered 1/10, 2012 at 12:13 Comment(5)
yes i think that the antialiasing is the problem... is ther a way to fix that? I want the black color to be transparent. and as i said in the image is a transparent bg with white text on it so the image should be completely white.Galling
You could try to first fill the image with the transparent color. Also when you create the output image, ensure it has got an 8-bit alpha-channel.Goodloe
How to output with 8-bit alpha?Galling
You seem weak in search, this was asked already back in 2009: PHP gdLib 8-Bit PNG with alpha - also take a look at the difference between transparency and alpha. You might want to try with an alphacolor, not a transparent one. You see?Goodloe
Accepted for the antialiasing solution in the link you gave.Galling
C
6

Something like this should do the work:

$width = 70;
$height = 25;

$im = imagecreatetruecolor($width, $height);

$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 221, 221, 221);

imageSaveAlpha($im, true);
imageAlphaBlending($im, false);

$transparent = imageColorAllocateAlpha($im, 0, 0, 0, 127);
imagefilledrectangle($im, 0, 0, $width-1, $height-1, $transparent);
imageAlphaBlending($im, true);

$font = 'font.ttf';
$randomnr = "1234";
imagettftext($im, 20, 0, 3, 22, $white, $font, $randomnr);

header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

header ("Content-type: image/png");
imagepng($im);
imagedestroy($im);
Canvass answered 1/10, 2012 at 12:26 Comment(0)
G
2

That is what you have specified here:

imagecolortransparent($im, imagecolorallocate($im, 0,0,0));

If you want to use some other color for transparent, choose some other color. Right now you're using black.

See imagecolortransparentDocs.

Also take note of this user's note on that same page:

A transparent background with text doesn't seem to work very well because of the antialiasing .

Goodloe answered 1/10, 2012 at 12:13 Comment(5)
yes i think that the antialiasing is the problem... is ther a way to fix that? I want the black color to be transparent. and as i said in the image is a transparent bg with white text on it so the image should be completely white.Galling
You could try to first fill the image with the transparent color. Also when you create the output image, ensure it has got an 8-bit alpha-channel.Goodloe
How to output with 8-bit alpha?Galling
You seem weak in search, this was asked already back in 2009: PHP gdLib 8-Bit PNG with alpha - also take a look at the difference between transparency and alpha. You might want to try with an alphacolor, not a transparent one. You see?Goodloe
Accepted for the antialiasing solution in the link you gave.Galling

© 2022 - 2024 — McMap. All rights reserved.