CakePHP Image Helper for Front End Optimization

Going along with one of the many ways to optimize front end performance, I created an image helper for CakePHP that will get the image dimensions and include them as html attributes.

<?php
/**
 * This class builds an image tag. The main purpose of this is to get the image dimensions and
 * include the appropriate attributes if not specified. This will improve front end performance.
 * 
 * @author Seth Cardoza <[email protected]>
 * @category image
 * @package helper
 */
class ImageHelper extends Helper
{
    /**
     * Builds html img tag determining width and height if not specified in the
     * attributes parameter.
     *
     * @param string $src relative path to image including the 'img' directory
     * @param array $attributes array of html attributes to apply to the image
     *
     * @access public
     *
     * @return no return value, outputs the img tag
     */
    public function displayImage($src, $attributes = array()) {
        //get width/height via exif data
        //build image html
        if(file_exists(WWW_ROOT . $src)) {
            $image_size = getimagesize(WWW_ROOT . $src);
            if(!array_key_exists('width', $attributes) && array_key_exists('height', $attributes)) {
                $attributes['width'] = ($image_size[0] * $attributes['height']) / $image_size[1];
            } elseif(array_key_exists('width', $attributes) && !array_key_exists('height', $attributes)) {
                $attributes['height'] = ($image_size[1] * $attributes['width']) / $image_size[0];
            } else {
                $attributes['width'] = $image_size[0];
                $attributes['height'] = $image_size[1];
            }
        }

       
        $html = '<img src="' . $src . '"';
       
        foreach($attributes as $key => $value) {
            $html .= ' ' . $key . '="' . htmlentities($value, ENT_COMPAT, 'ISO-8859-1', false) . '"';
        }
       
        $html .= ' />';
        echo $html;
    }
}
?>

Download CakePHP Image Helper