1   <?php
  2   /*
  3   *   replacePngTags - Justin Koivisto [W.A. Fisher Interactive] 7/1/2003 10:45AM
  4   *   Modified: 2/11/2004 10:18AM
  5   *
  6   *   Modifies IMG tags for MSIE5+ browsers to ensure that PNG-24 transparencies
  7   *   are displayed correctly.  Replaces original SRC attribute with a transparent
  8   *   GIF file (spacer.gif) that is located in the same directory as the orignal
  9   *   image, and adds the STYLE attribute needed to for the browser. (Matching
  10   *   is case-insensitive. However, the width attribute should come before height.
  11   *
  12   *   Also replaces code for PNG images specified as backgrounds via:
  13   *   background-image: url('image.png'); When using PNG images in the background,
  14   *   there is no need to use a spacer.gif image.
  15   *
  16   *   @param  $x  String containing the content to search and replace in.
  17   *   @result Returns the modified string.
  18   */
  19   function replacePngTags($x,$spacer="images/spacer.gif")
  20   {
  21       // make sure that we are only replacing for the Windows versions of Internet
  22       // Explorer 5+, and not Opera identified as MSIE
  23       $msie='/msie\s([5-9])\.?[0-9]*.*(win)/i';
  24       $opera='/opera\s+[0-9]+/i';
  25       if(!isset($_SERVER['HTTP_USER_AGENT']) ||
  26           !preg_match($msie,$_SERVER['HTTP_USER_AGENT']) ||
  27           preg_match($opera,$_SERVER['HTTP_USER_AGENT']))
  28           return $x;
  29      
  30       // find all the png images in backgrounds
  31       preg_match_all('/background-image:\s*url\(\'(.*\.png)\'\);/Uis',$x,$background);
  32       for($i=0;$i<count($background[0]);$i++){
  33           // simply replace:
  34           //  "background-image: url('image.png');"
  35           // with:
  36           //  "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(
  37           //      enabled=true, sizingMethod=scale src='image.png');"
  38           // haven't tested to see if background-repeat styles work...
  39           $x=str_replace($background[0][$i],'filter:progid:DXImageTransform.'.
  40                   'Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale'.
  41                   ' src=\''.$background[1][$i].'\');',$x);
  42       }
  43  
  44       // OK, time to find all the IMG tags with ".png" in them
  45       preg_match_all('/(<img.*\.png.*>|<input.*type=([\'"])image\\2.*\.png.*>)/Uis',$x,$images);
  46       while(list($imgnum,$v)=@each($images[0])){
  47           $original=$v;
  48           $atts=''; $width=0; $height=0;
  49           // If the size is defined by styles, find
  50           preg_match_all('/style=".*(width: ([0-9]+))px.*'.
  51                           '(height: ([0-9]+))px.*"/Ui',$v,$arr2);
  52           if(is_array($arr2) && count($arr2[0])){
  53               // size was defined by styles, get values
  54               $width=$arr2[2][0];
  55               $height=$arr2[4][0];
  56           }
  57           // size was not defined by styles, get values
  58           preg_match_all('/width=\"?([0-9]+)\"?/i',$v,$arr2);
  59           if(is_array($arr2) && count($arr2[0])){
  60               $width=$arr2[1][0];
  61           }
  62           preg_match_all('/height=\"?([0-9]+)\"?/i',$v,$arr2);
  63           if(is_array($arr2) && count($arr2[0])){
  64               $height=$arr2[1][0];
  65           }
  66           preg_match_all('/src=\"([^\"]+\.png)\"/i',$v,$arr2);
  67           if(isset($arr2[1][0]) && !empty($arr2[1][0]))
  68               $image=$arr2[1][0];
  69           else
  70               $image=NULL;
  71  
  72           // We do this so that we can put our spacer.gif image in the same
  73           // directory as the image
  74           $tmp=split('[\\/]',$image);
  75           array_pop($tmp);
  76           $image_path=join('/',$tmp);
  77           if(strlen($image_path)) $image_path.='/';
  78  
  79           // end quote is already supplied by originial src attribute
  80           $replace_src_with=$spacer.'" style="width: '.$width.
  81               'px; height: '.$height.'px; filter: progid:DXImageTransform.'.
  82               'Microsoft.AlphaImageLoader(src=\''.$image.'\', sizingMethod='.
  83               '\'scale\')';
  84          
  85           // now create the new tag from the old
  86           $new_tag=str_replace($image,$replace_src_with,$original);
  87          
  88           // now place the new tag into the content
  89           $x=str_replace($original,$new_tag,$x);
  90       }
  91       return $x;
  92   }
  93   ?>