PHP Imagick – create images with texts

PHP Imagick

PHP has this amazing library (probably the best) called Imagick.

Have you ever wanted to generate images with certain texts on it ? and  certain background color ? with PHP ?

So, today I’ll be teaching you this simple technique to create images and write texts on it with this cool tool.

First of all, I’ll break this process down into few steps…

  1. Install ImageMagick®
  2. Install Imagick
  3. Write your code

Well, most people get confused about ImageMagic and Imagick.

 

Are they same ?  No.

 

ImageMagick is a software used to create, edit, and compose bitmap images. Also, It can read, convert and write images in a variety of formats.

In contrast, Imagick is a native php extension to create and modify images using the ImageMagick API.

Of course you can learn furthermore about these here.

So, that confusion seems like cleared right?

So, let’s go ahead.

01. Download and Install ImageMagick®

You can download the ImageMagick binaries from their official website.

Select the suitable package according to your operating system. Download and install it.

 

02. Installing Imagick php extension

You can download this extension on PECL.

This page  contains a full guide on imagick extension installation.

After restarting apache you’re good to go!

 

03. Let’s create images

Alright! After making sure you installed both of them correctly, you can finally move forward to write some codes.

First, create a new Imagick object.


$im = new Imagick();

Then Create a new image with the following arguments.

$im->newImage( 1000, 1000, '#FF0000' );

 

Alright, now we’ve created a blank image with a width, height and a background color.

Well you cannot see any output if this is working as it should, because, we aren’t writing the image to disk yet.

Now, let’s write some texts on this blank image.

Create new ImagickDraw object and set the following properties. All of them are self descriptive.

 


$text_draw = new ImagickDraw();
$text_draw->setFont( 'path/to/font' );
$text_draw->setFontSize( 150 );
$text_draw->setStrokeColor('#fff');
$text_draw->setStrokeWidth(6);
$text_draw->setStrokeAntialias(true); //try with and without
$text_draw->setTextAntialias(true); //try with and without
$text_draw->setFillColor('#C0C0C0');
$text_draw->setGravity( Imagick::ALIGN_CENTER );

Also, add this ImagickDraw object to the orginal image we created.


$im->annotateImage( $text_draw, 0, 400, 0, 'Hello World!');

And also, set the image format and write to disk.


$im->setImageFormat( "png" );
$im->writeImage( 'path/to/save/filename.png' );
$im->destroy();

(Be careful when you’re defining the path. Because in windows computers, the path should be given as absolute paths. Therefore a if you’re using Windows make sure you provide full path).

Finally, this is the full version of the code


$im = new Imagick();

$im->newImage( 1000, 1000, '#FF0000' );

$text_draw = new ImagickDraw();
$text_draw->setFont( 'path/to/font' );
$text_draw->setFontSize( 150 );
$text_draw->setStrokeColor('#fff');
$text_draw->setStrokeWidth(6);
$text_draw->setStrokeAntialias(true); //try with and without
$text_draw->setTextAntialias(true); //try with and without
$text_draw->setFillColor('#C0C0C0');
$text_draw->setGravity( Imagick::ALIGN_CENTER );

$im->setImageFormat( "png" );
$im->writeImage( 'path/to/save/filename.png' );
$im->destroy();

That’s it!  😉

 Summary

This tutorial discussed how to generate a simple image with texts on it. Likewise, PHPImagick and ImageMagic can be used to create even more complex images with images, texts and shapes on it.


Food for thought: Setting up Cron Jobs in Laravel

Leave a Reply

Your email address will not be published. Required fields are marked *