<?php
/*
*   replacePngTags - Justin Koivisto [W.A. Fisher Interactive] 7/1/2003 10:45AM
*   Modified: 2/11/2004 10:18AM
*
*   Modifies IMG tags for MSIE5+ browsers to ensure that PNG-24 transparencies
*   are displayed correctly.  Replaces original SRC attribute with a transparent
*   GIF file (spacer.gif) that is located in the same directory as the orignal
*   image, and adds the STYLE attribute needed to for the browser. (Matching
*   is case-insensitive. However, the width attribute should come before height.
*
*   Also replaces code for PNG images specified as backgrounds via:
*   background-image: url('image.png'); When using PNG images in the background,
*   there is no need to use a spacer.gif image.
*
*   @param  $x  String containing the content to search and replace in.
*   @result Returns the modified string.
*/
function replacePngTags($x,$spacer="images/spacer.gif")
{
    // make sure that we are only replacing for the Windows versions of Internet
    // Explorer 5+, and not Opera identified as MSIE
    $msie='/msie\s([5-9])\.?[0-9]*.*(win)/i';
    $opera='/opera\s+[0-9]+/i';
    if(!isset($_SERVER['HTTP_USER_AGENT']) ||
        !preg_match($msie,$_SERVER['HTTP_USER_AGENT']) ||
        preg_match($opera,$_SERVER['HTTP_USER_AGENT']))
        return $x;
    
    // find all the png images in backgrounds
    preg_match_all('/background-image:\s*url\(\'(.*\.png)\'\);/Uis',$x,$background);
    for($i=0;$i<count($background[0]);$i++){
        // simply replace:
        //  "background-image: url('image.png');"
        // with:
        //  "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(
        //      enabled=true, sizingMethod=scale src='image.png');"
        // haven't tested to see if background-repeat styles work...
        $x=str_replace($background[0][$i],'filter:progid:DXImageTransform.'.
                'Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale'.
                ' src=\''.$background[1][$i].'\');',$x);
    }

    // OK, time to find all the IMG tags with ".png" in them
    preg_match_all('/(<img.*\.png.*>|<input.*type=([\'"])image\\2.*\.png.*>)/Uis',$x,$images);
    while(list($imgnum,$v)=@each($images[0])){
        $original=$v;
        $atts=''; $width=0; $height=0;
        // If the size is defined by styles, find
        preg_match_all('/style=".*(width: ([0-9]+))px.*'.
                        '(height: ([0-9]+))px.*"/Ui',$v,$arr2);
        if(is_array($arr2) && count($arr2[0])){
            // size was defined by styles, get values
            $width=$arr2[2][0];
            $height=$arr2[4][0];
        }
        // size was not defined by styles, get values
        preg_match_all('/width=\"?([0-9]+)\"?/i',$v,$arr2);
        if(is_array($arr2) && count($arr2[0])){
            $width=$arr2[1][0];
        }
        preg_match_all('/height=\"?([0-9]+)\"?/i',$v,$arr2);
        if(is_array($arr2) && count($arr2[0])){
            $height=$arr2[1][0];
        }
        preg_match_all('/src=\"([^\"]+\.png)\"/i',$v,$arr2);
        if(isset($arr2[1][0]) && !empty($arr2[1][0]))
            $image=$arr2[1][0];
        else
            $image=NULL;

        // We do this so that we can put our spacer.gif image in the same
        // directory as the image
        $tmp=split('[\\/]',$image);
        array_pop($tmp);
        $image_path=join('/',$tmp);
        if(strlen($image_path)) $image_path.='/';

        // end quote is already supplied by originial src attribute
        $replace_src_with=$spacer.'" style="width: '.$width.
            'px; height: '.$height.'px; filter: progid:DXImageTransform.'.
            'Microsoft.AlphaImageLoader(src=\''.$image.'\', sizingMethod='.
            '\'scale\')';
        
        // now create the new tag from the old
        $new_tag=str_replace($image,$replace_src_with,$original);
        
        // now place the new tag into the content
        $x=str_replace($original,$new_tag,$x);
    }
    return $x;
}
?>