source: extensions/square_thumbnails/trunk/functions.inc.php @ 9756

Last change on this file since 9756 was 9756, checked in by patdenice, 13 years ago

Compatibility with imagemagik

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