[5962] | 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 | |
---|
[9770] | 46 | $coord = process_ratio($srcWidth, $srcHeight); |
---|
[5962] | 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); |
---|
[9770] | 76 | imagecopyresampled( $destImage, $srcImage, 0, 0, $coord['x'], $coord['y'], |
---|
[5962] | 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); |
---|
[9770] | 83 | imagecopyresized( $destImage, $srcImage, 0, 0, $coord['x'], $coord['y'], |
---|
[5962] | 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 |
---|
[9757] | 135 | function upload_square_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false) |
---|
[5962] | 136 | { |
---|
| 137 | if ($result !== false) |
---|
| 138 | { |
---|
| 139 | //someone hooked us - so we skip |
---|
| 140 | return $result; |
---|
| 141 | } |
---|
| 142 | |
---|
[9953] | 143 | $extension = strtolower(get_extension($source_filepath)); |
---|
| 144 | |
---|
| 145 | if (is_imagick() and $extension != 'gif') |
---|
[9754] | 146 | { |
---|
[9756] | 147 | return upload_square_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata); |
---|
[9754] | 148 | } |
---|
| 149 | else |
---|
| 150 | { |
---|
[9756] | 151 | return upload_square_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality); |
---|
[9754] | 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
[9756] | 155 | function upload_square_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality) |
---|
[9754] | 156 | { |
---|
[5962] | 157 | if (!function_exists('gd_info')) |
---|
| 158 | { |
---|
| 159 | return false; |
---|
| 160 | } |
---|
| 161 | |
---|
[9953] | 162 | $gd_info = gd_info(); |
---|
| 163 | |
---|
[5962] | 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 | { |
---|
[9756] | 170 | $source_image = @imagecreatefromjpeg($source_filepath); |
---|
[5962] | 171 | } |
---|
| 172 | else if ($extension == 'png') |
---|
| 173 | { |
---|
[9756] | 174 | $source_image = @imagecreatefrompng($source_filepath); |
---|
[5962] | 175 | } |
---|
[9953] | 176 | elseif ($extension == 'gif' and $gd_info['GIF Read Support'] and $gd_info['GIF Create Support']) |
---|
| 177 | { |
---|
| 178 | $source_image = @imagecreatefromgif($source_filepath); |
---|
| 179 | } |
---|
[5962] | 180 | else |
---|
| 181 | { |
---|
[9953] | 182 | die('[GD] unsupported file extension'); |
---|
[5962] | 183 | } |
---|
[9754] | 184 | |
---|
| 185 | $rotation = null; |
---|
| 186 | if (function_exists('imagerotate')) |
---|
| 187 | { |
---|
| 188 | $rotation = get_rotation_angle($source_filepath); |
---|
| 189 | } |
---|
[5962] | 190 | |
---|
| 191 | // width/height |
---|
| 192 | $source_width = imagesx($source_image); |
---|
| 193 | $source_height = imagesy($source_image); |
---|
[9770] | 194 | |
---|
[9772] | 195 | $coord = process_ratio($source_width, $source_height, $rotation); |
---|
[5962] | 196 | |
---|
[9754] | 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) |
---|
[5962] | 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 | |
---|
[9754] | 208 | $destination_image = imagecreatetruecolor($resize_dimensions['width'], $resize_dimensions['height']); |
---|
[5962] | 209 | |
---|
| 210 | imagecopyresampled( |
---|
| 211 | $destination_image, |
---|
| 212 | $source_image, |
---|
| 213 | 0, |
---|
| 214 | 0, |
---|
[9770] | 215 | $coord['x'], |
---|
| 216 | $coord['y'], |
---|
[9754] | 217 | $resize_dimensions['width'], |
---|
| 218 | $resize_dimensions['height'], |
---|
[5962] | 219 | $source_width, |
---|
| 220 | $source_height |
---|
| 221 | ); |
---|
[9754] | 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 | } |
---|
[5962] | 228 | |
---|
[9754] | 229 | $extension = strtolower(get_extension($destination_filepath)); |
---|
| 230 | if ($extension == 'png') |
---|
| 231 | { |
---|
| 232 | imagepng($destination_image, $destination_filepath); |
---|
| 233 | } |
---|
[9953] | 234 | elseif ($extension == 'gif') |
---|
| 235 | { |
---|
| 236 | imagegif($destination_image, $destination_filepath); |
---|
| 237 | } |
---|
[9754] | 238 | else |
---|
| 239 | { |
---|
| 240 | imagejpeg($destination_image, $destination_filepath, $quality); |
---|
| 241 | } |
---|
[5962] | 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 | |
---|
[9756] | 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 | |
---|
[10386] | 265 | $coord = process_ratio($source_width, $source_height); |
---|
[9770] | 266 | |
---|
| 267 | $image->cropImage($source_width, $source_height, $coord['x'], $coord['y']); |
---|
[9756] | 268 | |
---|
[10386] | 269 | $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height); |
---|
[9756] | 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 | |
---|
[10386] | 299 | function process_ratio(&$source_width, &$source_height) |
---|
[9770] | 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 | |
---|
[9771] | 306 | if (!isset($conf['thumbnails_ratio_orientation'])) |
---|
| 307 | $conf['thumbnails_ratio_orientation'] = true; |
---|
| 308 | |
---|
[9770] | 309 | $x = 0; |
---|
| 310 | $y = 0; |
---|
| 311 | |
---|
[10386] | 312 | if ($source_width >= $source_height or !$conf['thumbnails_ratio_orientation']) |
---|
[9770] | 313 | list($widthRatio, $heightRatio) = explode(':', $conf['thumbnails_ratio']); |
---|
[9953] | 314 | |
---|
[9770] | 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 | { |
---|
[10386] | 323 | $destHeight = round($source_width * $heightRatio / $widthRatio); |
---|
| 324 | $y = round(($source_height - $destHeight) / 2 ); |
---|
| 325 | $source_height = $destHeight; |
---|
[9770] | 326 | } |
---|
| 327 | elseif ($dest_ratio < $img_ratio) |
---|
| 328 | { |
---|
[10386] | 329 | $destWidth = round($source_height * $widthRatio / $heightRatio); |
---|
| 330 | $x = round(($source_width - $destWidth) / 2 ); |
---|
| 331 | $source_width = $destWidth; |
---|
[9770] | 332 | } |
---|
| 333 | |
---|
| 334 | return array('x' => $x, 'y' => $y); |
---|
| 335 | } |
---|
| 336 | |
---|
[5962] | 337 | add_event_handler('thumbnail_resize', 'thumbnail_square_resize', 40, 5); |
---|
[9757] | 338 | add_event_handler('upload_thumbnail_resize', 'upload_square_resize', 40, 7); |
---|
[5962] | 339 | |
---|
| 340 | ?> |
---|