Browse Source

FIX croping function

Laurent Destailleur 3 years ago
parent
commit
0d8cf96f8f
2 changed files with 48 additions and 12 deletions
  1. 35 7
      htdocs/core/js/lib_photosresize.js
  2. 13 5
      htdocs/core/photos_resize.php

+ 35 - 7
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);
 };

+ 13 - 5
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 '<!-- Form to crop -->'."\n";
@@ -571,8 +576,11 @@ if (!empty($conf->use_javascript_ajax)) {
 		      <input type="hidden" id="file" name="file" value="'.dol_escape_htmltag($original_file).'" />
 		      <input type="hidden" id="action" name="action" value="confirm_crop" />
 		      <input type="hidden" id="product" name="product" value="'.dol_escape_htmltag($id).'" />
+		      <input type="hidden" id="dol_screenwidth" name="dol_screenwidth" value="'.$_SESSION['dol_screenwidth'].'" />
 		      <input type="hidden" id="refsizeforcrop" name="refsizeforcrop" value="'.$refsizeforcrop.'" />
-		      <input type="hidden" id="ratioforcrop" name="ratioforcrop" value="'.$ratioforcrop.'" /><!-- field used by core/lib/lib_photoresize.js -->
+		      <input type="hidden" id="ratioforcrop" name="ratioforcrop" value="'.$ratioforcrop.'" /><!-- value in field used by js/lib/lib_photoresize.js -->
+		      <input type="hidden" id="imagewidth" name="imagewidth" value="'.$width.'" /><!-- value in field used by js/lib/lib_photoresize.js -->
+		      <input type="hidden" id="imageheight" name="imageheight" value="'.$height.'" /><!-- value in field used by js/lib/lib_photoresize.js -->
 	          <input type="hidden" name="modulepart" value="'.dol_escape_htmltag($modulepart).'" />
 		      <input type="hidden" name="id" value="'.dol_escape_htmltag($id).'" />
 		      <br>