<?php

/**
 * PHPTAL filter to automatically add width and height to images
 *
 * @author TAAT
 * @version 1.0
 */
class My_PHPTAL_Filter_ImageSize implements PHPTAL_Filter {
    
// PROPERTIES

    /**
     * DOM
     * @access private
     * @var string
     */
    
private $_dom null;

    
/**
     * Path to root
     * @access private
     * @var string
     */
    
private $_root '/home/site/14421/www/';


    
/**
     * Character encoding
     * @access private
     * @var string
     */
    
private $_encoding 'UTF-8';

    
/**
     * Image types (extensions) to process
     * @access private
     * @var string
     */
    
private $_images = array('png''gif''jpeg''jpg');

    
/**
     * Overwrite existing values
     * @access private
     * @var string
     */
    
private $_overwrite true;

    
// PUBLIC METHODS

    /**
    * Set images to process
    * @access public
    * @param string $params, [$param1...]
    * @return object itself
    */
    
public function setImages($params) {
        if (
is_array($params)) {
            
$this->_tags $params;
        } else {
            
$this->_tags = array();
            foreach (
func_get_args() as $arg) {
                
$this->_tags[] = (string) $arg;
            }
        }
        return 
$this;
    }

    
/**
     * Sets if to overwrite existing values
     * @access private
     * @param bool $flag
     * @return null
     */
    
private function setOverwrite($flag) {
        
$this->_overwrite $flag;
    }

    
/**
     * Sets the value of the private property 'root'
     *
     * @access public
     * @param string $root Property value
     * @return object itself
     */
    
public function setRoot($root) {
        
$this->_root $root;
        return 
$this;
    }

    
/**
     * Filter XHTML input
     * @access public
     * @param string $xhtml input code
     * @return string
     */
    
public function filter($xhtml) {
        
$dom DOMDocument::loadXML($xhtml);
        
$dom->encoding $this->_encoding;
        
$dom->preserveWhiteSpace true;
        
$this->_dom $dom;

        
$items =  $dom->getElementsByTagName('img');
        foreach (
$items as $item) {
            
$width $item->getAttribute('width');
            
$height $item->getAttribute('height');
            if (empty(
$width) || empty($height) || $this->_overwrite) {
                
$src $item->getAttribute('src');
                if (
$src[0] == '/') {
                    
$src rtrim($this->_root'/') . $src;
                }
                
$filename realpath($src);
                if (!empty(
$filename)) {
                    
$extension substr($filenamestrrpos($filename'.')+1);
                    if (
in_array($extension$this->_images)) {
                        
$size getimagesize($filename);
                        if (empty(
$width) || $this->_overwrite) {
                            
$item->setAttribute('width'$size[0]);
                        }
                        if (empty(
$height) || $this->_overwrite){
                            
$item->setAttribute('height'$size[1]);
                        }
                    }
                }
            }
        }

        
$xhtml $dom->saveXML();

        
// remove XML prolog
        
$xhtml preg_replace('@<\?xml[^\?]+\?>\n?@s'''$xhtml);

        return 
$xhtml;
    }

}

?>