Categories
Electronics General

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!

<?php
$con=mysqli_connect("localhost","USERNAME","PASSWORD","DATABASENAME");
// 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 PHP.net, 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

and

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!

Categories
Electronics Equipment General

Using a Windows 7 Upgrade disc with Bootcamp fresh install

I came across a problem when installing Windows 7 to my Mac using Bootcamp. I purchased my copy of Windows 7 when I had my older Windows PC  meaning I was eligible for the upgrade pricing. Using a Mac, however meant I couldn’t use the upgrade disc. Seeing as I don’t use my Windows PC anymore (it’s since been wiped and Ubuntu installed) I think I’m entitled to use the software I purchased on my Mac! The installation runs fine, with no issues, except when you enter your Product Key in. Windows gives you a nice error during install saying the Key is Invalid. It still allows you to install Windows 7, but you must reactivate within 3 days.
The quickest, and easiest way to activate Windows 7 using your own Product Key is to do the following:
1- Open up regedit (Click the Start Orb, type regedit and press enter)
2- Navigate to HKEY_LOCAL_MACHINE/Software/Microsoft/Windows/CurrentVersion/Setup/OOBE/
3- On the right hand side, double click MediaBootInstall and change the value to (from 1)
4- Close regedit, and start a command prompt as an administrator
5- Enter slmgr /rearm and wait for a message box telling you the command completed successfully
6- Reboot your Mac
7- Click Start, and type Activate Windows
8- Enter your Product Key and click OK.

Easy as pie! Special Thanks to Justin Kerr of the MaximumPC blog for the information. Their site has pictures that can also be followed!

Categories
Electronics Equipment General

Quick Note – Development Enviroment

As well as creating the blog, I also have to create a development environment. There are plenty of ways to do it, and no way is right, but in my circumstances I have chosen to use a late 2012 Mac Mini running OS X 10.8.2 and using Bootcamp with Windows 7. I also have Ubuntu running on a nearby laptop, as well as a Virtualised image on OSX.

The set up of everything went relatively smoothly, excluding using the Magic Mouse and Magic Trackpad in Windows 7. My specific issue resolves around both the mouse and trackpad appearing “jittery”, as if te batteries were dying and the full smooth movement of the mouse was not being sent to the computer. This has annoyed a lot of folk online, and as it turns out after lots of Googling, you have to do the following:

1 – Head to Device Manager, Then Network Adapters
2 – Right-Click the Broadcom 802.11n
3 – Choose the Advanced Tab.
4 – Locate Bluetooth Collaboration from the drop-down list, then choose “Enable”

A quick and simple fix! Many thanks to Kemal Kocabiyik and his blog for assisting me with this one!