From 0d8cf96f8f92aa2424497dd4f99e68cb9ea003a3 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 2 Jan 2022 20:54:35 +0100 Subject: [PATCH] FIX croping function --- htdocs/core/js/lib_photosresize.js | 42 +++++++++++++++++++++++++----- htdocs/core/photos_resize.php | 18 +++++++++---- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/htdocs/core/js/lib_photosresize.js b/htdocs/core/js/lib_photosresize.js index 99c7c36c29e..38c508f90b3 100644 --- a/htdocs/core/js/lib_photosresize.js +++ b/htdocs/core/js/lib_photosresize.js @@ -32,12 +32,40 @@ function updateCoords(c) { //alert(parseInt(jQuery("#ratioforcrop").val())); ratio=1; - if (parseInt(jQuery("#ratioforcrop").val()) > 0) ratio = parseInt(jQuery("#ratioforcrop").val()); + imagewidth=0; + imageheight=0; + + console.log(c); + + if (parseInt(jQuery("#ratioforcrop").val()) > 1) { + ratio = parseInt(jQuery("#ratioforcrop").val()); + if (parseInt(jQuery("#imagewidth").val()) > 0) imagewidth = parseInt(jQuery("#imagewidth").val()); + if (parseInt(jQuery("#imageheight").val()) > 0) imageheight = parseInt(jQuery("#imageheight").val()); + } + + x = Math.floor(c.x * ratio); + y = Math.floor(c.y * ratio); + x2 = Math.ceil(c.x2 * ratio); + y2 = Math.ceil(c.y2 * ratio); + console.log("x="+x+" y="+y+" x2="+x2+" y2="+y2+" imageheight="+imageheight+" ratio="+ratio); + if (imagewidth > 0 && x > imagewidth) { + x = imagewidth; + } + if (imageheight > 0 && y > imageheight) { + y = imageheight; + } + if (imagewidth > 0 && x2 > imagewidth) { + x2 = imagewidth; + } + if (imageheight > 0 && y2 > imageheight) { + y2 = imageheight; + } + //console.log(ratio); - jQuery('#x').val(Math.ceil(c.x * ratio)); - jQuery('#y').val(Math.ceil(c.y * ratio)); - jQuery('#x2').val(Math.ceil(c.x2 * ratio)); - jQuery('#y2').val(Math.ceil(c.y2 * ratio)); - jQuery('#w').val(Math.ceil(c.w * ratio)); - jQuery('#h').val(Math.ceil(c.h * ratio)); + jQuery('#x').val(x); + jQuery('#y').val(y); + jQuery('#x2').val(x2); + jQuery('#y2').val(y2); + jQuery('#w').val(x2-x); + jQuery('#h').val(y2-y); }; diff --git a/htdocs/core/photos_resize.php b/htdocs/core/photos_resize.php index ac821600fbe..e5c652d7d51 100644 --- a/htdocs/core/photos_resize.php +++ b/htdocs/core/photos_resize.php @@ -536,11 +536,16 @@ if (!empty($conf->use_javascript_ajax)) { $widthforcrop = $width; $refsizeforcrop = 'orig'; $ratioforcrop = 1; + // If image is too large, we use another scale. - if (!empty($_SESSION['dol_screenwidth']) && ($widthforcrop > round($_SESSION['dol_screenwidth'] / 2))) { - $ratioforcrop = 2; - $widthforcrop = round($_SESSION['dol_screenwidth'] / $ratioforcrop); - $refsizeforcrop = 'screenwidth'; + if (!empty($_SESSION['dol_screenwidth'])) { + $widthforcroporigin = $widthforcrop; + while ($widthforcrop > round($_SESSION['dol_screenwidth'] / 1.5)) { + //var_dump($widthforcrop.' '.round($_SESSION['dol_screenwidth'] / 1.5)); + $ratioforcrop = 2 * $ratioforcrop; + $widthforcrop = floor($widthforcroporigin / $ratioforcrop); + $refsizeforcrop = 'screenwidth'; + } } print ''."\n"; @@ -571,8 +576,11 @@ if (!empty($conf->use_javascript_ajax)) { + - + + +