1 ) or ( $ratioHeight > 1 ) ) { if ( $ratioWidth < $ratioHeight) { $destWidth = $srcWidth/$ratioHeight; $destHeight = $newHeight; } else { $destWidth = $newWidth; $destHeight = $srcHeight/$ratioWidth; } } else { $destWidth = $srcWidth; $destHeight = $srcHeight; } // according to the GD version installed on the server if ( $_POST['gd'] == 2 ) { // GD 2.0 or more recent -> good results (but slower) $destImage = imagecreatetruecolor( $destWidth, $destHeight); imagecopyresampled( $destImage, $srcImage, 0, 0, $coord['x'], $coord['y'], $destWidth,$destHeight,$srcWidth,$srcHeight ); } else { // GD prior to version 2 -> pretty bad results :-/ (but fast) $destImage = imagecreate( $destWidth, $destHeight); imagecopyresized( $destImage, $srcImage, 0, 0, $coord['x'], $coord['y'], $destWidth,$destHeight,$srcWidth,$srcHeight ); } if (($tndir = mkget_thumbnail_dir($dirname, $page['errors'])) == false) { return false; } $dest_file = $tndir.'/'.$conf['prefix_thumbnail']; $dest_file.= get_filename_wo_extension($filename); $dest_file.= '.'.$tn_ext; // creation and backup of final picture if (!is_writable($tndir)) { array_push($page['errors'], '['.$tndir.'] : '.l10n('no write access')); return false; } imagejpeg($destImage, $dest_file, $conf['tn_compression_level']); // freeing memory ressources imagedestroy( $srcImage ); imagedestroy( $destImage ); list($tn_width, $tn_height) = getimagesize($dest_file); $tn_size = floor(filesize($dest_file) / 1024).' KB'; $info = array( 'path' => $path, 'tn_file' => $dest_file, 'tn_width' => $tn_width, 'tn_height' => $tn_height, 'tn_size' => $tn_size ); return $info; } // error else { echo l10n('Picture unreachable or no support')." "; if ( isset( $extenstion ) ) { echo l10n('for the file format').' '.$extension; } else { echo l10n('for this file format'); } exit(); } } // Resize function for Upload Form function upload_square_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false) { if ($result !== false) { //someone hooked us - so we skip return $result; } if (is_imagick()) { return upload_square_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata); } else { return upload_square_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality); } } function upload_square_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality) { if (!function_exists('gd_info')) { return false; } // extension of the picture filename $extension = strtolower(get_extension($source_filepath)); $source_image = null; if (in_array($extension, array('jpg', 'jpeg'))) { $source_image = @imagecreatefromjpeg($source_filepath); } else if ($extension == 'png') { $source_image = @imagecreatefrompng($source_filepath); } else { die('unsupported file extension'); } $rotation = null; if (function_exists('imagerotate')) { $rotation = get_rotation_angle($source_filepath); } // width/height $source_width = imagesx($source_image); $source_height = imagesy($source_image); $coord = process_ratio($source_width, $source_height); $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation); // testing on height is useless in theory: if width is unchanged, there // should be no resize, because width/height ratio is not modified. if ($resize_dimensions['width'] == $source_width and $resize_dimensions['height'] == $source_height) { // the image doesn't need any resize! We just copy it to the destination copy($source_filepath, $destination_filepath); return true; } $destination_image = imagecreatetruecolor($resize_dimensions['width'], $resize_dimensions['height']); imagecopyresampled( $destination_image, $source_image, 0, 0, $coord['x'], $coord['y'], $resize_dimensions['width'], $resize_dimensions['height'], $source_width, $source_height ); // rotation occurs only on resized photo to avoid useless memory use if (isset($rotation)) { $destination_image = imagerotate($destination_image, $rotation, 0); } $extension = strtolower(get_extension($destination_filepath)); if ($extension == 'png') { imagepng($destination_image, $destination_filepath); } else { imagejpeg($destination_image, $destination_filepath, $quality); } // freeing memory ressources imagedestroy($source_image); imagedestroy($destination_image); // everything should be OK if we are here! return true; } function upload_square_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false) { // extension of the picture filename $extension = strtolower(get_extension($source_filepath)); if (!in_array($extension, array('jpg', 'jpeg', 'png'))) { die('[Imagick] unsupported file extension'); } $image = new Imagick($source_filepath); $rotation = get_rotation_angle($source_filepath); // width/height $source_width = $image->getImageWidth(); $source_height = $image->getImageHeight(); $coord = process_ratio($source_width, $source_height); $image->cropImage($source_width, $source_height, $coord['x'], $coord['y']); $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation); // testing on height is useless in theory: if width is unchanged, there // should be no resize, because width/height ratio is not modified. if ($resize_dimensions['width'] == $source_width and $resize_dimensions['height'] == $source_height) { // the image doesn't need any resize! We just copy it to the destination copy($source_filepath, $destination_filepath); return true; } $image->setImageCompressionQuality($quality); $image->setInterlaceScheme(Imagick::INTERLACE_LINE); if ($strip_metadata) { // we save a few kilobytes. For example a thumbnail with metadata // weights 25KB, without metadata 7KB. $image->stripImage(); } $image->resizeImage($resize_dimensions['width'], $resize_dimensions['height'], Imagick::FILTER_LANCZOS, 0.9); if (isset($rotation)) { $image->rotateImage(new ImagickPixel(), -$rotation); $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT); } $image->writeImage($destination_filepath); $image->destroy(); // everything should be OK if we are here! return true; } function process_ratio(&$source_width, &$source_height) { global $conf; if (!isset($conf['thumbnails_ratio']) or !preg_match('/^\d+:\d+$/', $conf['thumbnails_ratio'])) $conf['thumbnails_ratio'] = '1:1'; if (!isset($conf['thumbnails_ratio_orientation'])) $conf['thumbnails_ratio_orientation'] = true; $x = 0; $y = 0; if ($source_width >= $source_height or !$conf['thumbnails_ratio_orientation']) list($widthRatio, $heightRatio) = explode(':', $conf['thumbnails_ratio']); else list($heightRatio, $widthRatio) = explode(':', $conf['thumbnails_ratio']); $img_ratio = $source_width / $source_height; $dest_ratio = $widthRatio / $heightRatio; if($dest_ratio > $img_ratio) { $destHeight = ceil(($source_width * $heightRatio) / $widthRatio); $y = ceil(($source_height - $destHeight) / 2 ); $source_height = $destHeight ; } elseif ($dest_ratio < $img_ratio) { $destWidth = ceil(($source_height * $widthRatio) / $heightRatio); $x = ceil(($source_width - $destWidth) / 2 ); $source_width = $destWidth ; } return array('x' => $x, 'y' => $y); } add_event_handler('thumbnail_resize', 'thumbnail_square_resize', 40, 5); add_event_handler('upload_thumbnail_resize', 'upload_square_resize', 40, 7); ?>