The beauty of gzip compression

I have just discovered the magic of gzip compression on web pages. I knew it would be good, but I was blown away by the saving.

Without gzip compression, the homepage of the Oz Broadband Speed Test was 32.37 KB (33142 bytes), with gzip turned on the same page was 7.61 KB (7789 bytes).

That’s a massive 75% saving in data. While it might not seem like much, on a high trafficked site it really starts to add up.

Turning gzip on is also easy, with the use of an .htaccess file on an Apache web server, and is done with a single line of code.

    php_value output_handler ob_gzhandler

I’m not sure how much extra load this will add to the server, but I am hoping it is minimal. I’ll keep an eye on this over the next few days.

The best thing with this is that if your browser doesn’t support any compression methods (highly unlikely in today’s browsers) then it will simply send the page back without compression.

It’s really win win.

I should also point out that this only compresses PHP files, and not CSS, JavaScript or images.

Google Charts API Extended Encode Scaling

I’ve recently been playing with the Google Charts API for a project, and ran into a problem with graph scaling when using extended encoding.

The problem was that there was no scaling on the graph when using extended encoding, so values of the graph were plotted with the y-axis having a maximum value of 4095.

This meant that smaller values look insignificant on the graph, not to mention all the wasted space. The solution I came up with is to scale the data so it fills the chart appropriately.

The PHP code I came up with is below, and also extends on a function written by Ben Dodson.

    < ?php
/*
 * Returns a scaled value.
 *
 * @param    value     Int to scale
 * @param    max       Maximum int in array to calculate scale value
 * @param    scale     The int to scale value to
 * @return             Scaled value
 * @author             Alex McKenzie 
 */
function scale_value($value, $max, $scale = 4095) {
	return ($value/$max) * $scale;
}

/**
 * Retunrs an extended encoded string for use with Google Charts API.
 *
 * Modified function - original by Ben Dodson (http://bendodson.com/blog/2008/02/28/google-extended-encoding-made-easy/).
 *
 * @param    array     Array of values to encode
 * @param    scale     Whether to scale the values
 * @return             Extended encoded string
 * @author             Alex McKenzie [alex [at] alexmckenzie [dot] info]
 */
function array_to_extended_encoding($array, $scale = 'yes') {
    $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';

    // Scale values before encoding if required.
    if ($scale == 'yes') {
        $max = max($array);
        $scaled_array = array();
        foreach($array as $value) {
            array_push($scaled_array, scale_value($value, $max));
        }
        $array = $scaled_array;
    }

    // Encode values in array.
    $encoding = '';
    foreach ($array as $value) {
        $first = floor($value / 64);
        $second = $value % 64;
        $encoding .= $characters[$first] . $characters[$second];
    }
    return $encoding;
}?>

Now using some sample data, we can see the difference scaling makes in the following two examples:

No scaling Scaling

The graphs above were generated with the following code:

    < ?php
$graph = array(200,300,200,250,350,150,100);
?>
 
<img src="https://chart.apis.google.com/chart?cht=bvs&chs=200x150&chd=e:< ?=array_to_extended_encoding($graph, $scale = 'no')?/>" alt="No scalling" />
<img src="https://chart.apis.google.com/chart?cht=bvs&chs=200x150&chd=e:< ?=array_to_extended_encoding($graph)?/>" alt="Scalling" />

Uppercase first word in a string or sentence

I have just had a task which requires making the first word of a string uppercase. I have come up with quite an easy and simple solution and thought I would share it with you.

An example input string might look like “HELLO world. This is a test.” The required output is “Hello world. This is a test.”

Below is a function which will help you achieve this:


function uc_first_word($string) {
    $s = explode(' ', $string);
    $s[0] = ucfirst(strtolower($s[0]));
    $s = implode(' ', $s);
    return $s;
}

Now I know this function isn’t full proof, and assumes that the string you are passing actually contains words. If the first word was a roman numeral for example, this function would not work (ie VI would become Vi).

Usage of this function is quite easy:

$string = uc_first_word('HELLO world. This is a test.');

I hope that this might help someone searching for a similar solution.