image manipulation plugin

From: Date: Sun, 15 Dec 2002 05:34:16 +0000
Subject: image manipulation plugin
Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-6809@lists.php.net to get a copy of this message
So a problem that has been bugging me for over a year I have finally solved. What's the problem you ask? Say you have an image that's 1024x768 but you want to display it no larger than 650x650. However, you know that simply setting WIDTH=650 and HEIGHT=650 will result in a messed up image. Or, say you only want the width to be no larger than 650 and the height doesn't really matter, or vice versa. Or say you just want to display an image with the given height and width, or maybe you just want an easy way to display an image! I have written a plugin that will do all of the above. Here is the source code. I hope that it works for you all and whatnot. Feel free to email me on or off the mailing list with questions or suggestions. Have a nice day! Paul Lockaby <?php /* * Smarty plugin * -------------------------------------------------------------
* Type:     function
* Name:     image
* Version: 1.0
* Date:     Dec 14, 2002
* Author: Paul Lockaby <paul@dostuff.net> * Purpose: print the necessary image tags with optional
*           constraints
* Input:    url = the url for the picture
*           width = (optional) width
*           height = (optional) height
*           limit = (optional) [true|false] will resize image
*                   to above width and height only if it is
*                   larger than above width and height
*           border = (optional) size of border (default 0)
*           alt = (optional) alt text to display
* * Examples: {img url="http://www.yoursite.com/image.jpg"}
*           {img url="images/me.gif" alt="A picture of me!"}
*           {img url="picture.gif" width=500 height=400}
* ------------------------------------------------------------- */ function smarty_function_image($params, &$smarty) { extract($params); if (empty($url)) {
       $smarty->trigger_error("image: missing 'url' parameter");
       return;
} if (empty($alt)) {
       $alt = $url;
} $img = "<IMG SRC=\"".$url."\""; if (!empty($border)) {
       $img .= " BORDER=".$border;
} else {
       $img .= " BORDER=0";
} if (!empty($alt)) {
       $img .= " ALT=\"".$alt."\"";
} // 0 = width, 1 = height if ($size = @getimagesize($url)) {
       if (empty($limit) || $limit==false) {
           // only a width was specified; we will fill in the height
           if (!empty($width)) {
               $img .= " WIDTH=\"".$width."\" HEIGHT=\"".$size[1]."\"";
           }
           // only a height was specified; we will fill in the width
           if (!empty($height)) {
               $img .= " WIDTH=\"".$size[0]."\" HEIGHT=\"".$height."\"";
           }
           // neither a height nor a width was specified; we will fill in both
           if (empty($width) && empty($height)) {
               $img .= " ".$size[3];
           }
       } else {
           if ((!empty($width) && ($size[0] > $width)) || (!empty($height) && ($size[1] > $height))) {
               if (!empty($height) && !empty($width)) {
                   // compare the ratios to determine how much each dimension needs to be changed
                   // this will return the width if the height is set to specified
                   $bth_width = round($size[0]*($height/$size[1]));
                   // this will return the height if the width is set to specified
                   $bth_height = round($size[1]*($width/$size[0]));
                   // first we set the width to the max and see how big the height will be
                   if (!($bth_height > $height)) {
                       // returned height is acceptable (i.e. less than specified)
                       $fin1_height = $bth_height;
                       $fin1_width = $width;
                   }
                   // now we set the height to the max and see how big the width will be
                   if (!($bth_width > $width)) {
                       // returned width is acceptable (i.e. less than specified)
                       $fin2_height = $height;
                       $fin2_width = $bth_width;
                   }
                   // check to see if both of them went through
                   if (isset($fin1_height) && isset($fin1_width) && isset($fin2_height) && isset($fin2_width)) {
                       // now check the difference between abs($fin1_height-$fin1_width) and abs($fin2_height-$fin2_width)
                       // since we obviously want the larger image, take whichever one has the smaller difference
                       if (abs($fin1_height - $fin1_width) < abs($fin2_height - $fin2_width)) {
                           $new_height = $fin1_height;
                           $new_width = $fin1_width;
                       } else {
                           $new_height = $fin2_height;
                           $new_width = $fin2_width;
                       }
                   } else {
                       // since $new_height and $new_width weren't set above, we have to set them here
                       if (isset($fin1_height) && isset($fin1_width)) {
                           $new_height = $fin1_height;
                           $new_width = $fin1_width;
                       } else {
                           $new_height = $fin2_height;
                           $new_width = $fin2_width;
                       }
                   }
               } else {
                   // only a height or only a width was specified
                   // much easier
                   if (!empty($height)) {
                       // working with only a height now
                       $new_height = $height;
                       $new_width = round($size[0]*($height/$size[1]));
                   }
                   if (!empty($width)) {
                       // working with only a width now
                       $new_height = round($size[1]*($width/$size[0]));
                       $new_width = $width;
                   }
               }
               $img .= " HEIGHT=\"".$new_height."\" WIDTH=\"".$new_width."\"";
           } else {
               $img .= " ".$size[3];
           }
       }
} $img .= ">"; echo $img; } ?>

« previous php.smarty.general (#6809) next »