PHP GD text with transparency / alpha background

okay, so I had a problem with my text lying over a partially transparent image. I want the text to be solid, but I want the background part of the image to be transparent, and part of the text to be solid, and I have a problem: the text inherits the transparent background of one of the previous layers. here is the code and an example of the output, and under this output I want it to look. the image lies on a light gray background, so the light border around the image between dark gray is transparent, but nothing else should be especially in the text. this, apparently, is not the text itself, but the background of transparent text blocks. which, as you see, is not very desirable. please help, this is the only problem I left to complete my project. :)

can not post images yet, so the link to the image is an example of output and the desired result ( orig ):

enter image description here

<?php $img = imagecreatetruecolor(200, 50); $imageX = imagesx($img); $imageY = imagesy($img); imagealphablending($img, false); imagesavealpha($img, true); $transparent = imagecolorallocatealpha($img, 255,255,255, 127); $white = imagecolorallocate($img, 255,255,255); $grey = imagecolorallocate($img, 127,127,127); imagefilledrectangle($img, 0, 0, $imageX, $imageY, $grey); imagefilledrectangle($img, 2, 2, $imageX-4, $imageY-4, $transparent); $font = "./arialbd.ttf"; $fontSize = 12; $text = "THIS IS A TEST"; $textDim = imagettfbbox($fontSize, 0, $font, $text); $textX = $textDim[2] - $textDim[0]; $textY = $textDim[7] - $textDim[1]; $text_posX = ($imageX / 2) - ($textX / 2); $text_posY = ($imageY / 2) - ($textY / 2); imagefilledrectangle($img, 10, 10, $imageX-10, $imageY-10, $grey); imagettftext($img, $fontSize, 0, $text_posX, $text_posY, $white, $font, $text); header("Content-Type: image/png"); imagepng($img); ?> 
+7
php transparent gd imagettftext
source share
1 answer

hah, I think I didn’t think too much about it. the solution was to bring the image back back before overlaying text on the image.

 <?php $img = imagecreatetruecolor(200, 50); $imageX = imagesx($img); $imageY = imagesy($img); imagealphablending($img, false); imagesavealpha($img, true); $transparent = imagecolorallocatealpha($img, 255,255,255, 127); $white = imagecolorallocate($img, 255,255,255); $grey = imagecolorallocate($img, 127,127,127); imagefilledrectangle($img, 0, 0, $imageX, $imageY, $grey); imagefilledrectangle($img, 2, 2, $imageX-4, $imageY-4, $transparent); $font = "./arialbd.ttf"; $fontSize = 12; $text = "THIS IS A TEST"; $textDim = imagettfbbox($fontSize, 0, $font, $text); $textX = $textDim[2] - $textDim[0]; $textY = $textDim[7] - $textDim[1]; $text_posX = ($imageX / 2) - ($textX / 2); $text_posY = ($imageY / 2) - ($textY / 2); imagefilledrectangle($img, 10, 10, $imageX-10, $imageY-10, $grey); imagealphablending($img, true); imagettftext($img, $fontSize, 0, $text_posX, $text_posY, $white, $font, $text); header("Content-Type: image/png"); imagepng($img); ?> 
+19
source share

All Articles