<?php

/*
Plugin Name: nuzzaci Width Watcher
Plugin URI: http://www.nuzzaci.co.uk/2007/06/22/11:41:49
Description: This plugin will look through your posts before they get displayed and make sure that no images are wider than a desired maximum width (specify desired width in the nuzzaciWidthWatcher.php file). If an image is too wide, the plugin will add a height and width attribute to the image so that it will be displayed with the desired width while still keeping the correct ratio. It will ignore all images that already have a width or height attribute and/or if the size is less than the set maximum width. This plugin doesn't touch the original image or the database.
Version: 0.6
Author: nico nuzzaci
Author URI: http://www.nuzzaci.co.uk
*/

// Specify the desired maximum width here:
$nuzzaciWidthWatcher_maxWidth "425";

function 
nuzzaciWidthWatcher($content){
   global 
$nuzzaciWidthWatcher_maxWidth;

   
// The post
   
$postContent $content;

   
// Search pattern to find images
   
$searchPattern "/<img(.*?)src\=[\"|'](.*?)[\"|'](.*?)>/";

   
// See if the post contains any images
   
if(!preg_match($searchPattern$postContent)){

      
// If the post doesn't contain any images; our work here is done
      
return $postContent;

   }else{

      
// Seems like Mr preg_mactch found something, so let's locate all of them
      
preg_match_all($searchPattern$postContent$searchResults);

      
// Parse the result
      
foreach($searchResults[2] as $id => $src){

         
// Get image size
         
if((@$dreamhost getimagesize($src))){

            
// If getimagesize() works
            
list($width$height) = getimagesize($src);

         }else{

            
// If getimagesize failed, try to use curl
            
$ch curl_init();
            
$timeout 10;
            
curl_setopt ($chCURLOPT_URL$src);
            
curl_setopt ($chCURLOPT_RETURNTRANSFER1);
            
curl_setopt ($chCURLOPT_CONNECTTIMEOUT$timeout);
            
$file_contents curl_exec($ch);
            
curl_close($ch);

            
$imageData ImageCreateFromString($file_contents);
            
imagejpeg($imageData"temp.jpg",100);

            list(
$width$height) = getimagesize("temp.jpg");
         }

         
// Values for new image size
         
$maxWidth $nuzzaciWidthWatcher_maxWidth;
         
$maxHeight $height;

         
// Exlude images with width or height attribute
         
if(preg_match("/(width|height)\=\"/"$searchResults[0][$id])){
            continue;
         }

         
// Exlude images that are the correct width or smaller
         
if($width <= $maxWidth){
            continue;
         }

         
// Calulate ratio
         
$heightScale $height/$maxHeight;
         
$widthScale $width/$maxWidth;

         if(
$heightScale>$widthScale){
            
// Portrait
            
$newWidth round($width * (1/$heightScale));
            
$newHeight round($height * (1/$heightScale));
         }else{
            
// Landscape
            
$newWidth round($width * (1/$widthScale));
            
$newHeight round($height * (1/$widthScale));
         }

         
$newWidthAndHeight " width=\"".$newWidth."\" height=\"".$newHeight."\"";

         
// Find and replace
         
$find $searchResults[0][$id];
         
$replace "<img".$searchResults[1][$id]."src=\"".$searchResults[2][$id]."\"".$newWidthAndHeight."".$searchResults[3][$id].">";
         
$postContent str_replace($find$replace$postContent);

      }
   }

   return 
$postContent;
}


add_filter('the_content''nuzzaciWidthWatcher');

?>