Custom Invitations using PHP!

People often don’t realise how versatile PHP and its add-on’s, including Imagik can be. I’ve used the following code in the past, and again recently, and I always receive comments on how nicely created invitations are. Its the personal touch that does it!

Firstly, I created an invitation using the awesome Pixelmator for Mac. You can use Gimp or Photoshop, or whatever takes your fancy, but what your looking for is a nice invitation with room for peoples names, like this:

Pre-PHP processing

(I’ve saved this as a PNG file)

Next up, some pre-processing work is required. I’ll go into this stuff in more detail in future blog posts, but what you want to do is set up a LAMP install (Linux, Apache, MySql and PHP). Make sure you have Imagik installed. You then want to install PHPMyAdmin, and create a database and one table with 2 columns (ID and Name).

ID Name
1 Nick T
2 Mr. A. N. Other

You can then plop the following code into your root directory, customise as needed, and away you go!

// Check connection
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
// Database connection has passed, now to select ALL the data from the Table "NAME"
$result = mysqli_query($con,"SELECT * FROM name");
while($row = mysqli_fetch_array($result)) //Grab everything and put it into an array
{ //Start the loop
$image = new Imagick('siteinfo.png'); //The name of the background image
$draw = new ImagickDraw(); //Tell imagick were going to draw on this image
$draw->setGravity (Imagick::GRAVITY_CENTER); //Centre our Text
$draw->setFillColor('black'); //Set the font colour to BLACK
$draw->setFont("/var/www/skia.ttf"); //Load the font we want to use
$draw->setFontSize( 40 ); //Set our font size
$text = $row['name']; //Place the name into a variable
$image->annotateImage($draw, 240, -150, 0, $text); //Write this name into the image at the location specified
$image->setImageFormat('jpg'); //Tell imagick were going to save it as a JPG
$name = $row['id'] . ".jpg"; //Give it the file name of its ROW in the database
$image->writeImages($name, TRUE); //Save it to the root folder (where the script was ran)
} //Lather, Rinse, Repeat!
mysqli_close($con); //Close the MySQL connection

One of the important functions in the above script is the

$image->annotateImage($draw, 240, -150, 0, $text); //Write this name into the image at the location specified

Using the awesome documentation online at, you find out that the 3 numbers, in order mean X position, Y position and angle. Angle is set to 0 because I don’t want the text angled. A nice straight line of text. The X position relates to where in the image the text is placed horizontally, and the Y relates to the vertical position. There was no hard and fast way to figure out those numbers, so I played about with them until I was happy. There is more than likely a function already created, or you could write your own that works out the exact location, but for my needs, a little bit of guessing with a small sample yielded pretty good results!

After running that PHP file, you get this at the other end:

Image 1
Image 1


Image 2
Image 2

The above took less than 1 second to produce, once the script was ran. I used this to create over 60 invites, and that took around 10 seconds of script execution time! (If I remember, I’ll put in a timer to see how long exactly it took. Bare in mind this was completed on the Ubuntu Laptop – which isn’t exactly a powerhouse!)

I’m fully aware that there was probably a workflow for Automator on the Mac which I could have used – but its nice to keep the PHP skills going for little things like this!

UPDATE: Using the 2 names above, the script was executed in 0.74 seconds – This is on a Dual Core Intel(R) CPU T1400 @ 1.73GHz with 1Gb of RAM.

UPDATE 2: Using 59 names from the database, the script was executed in 6.01 seconds. Not bad!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.