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)),
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