| 156 | | return pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata); |
| 157 | | } |
| 158 | | else |
| 159 | | { |
| 160 | | return pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality); |
| | 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); |
| | 260 | function 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 | |