From d1eb25df099fa74e725692f3d6335b5d35abfc4b Mon Sep 17 00:00:00 2001 From: patdenice Date: Thu, 21 Apr 2011 21:55:20 +0000 Subject: feature:2273 Ability to crop thumbnail (fixed size) git-svn-id: http://piwigo.org/svn/trunk@10552 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin/include/functions_upload.inc.php | 76 +++++++++++++++++++--- admin/photos_add_settings.php | 4 +- .../default/template/photos_add_settings.tpl | 38 +++++++++-- language/en_UK/admin.lang.php | 4 ++ language/fr_FR/admin.lang.php | 8 ++- 5 files changed, 113 insertions(+), 17 deletions(-) diff --git a/admin/include/functions_upload.inc.php b/admin/include/functions_upload.inc.php index 98c17d53b..ce0fafeb4 100644 --- a/admin/include/functions_upload.inc.php +++ b/admin/include/functions_upload.inc.php @@ -25,7 +25,7 @@ include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); // add default event handler for image and thumbnail resize add_event_handler('upload_image_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 7); -add_event_handler('upload_thumbnail_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 7); +add_event_handler('upload_thumbnail_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 9); function get_upload_form_config() { @@ -89,6 +89,16 @@ function get_upload_form_config() 'can_be_null' => false, 'error_message' => l10n('The thumbnail image quality must be a number between %d and %d'), ), + + 'thumb_crop' => array( + 'default' => false, + 'can_be_null' => false, + ), + + 'thumb_follow_orientation' => array( + 'default' => true, + 'can_be_null' => false, + ), 'hd_keep' => array( 'default' => true, @@ -326,7 +336,9 @@ SELECT $conf['upload_form_thumb_maxwidth'], $conf['upload_form_thumb_maxheight'], $conf['upload_form_thumb_quality'], - true + true, + $conf['upload_form_thumb_crop'], + $conf['upload_form_thumb_follow_orientation'] ); $thumb_infos = pwg_image_infos($thumb_path); @@ -517,7 +529,7 @@ function get_resize_dimensions($width, $height, $max_width, $max_height, $rotati ); } -function pwg_image_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false) +function pwg_image_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false, $crop=false, $follow_orientation=true) { if ($result !== false) { @@ -529,15 +541,15 @@ function pwg_image_resize($result, $source_filepath, $destination_filepath, $max if (is_imagick() and $extension != 'gif') { - return pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata); + return pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata, $crop, $follow_orientation); } else { - return pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality); + return pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $crop, $follow_orientation); } } -function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality) +function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $crop=false, $follow_orientation=true) { if (!function_exists('gd_info')) { @@ -576,7 +588,13 @@ function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width // width/height $source_width = imagesx($source_image); $source_height = imagesy($source_image); - + + // Crop + if ($crop) + { + $coord = get_crop_coord($source_width, $source_height, $max_width, $max_height, $follow_orientation); + } + $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 @@ -595,8 +613,8 @@ function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width $source_image, 0, 0, - 0, - 0, + $crop ? $coord['x'] : 0, + $crop ? $coord['y'] : 0, $resize_dimensions['width'], $resize_dimensions['height'], $source_width, @@ -630,7 +648,7 @@ function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width return true; } -function pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false) +function pwg_image_resize_im($source_filepath, $destination_filepath, $max_width, $max_height, $quality, $strip_metadata=false, $crop=false, $follow_orientation=true) { // extension of the picture filename $extension = strtolower(get_extension($source_filepath)); @@ -646,6 +664,13 @@ function pwg_image_resize_im($source_filepath, $destination_filepath, $max_width // width/height $source_width = $image->getImageWidth(); $source_height = $image->getImageHeight(); + + // Crop + if ($crop) + { + $coord = get_crop_coord($source_width, $source_height, $max_width, $max_height, $follow_orientation); + $image->cropImage($source_width, $source_height, $coord['x'], $coord['y']); + } $resize_dimensions = get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation); @@ -727,6 +752,37 @@ function get_rotation_angle($source_filepath) return $rotation; } +function get_crop_coord(&$source_width, &$source_height, &$max_width, &$max_height, $follow_orientation) +{ + $x = 0; + $y = 0; + + if ($source_width < $source_height and $follow_orientation) + { + list($width, $height) = array($max_height, $max_width); + $max_width = $width; + $max_height = $height; + } + + $img_ratio = $source_width / $source_height; + $dest_ratio = $max_width / $max_height; + + if($dest_ratio > $img_ratio) + { + $destHeight = round($source_width * $max_height / $max_width); + $y = round(($source_height - $destHeight) / 2 ); + $source_height = $destHeight; + } + elseif ($dest_ratio < $img_ratio) + { + $destWidth = round($source_height * $max_width / $max_height); + $x = round(($source_width - $destWidth) / 2 ); + $source_width = $destWidth; + } + + return array('x' => $x, 'y' => $y); +} + function pwg_image_infos($path) { list($width, $height) = getimagesize($path); diff --git a/admin/photos_add_settings.php b/admin/photos_add_settings.php index 4c065705e..fd0db4c90 100644 --- a/admin/photos_add_settings.php +++ b/admin/photos_add_settings.php @@ -75,7 +75,9 @@ if (isset($_POST['submit'])) $fields[] = 'thumb_maxwidth'; $fields[] = 'thumb_maxheight'; $fields[] = 'thumb_quality'; - + $fields[] = 'thumb_crop'; + $fields[] = 'thumb_follow_orientation'; + foreach ($fields as $field) { $value = null; diff --git a/admin/themes/default/template/photos_add_settings.tpl b/admin/themes/default/template/photos_add_settings.tpl index c8cc19b84..3180ac024 100644 --- a/admin/themes/default/template/photos_add_settings.tpl +++ b/admin/themes/default/template/photos_add_settings.tpl @@ -1,10 +1,15 @@ -{footer_script}{literal} +{footer_script} +var width = '{'Width'|@translate}'; +var height = '{'Height'|@translate}'; +var max_width = '{'Maximum Width'|@translate}'; +var max_height = '{'Maximum Height'|@translate}'; + +{literal} jQuery(document).ready(function(){ function toggleResizeFields(prefix) { var checkbox = jQuery("#"+prefix+"_resize"); var needToggle = jQuery("input[name^="+prefix+"_]").not(checkbox).not(jQuery("#hd_keep")).parents('tr'); - if (jQuery(checkbox).is(':checked')) { needToggle.show(); @@ -21,12 +26,29 @@ jQuery(document).ready(function(){ } } + function toggleCropFields(prefix) { + if (jQuery("#"+prefix+"_crop").is(':checked')) { + jQuery("#"+prefix+"_width_th").text(width); + jQuery("#"+prefix+"_height_th").text(height); + jQuery("#"+prefix+"_follow_orientation_tr").show(); + } + else { + jQuery("#"+prefix+"_width_th").text(max_width); + jQuery("#"+prefix+"_height_th").text(max_height); + jQuery("#"+prefix+"_follow_orientation_tr").hide(); + } + + } + toggleResizeFields("websize"); jQuery("#websize_resize").click(function () {toggleResizeFields("websize")}); toggleResizeFields("hd"); jQuery("#hd_resize").click(function () {toggleResizeFields("hd")}); + toggleCropFields("thumb"); + jQuery("#thumb_crop").click(function () {toggleCropFields("thumb")}); + function toggleHdFields() { var checkbox = jQuery("#hd_keep"); var needToggle = jQuery("input[name^=hd_]").not(checkbox).parents('tr'); @@ -81,11 +103,19 @@ jQuery(document).ready(function(){ - + + + + + + + + + - + diff --git a/language/en_UK/admin.lang.php b/language/en_UK/admin.lang.php index a0e36b216..f79d3e052 100644 --- a/language/en_UK/admin.lang.php +++ b/language/en_UK/admin.lang.php @@ -825,4 +825,8 @@ $lang['Unable to dump database.'] = 'Unable to dump database.'; $lang['Some upgrades are available for extensions.'] = 'Some upgrades are available for extensions.'; $lang['Please wait...'] = 'Please wait...'; $lang['Ignore All'] = 'Ignore All'; +$lang['Crop'] = 'Crop'; +$lang['Width'] = 'Width'; +$lang['Height'] = 'Height'; +$lang['Follow Orientation'] = 'Follow Orientation'; ?> \ No newline at end of file diff --git a/language/fr_FR/admin.lang.php b/language/fr_FR/admin.lang.php index f66902c2c..84b524f1e 100644 --- a/language/fr_FR/admin.lang.php +++ b/language/fr_FR/admin.lang.php @@ -834,6 +834,10 @@ $lang['Unable to write new local directory.'] = 'Impossible d\'écrire le nouvea $lang['Unable to send template directory.'] = 'Impossible d\'envoyer le dossier template.'; $lang['Unable to dump database.'] = 'Impossible de sauvegarder la base de données.'; $lang['Some upgrades are available for extensions.'] = 'Des mises à jour sont disponibles pour les extensions.'; -$lang['Please wait...'] = 'Please wait...'; +$lang['Please wait...'] = 'Veuillez patienter...'; $lang['Ignore All'] = 'Tout ignorer'; -?> \ No newline at end of file +$lang['Crop'] = 'Retailler'; +$lang['Width'] = 'Largeur'; +$lang['Height'] = 'Hauteur'; +$lang['Follow Orientation'] = "Respecter l'orientation"; +?> -- cgit v1.2.3
{'Maximum Width'|@translate}
{'Maximum Width'|@translate} {'pixels'|@translate}
{'Maximum Height'|@translate}{'Maximum Height'|@translate} {'pixels'|@translate}