1 | <?php |
---|
2 | |
---|
3 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
4 | |
---|
5 | // Resize function for thumbnails creation page |
---|
6 | function thumbnail_square_resize($info, $path, $newWidth, $newHeight, $tn_ext) |
---|
7 | { |
---|
8 | global $conf, $lang, $page; |
---|
9 | |
---|
10 | if ($info !== false) |
---|
11 | { |
---|
12 | //someone hooked us - so we skip |
---|
13 | return $info; |
---|
14 | } |
---|
15 | |
---|
16 | if (!function_exists('gd_info')) |
---|
17 | { |
---|
18 | return; |
---|
19 | } |
---|
20 | |
---|
21 | $filename = basename($path); |
---|
22 | $dirname = dirname($path); |
---|
23 | |
---|
24 | // extension of the picture filename |
---|
25 | $extension = get_extension($filename); |
---|
26 | |
---|
27 | if (in_array($extension, array('jpg', 'JPG', 'jpeg', 'JPEG'))) |
---|
28 | { |
---|
29 | $srcImage = @imagecreatefromjpeg($path); |
---|
30 | } |
---|
31 | else if ($extension == 'png' or $extension == 'PNG') |
---|
32 | { |
---|
33 | $srcImage = @imagecreatefrompng($path); |
---|
34 | } |
---|
35 | else |
---|
36 | { |
---|
37 | unset($extension); |
---|
38 | } |
---|
39 | |
---|
40 | if ( isset( $srcImage ) ) |
---|
41 | { |
---|
42 | // width/height |
---|
43 | $srcWidth = imagesx( $srcImage ); |
---|
44 | $srcHeight = imagesy( $srcImage ); |
---|
45 | |
---|
46 | $coord = process_ratio($srcWidth, $srcHeight); |
---|
47 | |
---|
48 | $ratioWidth = $srcWidth/$newWidth; |
---|
49 | $ratioHeight = $srcHeight/$newHeight; |
---|
50 | |
---|
51 | // maximal size exceeded ? |
---|
52 | if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) |
---|
53 | { |
---|
54 | if ( $ratioWidth < $ratioHeight) |
---|
55 | { |
---|
56 | $destWidth = $srcWidth/$ratioHeight; |
---|
57 | $destHeight = $newHeight; |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
61 | $destWidth = $newWidth; |
---|
62 | $destHeight = $srcHeight/$ratioWidth; |
---|
63 | } |
---|
64 | } |
---|
65 | else |
---|
66 | { |
---|
67 | $destWidth = $srcWidth; |
---|
68 | $destHeight = $srcHeight; |
---|
69 | } |
---|
70 | |
---|
71 | // according to the GD version installed on the server |
---|
72 | if ( $_POST['gd'] == 2 ) |
---|
73 | { |
---|
74 | // GD 2.0 or more recent -> good results (but slower) |
---|
75 | $destImage = imagecreatetruecolor( $destWidth, $destHeight); |
---|
76 | imagecopyresampled( $destImage, $srcImage, 0, 0, $coord['x'], $coord['y'], |
---|
77 | $destWidth,$destHeight,$srcWidth,$srcHeight ); |
---|
78 | } |
---|
79 | else |
---|
80 | { |
---|
81 | // GD prior to version 2 -> pretty bad results :-/ (but fast) |
---|
82 | $destImage = imagecreate( $destWidth, $destHeight); |
---|
83 | imagecopyresized( $destImage, $srcImage, 0, 0, $coord['x'], $coord['y'], |
---|
84 | $destWidth,$destHeight,$srcWidth,$srcHeight ); |
---|
85 | } |
---|
86 | |
---|
87 | if (($tndir = mkget_thumbnail_dir($dirname, $page['errors'])) == false) |
---|
88 | { |
---|
89 | return false; |
---|
90 | } |
---|
91 | |
---|
92 | $dest_file = $tndir.'/'.$conf['prefix_thumbnail']; |
---|
93 | $dest_file.= get_filename_wo_extension($filename); |
---|
94 | $dest_file.= '.'.$tn_ext; |
---|
95 | |
---|
96 | // creation and backup of final picture |
---|
97 | if (!is_writable($tndir)) |
---|
98 | { |
---|
99 | array_push($page['errors'], '['.$tndir.'] : '.l10n('no write access')); |
---|
100 | return false; |
---|
101 | } |
---|
102 | imagejpeg($destImage, $dest_file, $conf['tn_compression_level']); |
---|
103 | // freeing memory ressources |
---|
104 | imagedestroy( $srcImage ); |
---|
105 | imagedestroy( $destImage ); |
---|
106 | |
---|
107 | list($tn_width, $tn_height) = getimagesize($dest_file); |
---|
108 | $tn_size = floor(filesize($dest_file) / 1024).' KB'; |
---|
109 | |
---|
110 | $info = array( 'path' => $path, |
---|
111 | 'tn_file' => $dest_file, |
---|
112 | 'tn_width' => $tn_width, |
---|
113 | 'tn_height' => $tn_height, |
---|
114 | 'tn_size' => $tn_size ); |
---|
115 | return $info; |
---|
116 | } |
---|
117 | // error |
---|
118 | else |
---|
119 | { |
---|
120 | echo l10n('Picture unreachable or no support')." "; |
---|
121 | if ( isset( $extenstion ) ) |
---|
122 | { |
---|
123 | echo l10n('for the file format').' '.$extension; |
---|
124 | } |
---|
125 | else |
---|
126 | { |
---|
127 | echo l10n('for this file format'); |
---|
128 | } |
---|
129 | exit(); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | // Resize function for Upload Form |
---|
135 | function upload_square_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false) |
---|
136 | { |
---|
137 | if ($result !== false) |
---|
138 | { |
---|
139 | //someone hooked us - so we skip |
---|
140 | return $result; |
---|
141 | } |
---|
142 | |
---|
143 | $extension = strtolower(get_extension($source_filepath)); |
---|
144 | |
---|
145 | if (is_imagick() and $extension != 'gif') |
---|
146 | { |
---|
147 | return upload_square_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata); |
---|
148 | } |
---|
149 | else |
---|
150 | { |
---|
151 | return upload_square_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | function upload_square_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality) |
---|
156 | { |
---|
157 | if (!function_exists('gd_info')) |
---|
158 | { |
---|
159 | return false; |
---|
160 | } |
---|
161 | |
---|
162 | $gd_info = gd_info(); |
---|
163 | |
---|
164 | // extension of the picture filename |
---|
165 | $extension = strtolower(get_extension($source_filepath)); |
---|
166 | |
---|
167 | $source_image = null; |
---|
168 | if (in_array($extension, array('jpg', 'jpeg'))) |
---|
169 | { |
---|
170 | $source_image = @imagecreatefromjpeg($source_filepath); |
---|
171 | } |
---|
172 | else if ($extension == 'png') |
---|
173 | { |
---|
174 | $source_image = @imagecreatefrompng($source_filepath); |
---|
175 | } |
---|
176 | elseif ($extension == 'gif' and $gd_info['GIF Read Support'] and $gd_info['GIF Create Support']) |
---|
177 | { |
---|
178 | $source_image = @imagecreatefromgif($source_filepath); |
---|
179 | } |
---|
180 | else |
---|
181 | { |
---|
182 | die('[GD] unsupported file extension'); |
---|
183 | } |
---|
184 | |
---|
185 | $rotation = null; |
---|
186 | if (function_exists('imagerotate')) |
---|
187 | { |
---|
188 | $rotation = get_rotation_angle($source_filepath); |
---|
189 | } |
---|
190 | |
---|
191 | // width/height |
---|
192 | $source_width = imagesx($source_image); |
---|
193 | $source_height = imagesy($source_image); |
---|
194 | |
---|
195 | $coord = process_ratio($source_width, $source_height, $rotation); |
---|
196 | |
---|
197 | $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation); |
---|
198 | |
---|
199 | // testing on height is useless in theory: if width is unchanged, there |
---|
200 | // should be no resize, because width/height ratio is not modified. |
---|
201 | if ($resize_dimensions['width'] == $source_width and $resize_dimensions['height'] == $source_height) |
---|
202 | { |
---|
203 | // the image doesn't need any resize! We just copy it to the destination |
---|
204 | copy($source_filepath, $destination_filepath); |
---|
205 | return true; |
---|
206 | } |
---|
207 | |
---|
208 | $destination_image = imagecreatetruecolor($resize_dimensions['width'], $resize_dimensions['height']); |
---|
209 | |
---|
210 | imagecopyresampled( |
---|
211 | $destination_image, |
---|
212 | $source_image, |
---|
213 | 0, |
---|
214 | 0, |
---|
215 | $coord['x'], |
---|
216 | $coord['y'], |
---|
217 | $resize_dimensions['width'], |
---|
218 | $resize_dimensions['height'], |
---|
219 | $source_width, |
---|
220 | $source_height |
---|
221 | ); |
---|
222 | |
---|
223 | // rotation occurs only on resized photo to avoid useless memory use |
---|
224 | if (isset($rotation)) |
---|
225 | { |
---|
226 | $destination_image = imagerotate($destination_image, $rotation, 0); |
---|
227 | } |
---|
228 | |
---|
229 | $extension = strtolower(get_extension($destination_filepath)); |
---|
230 | if ($extension == 'png') |
---|
231 | { |
---|
232 | imagepng($destination_image, $destination_filepath); |
---|
233 | } |
---|
234 | elseif ($extension == 'gif') |
---|
235 | { |
---|
236 | imagegif($destination_image, $destination_filepath); |
---|
237 | } |
---|
238 | else |
---|
239 | { |
---|
240 | imagejpeg($destination_image, $destination_filepath, $quality); |
---|
241 | } |
---|
242 | // freeing memory ressources |
---|
243 | imagedestroy($source_image); |
---|
244 | imagedestroy($destination_image); |
---|
245 | |
---|
246 | // everything should be OK if we are here! |
---|
247 | return true; |
---|
248 | } |
---|
249 | |
---|
250 | function upload_square_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false) |
---|
251 | { |
---|
252 | // extension of the picture filename |
---|
253 | $extension = strtolower(get_extension($source_filepath)); |
---|
254 | if (!in_array($extension, array('jpg', 'jpeg', 'png'))) |
---|
255 | { |
---|
256 | die('[Imagick] unsupported file extension'); |
---|
257 | } |
---|
258 | |
---|
259 | $image = new Imagick($source_filepath); |
---|
260 | |
---|
261 | // width/height |
---|
262 | $source_width = $image->getImageWidth(); |
---|
263 | $source_height = $image->getImageHeight(); |
---|
264 | |
---|
265 | $coord = process_ratio($source_width, $source_height); |
---|
266 | |
---|
267 | $image->cropImage($source_width, $source_height, $coord['x'], $coord['y']); |
---|
268 | |
---|
269 | $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height); |
---|
270 | |
---|
271 | // testing on height is useless in theory: if width is unchanged, there |
---|
272 | // should be no resize, because width/height ratio is not modified. |
---|
273 | if ($resize_dimensions['width'] == $source_width and $resize_dimensions['height'] == $source_height) |
---|
274 | { |
---|
275 | // the image doesn't need any resize! We just copy it to the destination |
---|
276 | copy($source_filepath, $destination_filepath); |
---|
277 | return true; |
---|
278 | } |
---|
279 | |
---|
280 | $image->setImageCompressionQuality($quality); |
---|
281 | $image->setInterlaceScheme(Imagick::INTERLACE_LINE); |
---|
282 | |
---|
283 | if ($strip_metadata) |
---|
284 | { |
---|
285 | // we save a few kilobytes. For example a thumbnail with metadata |
---|
286 | // weights 25KB, without metadata 7KB. |
---|
287 | $image->stripImage(); |
---|
288 | } |
---|
289 | |
---|
290 | $image->resizeImage($resize_dimensions['width'], $resize_dimensions['height'], Imagick::FILTER_LANCZOS, 0.9); |
---|
291 | |
---|
292 | $image->writeImage($destination_filepath); |
---|
293 | $image->destroy(); |
---|
294 | |
---|
295 | // everything should be OK if we are here! |
---|
296 | return true; |
---|
297 | } |
---|
298 | |
---|
299 | function process_ratio(&$source_width, &$source_height) |
---|
300 | { |
---|
301 | global $conf; |
---|
302 | |
---|
303 | if (!isset($conf['thumbnails_ratio']) or !preg_match('/^\d+:\d+$/', $conf['thumbnails_ratio'])) |
---|
304 | $conf['thumbnails_ratio'] = '1:1'; |
---|
305 | |
---|
306 | if (!isset($conf['thumbnails_ratio_orientation'])) |
---|
307 | $conf['thumbnails_ratio_orientation'] = true; |
---|
308 | |
---|
309 | $x = 0; |
---|
310 | $y = 0; |
---|
311 | |
---|
312 | if ($source_width >= $source_height or !$conf['thumbnails_ratio_orientation']) |
---|
313 | list($widthRatio, $heightRatio) = explode(':', $conf['thumbnails_ratio']); |
---|
314 | |
---|
315 | else |
---|
316 | list($heightRatio, $widthRatio) = explode(':', $conf['thumbnails_ratio']); |
---|
317 | |
---|
318 | $img_ratio = $source_width / $source_height; |
---|
319 | $dest_ratio = $widthRatio / $heightRatio; |
---|
320 | |
---|
321 | if($dest_ratio > $img_ratio) |
---|
322 | { |
---|
323 | $destHeight = round($source_width * $heightRatio / $widthRatio); |
---|
324 | $y = round(($source_height - $destHeight) / 2 ); |
---|
325 | $source_height = $destHeight; |
---|
326 | } |
---|
327 | elseif ($dest_ratio < $img_ratio) |
---|
328 | { |
---|
329 | $destWidth = round($source_height * $widthRatio / $heightRatio); |
---|
330 | $x = round(($source_width - $destWidth) / 2 ); |
---|
331 | $source_width = $destWidth; |
---|
332 | } |
---|
333 | |
---|
334 | return array('x' => $x, 'y' => $y); |
---|
335 | } |
---|
336 | |
---|
337 | add_event_handler('thumbnail_resize', 'thumbnail_square_resize', 40, 5); |
---|
338 | add_event_handler('upload_thumbnail_resize', 'upload_square_resize', 40, 7); |
---|
339 | |
---|
340 | ?> |
---|