Draw Text

August 9, 2009 by nuno costa 7 comments

phMagick :: Drawing text

it's quite easy to do create an image containing a string

$pmMagick->fromString('My text');

and you will end up with an image with the default formatting containing the text My text . You can apply some formatting to the text, font name, size, color, and background.

 

Draw using the default format

$phMagick = &new phMagick();
$phMagick->setDestination('text.png')
 ->fromString('phMagick rules!!!!!');

 

result:

 

Changing the default format

As the default formatting may not suit you, you can change the system wide default values of phpMagick without messing with phMagick code so you don't have to do it every time

phMagickTextObject::defaultColor('#f00');
phMagickTextObject::defaultFontSize(55);
 
 
$phMagick = &new phMagick();
$phMagick->setDestination('text1.png')
 ->fromString('phMagick rules!!!!!');

 

result:

 

Using a custom format

$format = new phMagickTextObject();
$format->fontSize(48)
 ->font('Arial.ttf')
 ->color('#fff')
 ->background('#fd3ab5');
 
$phMagick = &new phMagick();
$phMagick->setDestination('text-custom.png')
 ->fromString('phMagick rules!!!!!', $format);

result:

 

7 comments so far Add Your Comment

  1. by Rubin Shrestha on December 2 2009 at 13:18

    Is it possible to use p div tags and text formatting in phMagic ?

  2. by Marius on December 7 2009 at 22:09

    Hi

    Can you draw tables with your wrapper class?
    In the meantime I will work with tabs/spaces but would be very useful if there was a library for tables

    Let me know! Thanks

  3. by Shiplu on December 14 2009 at 22:39

    Is it possible to draw East Asian Utf-8 fonts?

    Example Bengali text could be, বাংলাদেশ

  4. by nuno costa on December 15 2009 at 16:44

    @Marius

    No, that is not supported at the moment

  5. by nuno costa on December 15 2009 at 16:45

    @Shiplu,

    It should be possible if your font supports that chars, just need to set the full path to the font you want to use

  6. by Fynn on January 7 2010 at 23:43

    Would it be possible to add text on an image where two words have different font colors?

    Thanks in advance

  7. by nuno costa on January 20 2010 at 21:32

    @Fynn
    yes it is, but is not straitforward

    you must create an image for each word, and then join them into one with the tile( array(‘image1′,’image2′ )) function

More from francodacosta.com

© francodacosta.com - All rights reserved