init
[garradin.git] / include / libs / template_lite / plugins / function.resize_image.php
1 <?/**
2 * Template-Lite {resize_image} function plugin Using Image Magic
3 *
4 * Type: function
5 * Name: resize_image
6 * Purpose: Outputs resized image based on values passed.
7 * Input:
8 * - img_src tag path
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
23 *
24 * Examples:<br>
25 * <pre>
26 * {resize_image img_src="/thumbnails/" directory="/html/mysite/ad_images/" thumbdir="/html/mysite/thumbnails/" filename="Myfile.jpg" xscale="150" yscale="200" thumbname="thumb_"}
27 * </pre>
28 *
29 * Output <img src="/thumbnails/thumb_Myfile.jpg" width="150" height="200" alt="image" border="0">
30 *
31 * Author: Rick Thomson rick@oznet.com
32 * Author: Mark Dickenson akapanamajack@sourceforge.net
33 */
34
35 // Calculate percentage between width and height
36 function tpl_function_resize_percent($maximum, $current)
37 {
38 return (real)(100 * ($maximum / $current));
39 }
40
41 function tpl_function_resize_unpercent($percent, $whole)
42 {
43 return (real)(($percent * $whole) / 100);
44 }
45
46 function tpl_function_resize_image($params, &$tpl)
47 {
48 extract($params);
49
50 if (empty($directory))
51 {
52 throw new Template_Exception("resize_image: missing 'directory' parameter", $tpl);
53 }
54
55 if (empty($thumbdir))
56 {
57 $thumbdir = $directory;
58 }
59
60 if (empty($filename))
61 {
62 throw new Template_Exception("resize_image: missing 'filename' parameter", $tpl);
63 }
64
65 if (empty($xscale))
66 {
67 $xscale = 2000;
68 }
69 $maximagewidth=$xscale;
70
71 if (empty($xscale))
72 {
73 $yscale = 2000;
74 }
75 $maximageheight=$yscale;
76
77 if (empty($alt))
78 {
79 $alt = "image";
80 }
81
82 if (empty($border))
83 {
84 $border = 0;
85 }
86
87 if (empty($daystokeep))
88 {
89 $daystokeep = 5;
90 }
91
92 if (!function_exists('gd_info'))
93 {
94 throw new Template_Exception("resize_image: the GD library is not installed", $tpl);
95 }
96
97 if(!file_exists($directory . $filename) && !empty($url) && function_exists('curl_init'))
98 {
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);
103 curl_exec ($ch);
104 curl_close ($ch);
105 fclose ($fp);
106 }
107
108 if(file_exists($directory . $filename))
109 {
110 $imageinfo = @getimagesize($directory . $filename);
111 if(empty($imageinfo))
112 {
113 return;
114 }
115
116 if ($returntype == 1)
117 {
118 $imagewidth = $imageinfo[0];
119 $imageheight = $imageinfo[1];
120
121 if($maximagewidth < $imagewidth) {
122 $imageheight = tpl_function_resize_unpercent(tpl_function_resize_percent($maximagewidth, $imagewidth), $imageheight);
123 $imagewidth = $maximagewidth;
124 }
125
126 if($maximageheight < $imageheight) {
127 $imagewidth = tpl_function_resize_unpercent(tpl_function_resize_percent($maximageheight, $imageheight), $imagewidth);
128 $imageheight = $maximageheight;
129 }
130 return "<img " . $class . " src=\"" . $img_src . $filename . "\" width=\"" . $imagewidth . "\" height=\"" . $imageheight . "\" alt=\"" . $alt . "\" border=\"" . $border . "\">";
131 }
132
133 if (empty($thumbname))
134 {
135 throw new Template_Exception("resize_image: missing 'thumbname' parameter", $tpl);
136 }
137
138 $now=urlencode(date("F j, Y, g:i a"));
139
140 $newimagepath = $thumbdir . $thumbname . $filename;
141
142 $newthumbnail = 0;
143 if(!file_exists($newimagepath))
144 {
145 copy($directory . $filename, $newimagepath);
146 $newthumbnail = 1;
147 }
148
149 $datechanged = date("j", time()) - date("j", filemtime($newimagepath));
150 if(($datechanged > -$daystokeep && $datechanged < $daystokeep) && $newthumbnail = 0)
151 {
152 // Do not rebuild
153 $imagewidth = $imageinfo[0];
154 $imageheight = $imageinfo[1];
155
156 if($maximagewidth < $imagewidth)
157 {
158 $imageheight = tpl_function_resize_unpercent(tpl_function_resize_percent($maximagewidth, $imagewidth), $imageheight);
159 $imagewidth = $maximagewidth;
160 }
161
162 if($maximageheight < $imageheight)
163 {
164 $imagewidth = tpl_function_resize_unpercent(tpl_function_resize_percent($maximageheight, $imageheight), $imagewidth);
165 $imageheight = $maximageheight;
166 }
167 }
168 else
169 {
170 // rebuild
171 copy($directory . $filename, $newimagepath);
172
173 $imagewidth = $imageinfo[0];
174 $imageheight = $imageinfo[1];
175
176 if($maximagewidth < $imagewidth)
177 {
178 $imageheight = tpl_function_resize_unpercent(tpl_function_resize_percent($maximagewidth, $imagewidth), $imageheight);
179 $imagewidth = $maximagewidth;
180 }
181
182 if($maximageheight < $imageheight)
183 {
184 $imagewidth = tpl_function_resize_unpercent(tpl_function_resize_percent($maximageheight, $imageheight), $imagewidth);
185 $imageheight = $maximageheight;
186 }
187 $imagewidth = round($imagewidth);
188 $imageheight = round($imageheight);
189 $scale = $imagewidth . "x" . $imageheight . "!";
190
191 if (empty($binpath))
192 {
193 if($imageinfo[2] == 1)
194 {
195 $sourceimage = imagecreatefromgif($directory . $filename);
196 }
197 elseif($imageinfo[2] == 2)
198 {
199 $sourceimage = imagecreatefromjpeg($directory . $filename);
200 }
201 elseif($imageinfo[2] == 3)
202 {
203 $sourceimage = imagecreatefrompng($directory . $filename);
204 }
205
206 $destinationimage = imagecreatetruecolor($imagewidth, $imageheight);
207 imagecopyresized($destinationimage, $sourceimage, 0, 0, 0, 0, $imagewidth, $imageheight, $imageinfo[0], $imageinfo[1]);
208 if($imageinfo[2] == 1)
209 {
210 imagegif($destinationimage, $newimagepath);
211 }
212 elseif($imageinfo[2] == 2)
213 {
214 imageJPEG($destinationimage, $newimagepath, 75);
215 }
216 elseif($imageinfo[2] == 3)
217 {
218 imagepng($destinationimage, $newimagepath);
219 }
220 imagedestroy($sourceimage);
221 imagedestroy($destinationimage);
222 }
223 else
224 {
225 if ($imageinfo[2] == 2)
226 {
227 system( $binpath . "mogrify -quality 75 -geometry $scale $newimagepath");
228 }
229 else
230 {
231 system( $binpath . "mogrify -geometry $scale $newimagepath");
232 }
233 }
234 }
235
236 return "<img " . $class . " src=\"" . $img_src . $thumbname . $filename . "?" . $now . "\" width=\"" . $imagewidth . "\" height=\"" . $imageheight . "\" alt=\"" . $alt . "\" border=\"" . $border . "\">";
237 }
238 }
239 ?>