Writing a text with a shadow

In this example, I’ll show how you draw a text with shadow onto the image.

First, load an image:

  1. $image = WideImage::load('image.jpg');

Then, access the Canvas object of the image:

  1. $canvas = $image->getCanvas();

To set active font, use the useFont() method:

  1. $canvas->useFont('path/to/arial.ttf', 16, $image->allocateColor(0, 0, 0));

Draw the text into the lower right corner. The writeText() method supports smart coordinates for TTF fonts.

  1. $canvas->writeText('right', 'bottom', 'Hello, world!');

Now we draw white text over it, shifted by 1 pixel to the left and up:

  1. $canvas->useFont('arial.ttf', 16, $image->allocateColor(255, 255, 255));
  2. $canvas->writeText('right – 1', 'bottom – 1', 'Hello, world!');

And save the image:

  1. $image->saveToFile('image-with-text.jpg');
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Tumblr
  • Twitter