Using PHP GD to create image form text with various fonts

I used this simple script to create images from text:

<?php header('Content-type: image/png'); $color = RgbfromHex($_GET['color']); $text = urldecode($_GET['text']); $font = 'arial.ttf'; $im = imagecreatetruecolor(400, 30); $bg_color = imagecolorallocate($im, 255, 255, 255); $font_color = imagecolorallocate($im, $color[0], $color[1], $color[2]); imagefilledrectangle($im, 0, 0, 399, 29, $bg_color); imagettftext($im, 20, 0, 10, 20, $font_color, $font, $text); imagepng($im); imagedestroy($im); function RgbfromHex($hexValue) { if(strlen(trim($hexValue))==6) { return array( hexdec(substr($hexValue,0,2)), // R hexdec(substr($hexValue,2,2)), // G hexdec(substr($hexValue,4,2)) // B ); } else return array(0, 0, 0); } ?> 

Am I calling a script with file.php? text = testing script & color = 000000

Now I would like to know how I could generate text with normal and bold fonts mixed in one image, something like file.php?text=testing <b>script</b>&color=000000


Thanks to dqhendricks for helping me figure this out.

Here's a quick script that I wrote that still needs a lot of improvements, but for basic functionality, it works fine:

 <?php header('Content-type: image/png'); $color = RgbfromHex($_GET['color']); $im = imagecreatetruecolor(400, 30); $white = imagecolorallocate($im, 255, 255, 255); imagefilledrectangle($im, 0, 0, 399, 29, $white); $tmp = $_GET['text']; $words = explode(" ", $tmp); $x = array(0,0,10); // DUMMY ARRAY WITH X POSITION FOR FIRST WORD $addSpace = 0; foreach($words as $word) { if($addSpace) $word = " ".$word; // IF WORD IS NOT THE FIRST ONE, THEN ADD SPACE if(stristr($word, "<b>")) { $font = 'arialbd.ttf'; // BOLD FONT $x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, str_replace(array("<b>","</b>"), "", $word)); } else { $font = 'arial.ttf'; // NORMAL FONT $x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, $word); } $addSpace = 1; } imagepng($im); imagedestroy($im); function RgbfromHex($hexValue) { if(strlen(trim($hexValue))==6) { return array( hexdec(substr($hexValue,0,2)), // R hexdec(substr($hexValue,2,2)), // G hexdec(substr($hexValue,4,2)) // B ); } else return array(0, 0, 0); } ?> 

Note. This will only work for โ€œboldโ€ individual words separated by spaces, and not for the bold part of a word.

Call script using file.php?text=testing+<b>script</b>&color=000000

+6
php image image-processing gd imagettftext
source share
2 answers

you will need to download the font file in bold and make two separate calls to imagettftext (), one with each font that you want to use. how to parse a string to find out which parts you would like to make bold, and where you should print each part of the text, it sounds like this will become very complex code. what are you using it for? there may be better solutions to achieve the same.

Adding

Use the return value from imagettftext () to determine where the next text print should begin.

From the documentation:

Returns an array with 8 elements representing four points, creating a bounding box for the text. The order of the points is lower left, lower right, upper right, upper left. Dots refer to the text regardless of the angle, so โ€œupper leftโ€ means in the upper left corner when you see the text horizontally. Returns false on error.

0
source share

Answer for:

Parsing would not be a problem, I would not understand how I will find the X-position for the second word in the example? text = testing script . I mean, how do I know where the first word ends, so I can put the second one there with another imagettftext () and bold. - Meredith Jan 9 '11 at 2:43


itโ€™s funny how someone took the time to say โ€œsee the edits for the answer to this,โ€ when they could just say to blow the string in spaces, then each word is in an array.

 <?PHP $sentence = "I LOVE giving retarded answers that don't amount to jack!"; $sentence = explode(" ",$sentence ); for($s=0;$s<count($sentence);$s++){ #THERES YOUR INDIVIDUAL WORDS! $word = $sentence[$s]; } ?> 

Your X position will be just $ s for your logic. To get the second word, you can do this:

 <?PHP $word1 = $sentence[0]; $word2 = $sentence[1]; $word3 = $sentence[2]; $word4 = $sentence[3]; ?> 

Yes, I called $ words for mental visual effect only.

$ sentence [1] will be word 2

-one
source share

All Articles