aboutsummaryrefslogtreecommitdiffstats
path: root/admin
diff options
context:
space:
mode:
authorpatdenice <patdenice@piwigo.org>2011-04-21 21:55:20 +0000
committerpatdenice <patdenice@piwigo.org>2011-04-21 21:55:20 +0000
commitd1eb25df099fa74e725692f3d6335b5d35abfc4b (patch)
tree1edbc89a9973448bfd8e02f2c6b4169190d92540 /admin
parentf5ef4fddd7ce277f27ef4face7933fe5a7406200 (diff)
feature:2273
Ability to crop thumbnail (fixed size) git-svn-id: http://piwigo.org/svn/trunk@10552 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to '')
-rw-r--r--admin/include/functions_upload.inc.php76
-rw-r--r--admin/photos_add_settings.php4
-rw-r--r--admin/themes/default/template/photos_add_settings.tpl38
3 files changed, 103 insertions, 15 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(){
<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>