• 0

[PHP] GD, Text and Transparency


Question

Ok, here we go...

I have a script that generates a PNG image, sets the background as transparent, then puts a string in the center of the image. The only problem is that the text looks horrible when the background is transparent...

<?php
header("Content-type: image/png");

$fontsize = 12;
$font = 'verdana.ttf';
$text = $_GET['text'];

$size = imagettfbbox($fontsize, 0, $font, $text);
$width = $size[2] + $size[0] + 8;
$height = abs($size[1]) + abs($size[7]) + 5;

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

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

imagefilledrectangle( $im, 0, 0, $width, $height, $white );
imagecolortransparent($im, $white);

$xcoord = 0;
$ycoord = $height - 3;

imagefttext($im, $fontsize, 0, $xcoord, $ycoord, $black, $font, $text);

imagepng($im);
imagedestroy($im);
?>

With a plain white background:textnontrrk4.png

With a transparent background: texttrvt4.png

I want the text to appear as it does in the image with the white background, though with a transparent background! Any help?

Link to comment
https://www.neowin.net/forum/topic/514771-php-gd-text-and-transparency/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Well, seems like I didn't think this one through very well...

Just set the initial background colour to one which is close to the background colour on the page... Works for me since the image is all based around one colour!

No idea if there is another way around it, like if the page had two contrasting background colours... But that's a problem for another day! Problem solved :)

  • 0
  apairet said:
Did someone found any decent solution to that problem?

I just found the solution...

<?php
	include("imgConst.php"); // contains font linkage and fontSize
	$text = $_GET[text];
	$arrayCoord = imagettfbbox($fontSize, 0, $font, $text);

	// Cr?ation ayant la taille du texte ? inclure
	$Neg = -1;
	$x = $arrayCoord[4];
	$baseLine = $arrayCoord[5] * $Neg;
	$y = $baseLine + 20;

	$imgTxt = @imagecreatetruecolor($x, $y);

	// Important stuff
	// Allocate a transparent background color
	$bgColor = ImageColorAllocateAlpha($imgTxt, 255, 255, 255, 127);
	// ..and a non-transparent text color
	$textColor = ImageColorAllocate($imgTxt, 0xFF, 0x00, 0x00);	

	// Turn on full alpha channel saving for PNG images
	imageSaveAlpha($imgTxt,true);



	// Fill with transparent and text with the other
	ImageFill($imgTxt, 0, 0 , $bgColor); 
	imagefttext($imgTxt, $fontSize, 0, 0, $baseLine, $textColor, $font, $text);

	// Affichage de l'image sur le navigateur
	header('Content-Type: image/png');

	imagepng($imgTxt);
	imagedestroy($imgTxt);
?>

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I have the Tab A9+ also, and I agree with all your points. I really like this tablet.
    • Microsoft changed recall to opt-in some time ago, which seems to still be the case. I personally feel like that is the right move for something like this. You are right, settings that turn themselves back on after an update is totally unacceptable; its weird how that only happened with privacy and advertising related settings...
    • I have a Tab S9 FE and a Tab A9+ which is a lot less expensive. I much prefer the TabA9+. 1. Its display is more landscape while that of the S9 FE is more boxy. 2. They both drain battery at about the same rate of between 7% and 9% an hour depending on how I use them 3. The Tab A9+ charges a lot faster than S9 FE, The S9 FE is better at handling memory, It is faster. They both have 8GB. The TabA9+ does a lot better at handling memory on Android 15, One UI 7.0 than it did on Android 14, One UI 6.1
    • Following rough launch, Splitgate 2 is going back to beta as studio announces layoffs by Pulasthi Ariyasinghe The sequel to the free-to-play arena shooter Splitgate was released just a couple of months ago alongside a surprise Battle Royale mode, but it doesn't look like the launch has gone too well for the studio, 1047 Games. In a lengthy social media post, the developer revealed that it is making some major changes within the company that will also affect its games. The studio said that it agrees with the community regarding Splitgate 2 launching too early with rushed features. Because of this, the title is returning to its beta state. While Splitgate 2 will remain playable, the studio will be deep in development reworking the title behind the scenes until at least early 2026. The soon-landing Season 3 content update is still planned for release. "We're returning not just to our roots in what we build, but in how we build it – with you," said the company. "That means more playtests, more surveys, more listening, and truly being community first. Our goal is to combine the DNA of the first game with the best improvements from the second." The studio also revealed that this shakeup means that it had to cut some team members from the studio. It didn't give an exact number for the roles being cut but said that "we hope to bring them back when we can." In the same vein of keeping costs low, 1047 Games will be shutting down the servers for the original Splitgate within a month's time. "While we'd love to keep servers online indefinitely, it's cost us hundreds of thousands of dollars over the past couple of years, and we have to prioritize our team," added the studio. It said that offline play or peer-to-peer matchmaking for Splitgate are being explored for fans to keep playing, but nothing concrete about these potential options was announced today.
  • Recent Achievements

    • Week One Done
      SmileWorks Dental earned a badge
      Week One Done
    • Community Regular
      vZeroG went up a rank
      Community Regular
    • Collaborator
      Snake Doc earned a badge
      Collaborator
    • Week One Done
      Snake Doc earned a badge
      Week One Done
    • One Month Later
      Johnny Mrkvička earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      587
    2. 2
      Michael Scrip
      199
    3. 3
      ATLien_0
      193
    4. 4
      +FloatingFatMan
      132
    5. 5
      Xenon
      122
  • Tell a friend

    Love Neowin? Tell a friend!