2 * Template-Lite {resize_image} function plugin Using Image Magic
6 * Purpose: Outputs resized image based on values passed.
9 * - directory = full directory path where images are located
10 * - thumbdir = Optional directory path to store thumbnail images. directory will be used if not supplied.
11 * - filename = Name of the file
12 * - xscale = Image max width size default 2000 px
13 * - yscale = Image max height size default 2000 px
14 * - thumbname = prefix name for new image, the thumb name is not needed if you are using a return type of 1
15 * - returntype = 1 - return image tag with full size image name and path but with height and width attributes adjusted
16 * 0 - resize image and store thumbnail in directory and return that thumbnail img tag. Default setting
17 * - url = Optional URL to download image from for resizing if the image doesn't exist in the image directory (CURL must be installed to use this feature)
18 * - binpath = Optional path to the ImageMagick mogrify command. If missing slower GD code will be used.
19 * - alt = Optional alt attribute for the img tag Default is "image"
20 * - border = Optional border attribute for img tag Default is 0
21 * - class = Optional class attribute for img tag
22 * - daystokeep = Optional number of days to cache the thumbnail image. Default is 5 days
26 * {resize_image img_src="/thumbnails/" directory="/html/mysite/ad_images/" thumbdir="/html/mysite/thumbnails/" filename="Myfile.jpg" xscale="150" yscale="200" thumbname="thumb_"}
29 * Output <img src="/thumbnails/thumb_Myfile.jpg" width="150" height="200" alt="image" border="0">
31 * Author: Rick Thomson rick@oznet.com
32 * Author: Mark Dickenson akapanamajack@sourceforge.net
35 // Calculate percentage between width and height
36 function tpl_function_resize_percent($maximum, $current)
38 return (real)(100 * ($maximum / $current));
41 function tpl_function_resize_unpercent($percent, $whole)
43 return (real)(($percent * $whole) / 100);
46 function tpl_function_resize_image($params, &$tpl)
50 if (empty($directory))
52 throw new Template_Exception("resize_image: missing 'directory' parameter", $tpl);
57 $thumbdir = $directory;
62 throw new Template_Exception("resize_image: missing 'filename' parameter", $tpl);
69 $maximagewidth=$xscale;
75 $maximageheight=$yscale;
87 if (empty($daystokeep))
92 if (!function_exists('gd_info'))
94 throw new Template_Exception("resize_image: the GD library is not installed", $tpl);
97 if(!file_exists($directory . $filename) && !empty($url) && function_exists('curl_init'))
99 $ch = curl_init ($url . $filename);
100 $fp = fopen ($directory . $filename, "w");
101 curl_setopt ($ch, CURLOPT_FILE
, $fp);
102 curl_setopt ($ch, CURLOPT_HEADER
, 0);
108 if(file_exists($directory . $filename))
110 $imageinfo = @getimagesize
($directory . $filename);
111 if(empty($imageinfo))
116 if ($returntype == 1)
118 $imagewidth = $imageinfo[0];
119 $imageheight = $imageinfo[1];
121 if($maximagewidth < $imagewidth) {
122 $imageheight = tpl_function_resize_unpercent(tpl_function_resize_percent($maximagewidth, $imagewidth), $imageheight);
123 $imagewidth = $maximagewidth;
126 if($maximageheight < $imageheight) {
127 $imagewidth = tpl_function_resize_unpercent(tpl_function_resize_percent($maximageheight, $imageheight), $imagewidth);
128 $imageheight = $maximageheight;
130 return "<img " . $class . " src=\"" . $img_src . $filename . "\" width=\"" . $imagewidth . "\" height=\"" . $imageheight . "\" alt=\"" . $alt . "\" border=\"" . $border . "\">";
133 if (empty($thumbname))
135 throw new Template_Exception("resize_image: missing 'thumbname' parameter", $tpl);
138 $now=urlencode(date("F j, Y, g:i a"));
140 $newimagepath = $thumbdir . $thumbname . $filename;
143 if(!file_exists($newimagepath))
145 copy($directory . $filename, $newimagepath);
149 $datechanged = date("j", time()) - date("j", filemtime($newimagepath));
150 if(($datechanged > -$daystokeep && $datechanged < $daystokeep) && $newthumbnail = 0)
153 $imagewidth = $imageinfo[0];
154 $imageheight = $imageinfo[1];
156 if($maximagewidth < $imagewidth)
158 $imageheight = tpl_function_resize_unpercent(tpl_function_resize_percent($maximagewidth, $imagewidth), $imageheight);
159 $imagewidth = $maximagewidth;
162 if($maximageheight < $imageheight)
164 $imagewidth = tpl_function_resize_unpercent(tpl_function_resize_percent($maximageheight, $imageheight), $imagewidth);
165 $imageheight = $maximageheight;
171 copy($directory . $filename, $newimagepath);
173 $imagewidth = $imageinfo[0];
174 $imageheight = $imageinfo[1];
176 if($maximagewidth < $imagewidth)
178 $imageheight = tpl_function_resize_unpercent(tpl_function_resize_percent($maximagewidth, $imagewidth), $imageheight);
179 $imagewidth = $maximagewidth;
182 if($maximageheight < $imageheight)
184 $imagewidth = tpl_function_resize_unpercent(tpl_function_resize_percent($maximageheight, $imageheight), $imagewidth);
185 $imageheight = $maximageheight;
187 $imagewidth = round($imagewidth);
188 $imageheight = round($imageheight);
189 $scale = $imagewidth . "x" . $imageheight . "!";
193 if($imageinfo[2] == 1)
195 $sourceimage = imagecreatefromgif($directory . $filename);
197 elseif($imageinfo[2] == 2)
199 $sourceimage = imagecreatefromjpeg($directory . $filename);
201 elseif($imageinfo[2] == 3)
203 $sourceimage = imagecreatefrompng($directory . $filename);
206 $destinationimage = imagecreatetruecolor($imagewidth, $imageheight);
207 imagecopyresized($destinationimage, $sourceimage, 0, 0, 0, 0, $imagewidth, $imageheight, $imageinfo[0], $imageinfo[1]);
208 if($imageinfo[2] == 1)
210 imagegif($destinationimage, $newimagepath);
212 elseif($imageinfo[2] == 2)
214 imageJPEG($destinationimage, $newimagepath, 75);
216 elseif($imageinfo[2] == 3)
218 imagepng($destinationimage, $newimagepath);
220 imagedestroy($sourceimage);
221 imagedestroy($destinationimage);
225 if ($imageinfo[2] == 2)
227 system( $binpath . "mogrify -quality 75 -geometry $scale $newimagepath");
231 system( $binpath . "mogrify -geometry $scale $newimagepath");
236 return "<img " . $class . " src=\"" . $img_src . $thumbname . $filename . "?" . $now . "\" width=\"" . $imagewidth . "\" height=\"" . $imageheight . "\" alt=\"" . $alt . "\" border=\"" . $border . "\">";