feature:2273

Ability to crop thumbnail (fixed size)

git-svn-id: http://piwigo.org/svn/trunk@10552 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
patdenice 2011-04-21 21:55:20 +00:00
parent f5ef4fddd7
commit d1eb25df09
5 changed files with 113 additions and 17 deletions

View file

@ -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()
{
@ -90,6 +90,16 @@ function get_upload_form_config()
'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,
'can_be_null' => false,
@ -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'))
{
@ -577,6 +589,12 @@ function pwg_image_resize_gd($source_filepath, $destination_filepath, $max_width
$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));
@ -647,6 +665,13 @@ function pwg_image_resize_im($source_filepath, $destination_filepath, $max_width
$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);
// testing on height is useless in theory: if width is unchanged, there
@ -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);

View file

@ -75,6 +75,8 @@ if (isset($_POST['submit']))
$fields[] = 'thumb_maxwidth';
$fields[] = 'thumb_maxheight';
$fields[] = 'thumb_quality';
$fields[] = 'thumb_crop';
$fields[] = 'thumb_follow_orientation';
foreach ($fields as $field)
{

View file

@ -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(){
<table>
<tr>
<th>{'Maximum Width'|@translate}</th>
<th><label for="thumb_crop">{'Crop'|@translate}</label></th>
<td><input type="checkbox" name="thumb_crop" id="thumb_crop" {$values.thumb_crop}></td>
</tr>
<tr id="thumb_follow_orientation_tr">
<th><label for="thumb_follow_orientation">{'Follow Orientation'|@translate}</label></th>
<td><input type="checkbox" name="thumb_follow_orientation" id="thumb_follow_orientation" {$values.thumb_follow_orientation}></td>
</tr>
<tr>
<th id="thumb_width_th">{'Maximum Width'|@translate}</th>
<td><input type="text" name="thumb_maxwidth" value="{$values.thumb_maxwidth}" size="4" maxlength="4"> {'pixels'|@translate}</td>
</tr>
<tr>
<th>{'Maximum Height'|@translate}</th>
<th id="thumb_height_th">{'Maximum Height'|@translate}</th>
<td><input type="text" name="thumb_maxheight" value="{$values.thumb_maxheight}" size="4" maxlength="4"> {'pixels'|@translate}</td>
</tr>
<tr>

View file

@ -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';
?>

View file

@ -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';
$lang['Crop'] = 'Retailler';
$lang['Width'] = 'Largeur';
$lang['Height'] = 'Hauteur';
$lang['Follow Orientation'] = "Respecter l'orientation";
?>