From 1144c8e105f22524212d618068873a813df96248 Mon Sep 17 00:00:00 2001 From: Mikael Roos Date: Mon, 15 Dec 2014 17:14:06 +0100 Subject: [PATCH] updating strategies to use for no-upscale --- CImage.php | 75 ++++++++++++++++++++----- webroot/img.php | 12 ++++ webroot/test/test_option-no-upscale.php | 14 ++++- 3 files changed, 84 insertions(+), 17 deletions(-) diff --git a/CImage.php b/CImage.php index 9880fef..a2c6a42 100644 --- a/CImage.php +++ b/CImage.php @@ -234,7 +234,7 @@ class CImage /** * Always upscale images, even if they are smaller than target image. */ - const UPSCALE_DEFAULT = true; + const UPSCALE_DEFAULT = true; private $upscale = self::UPSCALE_DEFAULT; @@ -1034,12 +1034,12 @@ class CImage $convolve = null; if ($this->convolve) { - $convolve = 'convolve' . preg_replace('/[^a-zA-Z0-9]/', '', $this->convolve); + $convolve = '_conv' . preg_replace('/[^a-zA-Z0-9]/', '', $this->convolve); } $upscale = null; if ($this->upscale !== self::UPSCALE_DEFAULT) { - $upscale = 'nu'; + $upscale = '_nu'; } $subdir = str_replace('/', '-', dirname($this->imageSrc)); @@ -1331,6 +1331,23 @@ class CImage if (!$this->upscale && ($this->width < $this->newWidth || $this->height < $this->newHeight)) { $this->log("Resizing - smaller image, do not upscale."); + + $cropX = round(($this->cropWidth/2) - ($this->newWidth/2)); + $cropY = round(($this->cropHeight/2) - ($this->newHeight/2)); + + $posX = 0; + $posY = 0; + + if ($this->newWidth > $this->width) { + $posX = round(($this->newWidth - $this->width) / 2); + } + + if ($this->newHeight > $this->height) { + $posY = round(($this->newHeight - $this->height) / 2); + } + + $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); + imagecopy($imageResized, $this->image, $posX, $posY, $cropX, $cropY, $this->newWidth, $this->newHeight); } else { $cropX = round(($this->cropWidth/2) - ($this->newWidth/2)); $cropY = round(($this->cropHeight/2) - ($this->newHeight/2)); @@ -1338,10 +1355,11 @@ class CImage $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); imagecopyresampled($imgPreCrop, $this->image, 0, 0, 0, 0, $this->cropWidth, $this->cropHeight, $this->width, $this->height); imagecopy($imageResized, $imgPreCrop, 0, 0, $cropX, $cropY, $this->newWidth, $this->newHeight); - $this->image = $imageResized; - $this->width = $this->newWidth; - $this->height = $this->newHeight; } + + $this->image = $imageResized; + $this->width = $this->newWidth; + $this->height = $this->newHeight; } else if ($this->fillToFit) { @@ -1361,34 +1379,63 @@ class CImage $posY = round(($this->newHeight - $this->fillHeight) / 2); } - if (!$this->upscale && ($this->width < $this->newWidth || $this->height < $this->newHeight)) { + if (!$this->upscale + && ($this->width < $this->newWidth || $this->height < $this->newHeight) + ) { $this->log("Resizing - smaller image, do not upscale."); $posX = round(($this->fillWidth - $this->width) / 2); $posY = round(($this->fillHeight - $this->height) / 2); $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); imagecopy($imageResized, $this->image, $posX, $posY, 0, 0, $this->fillWidth, $this->fillHeight); - $this->image = $imageResized; - $this->width = $this->newWidth; - $this->height = $this->newHeight; } else { $imgPreFill = $this->CreateImageKeepTransparency($this->fillWidth, $this->fillHeight); $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); imagecopyresampled($imgPreFill, $this->image, 0, 0, 0, 0, $this->fillWidth, $this->fillHeight, $this->width, $this->height); imagecopy($imageResized, $imgPreFill, $posX, $posY, 0, 0, $this->fillWidth, $this->fillHeight); - $this->image = $imageResized; - $this->width = $this->newWidth; - $this->height = $this->newHeight; } + + $this->image = $imageResized; + $this->width = $this->newWidth; + $this->height = $this->newHeight; } else if (!($this->newWidth == $this->width && $this->newHeight == $this->height)) { // Resize it $this->log("Resizing, new height and/or width"); - if (!$this->upscale && ($this->width < $this->newWidth || $this->height < $this->newHeight)) { + if (!$this->upscale + && ($this->width < $this->newWidth || $this->height < $this->newHeight) + ) { $this->log("Resizing - smaller image, do not upscale."); + + if (!$this->keepRatio) { + $this->log("Resizing - stretch to fit selected."); + + $posX = 0; + $posY = 0; + $cropX = 0; + $cropY = 0; + + if ($this->newWidth > $this->width && $this->newHeight > $this->height) { + $posX = round(($this->newWidth - $this->width) / 2); + $posY = round(($this->newHeight - $this->height) / 2); + } else if ($this->newWidth > $this->width) { + $posX = round(($this->newWidth - $this->width) / 2); + $cropY = round(($this->height - $this->newHeight) / 2); + } else if ($this->newHeight > $this->height) { + $posY = round(($this->newHeight - $this->height) / 2); + $cropX = round(($this->width - $this->newWidth) / 2); + } + + //$this->log("posX=$posX, posY=$posY, cropX=$cropX, cropY=$cropY."); + $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); + imagecopy($imageResized, $this->image, $posX, $posY, $cropX, $cropY, $this->newWidth, $this->newHeight); + $this->image = $imageResized; + $this->width = $this->newWidth; + $this->height = $this->newHeight; + } } else { $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); imagecopyresampled($imageResized, $this->image, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height); diff --git a/webroot/img.php b/webroot/img.php index 3d7a942..b5aec20 100644 --- a/webroot/img.php +++ b/webroot/img.php @@ -550,9 +550,21 @@ if ($verbose) { unset($query['nc']); unset($query['json']); $url1 = '?' . htmlentities(urldecode(http_build_query($query))); + $url2 = '?' . urldecode(http_build_query($query)); echo <<$url1
+

+
+
+
 EOD;
 }
 
diff --git a/webroot/test/test_option-no-upscale.php b/webroot/test/test_option-no-upscale.php
index 96f8970..cc691f7 100644
--- a/webroot/test/test_option-no-upscale.php
+++ b/webroot/test/test_option-no-upscale.php
@@ -22,7 +22,7 @@ $images = array(
 
 
 // For each image, apply these testcases
-$nc = null; //"&nc"; //null; //&nc'; 
+$nc = "&nc"; //null; //&nc'; 
 $testcase = array(
     $nc . '&w=600',
     $nc . '&w=600&no-upscale',
@@ -30,10 +30,18 @@ $testcase = array(
     $nc . '&h=400&no-upscale',
     $nc . '&w=600&h=400',
     $nc . '&w=600&h=400&no-upscale',
-    $nc . '&w=600&h=400&stretch',
-    $nc . '&w=600&h=400&no-upscale&stretch',
+    $nc . '&w=700&h=400&stretch',
+    $nc . '&w=700&h=400&no-upscale&stretch',
+    $nc . '&w=700&h=200&stretch',
+    $nc . '&w=700&h=200&no-upscale&stretch',
+    $nc . '&w=300&h=400&stretch',
+    $nc . '&w=300&h=400&no-upscale&stretch',
     $nc . '&w=600&h=400&crop-to-fit',
     $nc . '&w=600&h=400&no-upscale&crop-to-fit',
+    $nc . '&w=600&h=200&crop-to-fit',
+    $nc . '&w=600&h=200&no-upscale&crop-to-fit',
+    $nc . '&w=300&h=400&crop-to-fit',
+    $nc . '&w=300&h=400&no-upscale&crop-to-fit',
     $nc . '&w=600&h=400&fill-to-fit',
     $nc . '&w=600&h=400&no-upscale&fill-to-fit',
 /*