coding >> TIC – Text Image Converter (118 views)

This is a php5 library (two classes) I wrote to convert TrueType Font text into an image, using the GD library.
Download here: Text Image Converter (213.33 KB) (includes an example, documentation and some free fonts).
or from svn (read only): svn co svn://byteset.com/tic
Documentation is also available online here: http://www.andreabelvedere.com/docs/tic/

See the library in action here: http://whatshouldidonow.eu/ (The site is still a work in progress)

To extend the library to add support for other Font types just extend the TIC abstract class and implement the create and dimension methods.

See example below on how to use it:

<?php
/**
 * @file image.php
 */
require_once 'lib/tic.php';
 
TIC::factory('fonts/AAJAX.TTF')
    ->setText('Hello World !')
    ->setBackground(0x00, 0xff, 0xff)
    ->setColor(0x00, 0x00, 0x00)
    ->setSize(24)->create(true);

Than in your html:

<img src="image.php" alt="TIC" />

That will output the following image:

TIC

The library is under the LGPL license, which means you can freely use it for commercial and non commercial applications, please post a comment for feedbacks, bugs, or links of projects where you have use it.

Leave a Comment or Trackback from your own site.

3 Responses to “TIC – Text Image Converter”

  1. Martin says:

    How is it possible to generate images from TTF fonts for chinese characters? I do not know how to get those UNICODE characters as arguments to setText. I did also try to use the PHP image library directly but ran into similar problems, the PHP-chr() function allows only ASCII.

    I do have TTF font files (from university of Heidelberg) and want to generate chinese texts from a database using PHP.

    Reply

    andrea belvedere Reply:

    Hi Martin,
    I am not sure I have fully understood what you are asking, however I have run this test with a Japanese font and it works perfectly:

    <?php
    require_once dirname($_SERVER['DOCUMENT_ROOT']) . '/tic/lib/tic.php';
     
    $fontfile = dirname($_SERVER['DOCUMENT_ROOT']) . '/tic/fonts/ttf-japanese-mincho.ttf';
    $tic = TIC::factory($fontfile);
    $tic->setText('アンドレア')
    ->setBackground(0xaa, 0xaa, 0xaa)
    ->setColor(array(0x00, 0x00, 0x00))
    ->setPadding(5)
    ->create(true); // create and output
    ?>

    And the output is here below:
    andrea

    In your case you will pass text to setText as follow: setText($unicode); where $unicode will hold your unicode string from the database.
    Hope this helps.
    Andrea

    Reply

  2. Your website is excellent. I m gonna read all, thank you. Keep working on it.

    Reply

Leave a Reply