PHP

Posts about the server side scripting language PHP

Creating A Thumbnail Of A Word Document With PHP And LiveDocx

24th September 2011 - 4 minutes read time

In any list of documents in a system it is a good idea to add some thumbnails of the document so that your users can get a quick overview of what a document looks like before downloading it. This is a good alternative to just displaying an icon of the document type.

Creating Word document icons is very simple thanks to a service called LiveDocx. LiveDocx was created as a web service to allow the easy creation of most document formats from a simple template. However, it is possible to send a normal Word document as the template file and get an image of the file in return.

The best way to use LiveDocx with PHP is to use the LiveDocx class contained within Zend Framework. You can use this outside of a Zend Framework application by including the file Zend/Service/LiveDocx/MailMerge.php at the top of your script.

Desaturate Images With PHP

6th September 2011 - 5 minutes read time

To desaturate an image means to remove all non-greyscale colours from it, essentially creating a black and white version. To do this you can use the function imagecopymergegray(), which is part of the GD image library. This function is a little difficult to figure out, but what is does is sample one image into another and optionally changing the amount of colours that are kept during the sampling process. The function can be made to sample just a section of the image, or the whole thing. The parameters of the function are as follows.

Getting Started With Xdebug And Netbeans 7 On Windows

16th August 2011 - 16 minutes read time

Note: This article assumes that you have a working Apache/PHP environment running with the latest major version of Netbeans (current 7) installed.

Xdebug is a brilliant PHP extension that provides both debugging and profiling tools. From a basic install it will vastly improve the quality of the error messages but it can also be made to interactively debug scripts using a number of different IDE's. It can also be used as a code profiling tool, but I won't be going into that in this post as that is a little beyond the scope of what I will be talking about.

Installation

If you are using XAMMP to host your Apache/PHP environment then you can skip the following step. Xdebug is actually bundled with XAMMP so you just need to enable it by going into your php.ini file and uncommenting the options you need.

PHPUnit Skeleton Classes

25th July 2011 - 7 minutes read time

If you create classes in PHP then you should be unit testing them as much as you can. Setting up unit testing classes for your code can be time consuming and involve a bunch of copying and pasting. Thankfully, PHPUnit comes with a couple of helper functions that allow the creation of unit testing classes automatically, which can save a bit of copying and pasting.

As an example for this post I will use the following Spider class, which is part of some code I am working on at the moment to create a simple site spider in PHP.

Netscape HTTP Cooke File Parser In PHP

30th June 2011 - 5 minutes read time

I recently needed to create a function that would read and extract cookies from a Netscape HTTP cookie file. This file is generated by PHP when it runs CURL (with the appropriate options enabled) and can be used in subsequent CURL calls. This file can be read to see what cookies where created after CURL has finished running. As an example, this is the sort of file that might be created during a typical CURL call.

Number Pair Additions In PHP

16th April 2011 - 4 minutes read time

I was helping my son out with his maths homework today and we came across a question that asked him to find some combinations of two numbers that added up to a given number (he is only 6 so this was good maths practice). This got me to thinking about how to calculate all possible ways in which two numbers could be added together to make another number.

The only rule had to be in there was that doing things like 1+11=12 and 11+1=12 are basically the same thing and so would be cheating. In which case I realised that it would only be necessary to use half of the numbers when creating the list, so for the number 12 every combination can be found using the numbers 0-6 because after this we start duplicating the sums, just swapping the order.

Here is the function I came up with.

Revisiting filter_var() and FILTER_VALIDATE_URL

1st April 2011 - 6 minutes read time

Quite a while ago I looked at using the filter_var() function to validate URL's using the FILTER_VALIDATE_URL flag and someone pointed out recently that this function has not only changed since the initial release, but that a number of flags can be added to change the way that this function works. Here are the flags available.

PHP Unconference Europe 2011: Part 2

3rd March 2011 - 27 minutes read time

Sunday

Due to the crappy train service from my home town I had to drive into Manchester on Sunday, but made it in good time to have a coffee before we went to vote on the talks. Some people had a heavy night drinking the night before and therefore didn't make it in the morning (or at all). Some has also opted to only attend the Saturday and so there were a few faces missing, but there were still plenty of people there.

PHP Unconference Europe 2011: Part 1

27th February 2011 - 20 minutes read time

This weekend saw the PHP Unconference Europe 2011 event in Manchester. The organisers were hoping that the rain would hold off for the weekend, and being true to form Manchester was wet and cold. Being an unconference the talks and discussions are mainly casual and dependent on what everyone decided to see on the day. The event ran across Saturday and Sunday with different talks on each day.

Saturday

Picture taken by Lorna Mitchell.

PHP Array Of Countries

3rd February 2011 - 12 minutes read time

Use the following array if you want to get a list of countries, along with their codes. These codes are the 2 letter ISO code, the 3 letter UN code and the 3 number UN code. I had to build this the other day in order to present a list of countries in a form so I post it here in case I need something like it again.