Make an invitation card batch in php
13 January 2011
My mom’s sister is getting married. On the wedding party she wanted to has a named card near each sitting place so everyone would know where to sit. She decided to print it by herself and asked me if I could prepare 108 individual printable copies from the list of members. And there would be nothing special in it apart from the fact that the wedding is in this Saturday and today is Thursday. I agreed ofcourse. Cards hasn’t to be the same but pretty random (a lot of pigeons and hearts flying around the names).
First encountered problem
Hmm, it was a list itself I think. That was a Microsoft Word document where each name was in new line. Without any separators like commas etc. The way of creating cards was also a problem. I don’t have a lot of free time and splitting each card manually would be a total waste of it. I heard that some kind of batch is possible thru a Word app, but, firstly- I don’t have Word (I opened document with Google docs), and secondly- I don’t know how. So in this case I would have to download some trial and learn how to make it. I didn’t even checked if it is possible in my OpenOffice since I haven’t using it for a long time and simply forgot about it. About seven years ago I was really into Delphi (Object pascal) and thought I could do something with it, but I misjudged my strenght for I forgot mostly everything. And here comes php out of the blue…
Prepare the list
Ok, so I thought that would be easy run. Make an array, pass it through a loop and that’s it. Well, I was pretty right but the problems were on every step. First of all I had to change the list from
some name
another name
and another
here is something pretty much longer
blah blah blah
to
"some name",
"another name",
"and another",
"here is something pretty much longer",
"blah blah blah"
The solution were simplier than I thought. Here is a code that do the trick:
<?php
$file = file("lista.txt"); //open up a file with names
//create a foreach statement and add " on beginning and ",<br> on the end of each line
foreach ($file as $line) {
echo "\"".$line."\",\n <br>"; //display every line on the screen
}
?>
I could pass the result to the file, but that was fastest way for me.
Draw meh
Ok, so truly I will say that I made some backgrounds in photoshop, but in this example I will build a code just for one background for better clarity. Oh, and I will tell that it is pretty good idea to use a localhost for it ;) First of all, I will specify some variables:
<?php
$font = 'trebucit.ttf'; // choose your font and copy it to the folder where your script is
$quotes = array(); // paste the list printed in previous step inside the array
$im = ImageCreateFromJpeg("pusty.jpeg"); // here is our background image with pigeons and hearts 16x8cm in 300dpi
$fontcolor = imagecolorallocate($im, 0, 0, 0); // black color for text for my background is white
$fontsize = 70;
$y = 700; //distance from the top
//below is the trick to center the text
$text_width = 40 * strlen($quotes); //actually 40 should be the size of font (so 70 in this case) but it worked with this value however
$x = ceil((1890 - $text_width) / 2); //ceil function rounds up the result of math, 1890 is a total width of image
} ?>
As you can see I wanted my text to be centered. I don’t think that the calculations could be not understandable. Then I just used the function imagettftext. I decided to loop the file creating with for loop. It is most complex I think and fits here perfectly. Here is a final code with explanations:
<?php
$font = 'trebucit.ttf';
$quotes = array(
"Eugeniusz Geniusz",
"Maciek Placek",
"Jan Kowalski",
...
"Robert Burneika",
"Józef Tkaczuk"
);
//make a for loop that will stop after 108th step for I knew there will be 108 names.
for( $i = 0; $i <= 107; $i++ ) {
$im = ImageCreateFromJpeg("pusty.jpeg"); //open up the background
$fontcolor = imagecolorallocate($im, 0, 0, 0); //black for text
$fontsize = 70;
$y = 700; //distance from top
$text_width = 40 * strlen($quotes[$i]);
$x = ceil((1890 - $text_width) / 2); //center the text - distance from left that is calculated depends of name lenght, 1890 is a total widt of image
imagettftext($im, $fontsize, 0, $x, $y, $fontcolor, $font, $quotes[$i]); //background, fontsize, angle, distance from left, distance from top, fontcolor, fontface, text
imagejpeg($im, $quotes[$i].'.jpeg', 100); //save a file with best quality
imagedestroy($im); //clear the memory
}
?>
As you can see I specified $i in for statement as a 0 and based my loop on it. Pretty simple, but works. I had a problem with polish fonts, but fixed it pretty fast. After all you have all images saved in seconds. And… That’s it :)
❧

Comments are closed.