Using PHP GD to create image form text with different fonts.

Posted by Meredith on Stack Overflow See other posts from Stack Overflow or by Meredith
Published on 2011-01-09T00:26:45Z Indexed on 2011/01/09 2:53 UTC
Read the original article Hit count: 236

Filed under:
|
|
|
|

I have been using this simple script to generate 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,4)), // G
                     hexdec(substr($hexValue,4,6))  // B
                    );
    }
    else return array(0, 0, 0);
}

?>

I call the script with file.php?text=testing script&color=000000

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

© Stack Overflow or respective owner

Related posts about php

Related posts about image