Writting text as an image

August 10, 2009 by nuno costa 6 comments

Draw text

It’s quite easy to do create an image containing a string

 

1
    $phMagick->fromString('My text');

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

 

1
2
3
    $p = new phMagick();
    $p->setDestination('text.png')
      ->fromString('phMagick - Image manipulation the easy way');
 
text1

 

Using a custom format

The default format is pretty ugly, so phMagick provide us with a way to change is defaults.

 

1
2
3
4
5
6
7
8
9
10
11
<?php
    $format = new phMagickTextObject();
    $format->fontSize(28)
           ->font('Arial.ttf')
           ->color('#fff')
           ->background('#fd3ab5');
 
 
    $p = new phMagick();
    $p->setDestination('text-custom.png')
      ->fromString('phMagick - Image manipulation the easy way', $format);
 
text-custom
 

For those who are not developers this code can start to look scary, but is quite simple to follow
In the first block we are defining the text format to be Arial 28pt white text on pink background
On the second block of code we create an image named text-custom.png from a string with the format we defined on the first block

6 comments so far Add Your Comment

  1. by sohil on November 17 2009 at 08:51

    does phMagick provide functionality to write text on image. Do let me know if ?

  2. by nuno costa on November 17 2009 at 17:44

    @ sohil,

    Not directly but you can do it with the watermark function by creating the text and then appending it to an image

    http://www.francodacosta.com/blog/phmagick/examples/watermarks

  3. by Adam Penly on January 21 2010 at 21:11

    Hey Sven-

    I’m LOVING phMagick…it’s making all my image manipulation a breeze. Thank you!

    I’m wondering, do you have an example or any suggestion when it comes to converting postscript files such as PDF or EPS to PNG with transparency? I feel like I must be missing something on the convert method. It doesn’t seem to want to maintain transparent channels.

    Thanks much!
    Adam

  4. by nuno costa on January 29 2010 at 22:02

    Hi Adam,

    Despite what you might think, I’m far from being an expert in images

    If you are converting to png you should have transparency.

    ImageMagick uses ghostscript to convert the pdf to an image, if that conversion is done via a “print to file” mechanism my guess is that you end up with a white instead of transparency

    I think you must mess with ghostscript directly!

    I found this link with a possible solution for you

    hope it helps

  5. by Krishna on March 14 2011 at 11:18

    Hi Nuno,
    This is a grate one.
    I am try to create an image using fromString(),But I did n’t find this plugin in plugins folder which i have downloaded from your site can please help me on this !!
    looking forward for your reply,
    Thanks
    Krishna .D

  6. by nuno costa on March 14 2011 at 13:19

    @Krishna

    it should be on txt.php inside the plugins folder

More from francodacosta.com

© francodacosta.com - All rights reserved