1
0
mirror of https://github.com/mosbth/cimage.git synced 2025-08-01 05:50:12 +02:00

passed test for resize strategies including no upscale using test/option-no-upscale

This commit is contained in:
Mikael Roos
2016-06-01 13:57:48 +02:00
parent 00ab2d7ca6
commit 74428f066c
6 changed files with 67 additions and 35 deletions

View File

@@ -1,6 +1,6 @@
The MIT License (MIT)
Copyright (c) 2012 - 2016 Mikael Roos, me@mikaelroos.se
Copyright (c) 2012 - 2016 Mikael Roos, https://mikaelroos.se, mos@dbwebb.se
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2542,10 +2542,8 @@ class CImage
$this->log("Output format is: $format");
if (!$this->verbose && $format == 'json') {
#header('Content-type: application/json');
#echo $this->json($file);
return serialize(["hello", 1]);
#return;
header('Content-type: application/json');
echo $this->json($file);
exit;
} elseif ($format == 'ascii') {
header('Content-type: text/plain');

View File

@@ -253,14 +253,17 @@ class CImageResizer
* @param float &$current value to check and change.
* @param float $orig value to check against.
*
* @return float as the value respected by the upscale setting.
* @return boolean true if upscaled changed values, else false.
*/
public function respectUpscale(&$current, $orig)
{
if (!$this->upscale && $current > $orig) {
$this->log("# Disallowed upscale of $orig to $current");
$this->log("# Disallowed upscale to $current ($orig)");
$current = $orig;
return true;
}
return false;
}
@@ -553,24 +556,54 @@ class CImageResizer
} elseif ($rs === CImageResizer::STRETCH && $both) {
// Stretch to fit, leave as is
// Ignores respectUpscale by intention
$this->log(" Stretch, leave as is");
$this->log(" Stretch");
// respectUpscale
$dw = $tw;
$dh = $th;
$this->respectUpscale($dw, $sw);
$this->respectUpscale($dh, $sh);
$dx = ($tw - $dw) / 2;
$dy = ($th - $dh) / 2;
$this->log(" Destination area dx=$dx, dy=$dy, dw=$dw, dh=$dh");
} elseif ($rs === CImageResizer::CROP_TO_FIT && $both) {
// Crop to fit image in box
// Ignores respectUpscale by intention
$this->log(" Crop to fit, ratio target=$ratio, source=$ar");
// Respect upscale
$dw = $tw;
$dh = $th;
$this->respectUpscale($dw, $sw);
$this->respectUpscale($dh, $sh);
$dx = ($tw - $dw) / 2;
$dy = ($th - $dh) / 2;
// Manage landscape/portrait
if ($ratio > $ar) {
$ch = $sw / $ratio;
$cy = ($sh - $ch) / 2;
$this->log(" Crop by cy=$cy ch=$ch");
} elseif ($ratio < $ar) {
$cw = $sh * $ratio;
$cx = ($sw - $cw) / 2;
$this->log(" Crop by cx=$cx cw=$cw");
}
// Update crop when no upscale
if (!$this->upscale && $dx) {
$cy = $th < $sh ? ($sh - $th) / 2 : 0;
$ch = $dh;
}
if (!$this->upscale && $dy) {
$cx = $tw < $sw ? ($sw - $tw) / 2 : 0;
$cw = $dw;
}
$this->log(" Parts cx=$cx, cy=$cy, cw=$cw, ch=$ch");
$this->log(" Destination area dx=$dx, dy=$dy, dw=$dw, dh=$dh");
} elseif ($rs === CImageResizer::FILL_TO_FIT && $both) {
@@ -579,6 +612,7 @@ class CImageResizer
$dw = $tw;
$dh = $th;
// Manage landscape/portrait
if ($ratio > $ar) {
$dw = $th * $ar;
$dh = $th;
@@ -586,14 +620,13 @@ class CImageResizer
$dw = $tw;
$dh = $tw / $ar;
}
$this->respectUpscale($dw, $sw);
$this->respectUpscale($dh, $sh);
$dx = ($tw - $dw) / 2;
$dy = ($th - $dh) / 2;
$this->log(" Destination area dx=$dx, dy=$dy, dw=$dw, dh=$dh");
}
// All done, sum it up

View File

@@ -5,3 +5,9 @@ Use resize fill-to-fit, crop-to-fit, stretch without width and height should res
## Integration tests
Checkout a version using composer and execute all tests, prefarably on cimage.se.
## Reference images
Create a bunch of reference images to compare between versions. Save together with how it was created.

BIN
webroot/img/tower.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

View File

@@ -18,43 +18,38 @@ $description = "Do not upscale image when original image (slice) is smaller than
$images = array(
'car.png',
'apple.jpg',
'tower.jpg',
);
// For each image, apply these testcases
$nc = "&bgc=660000"; //null; //"&nc"; //null; //&nc';
$nc = "&bgc=660000&nc"; //null; //"&nc"; //null; //&nc';
$testcase = array(
$nc . '&w=600',
$nc . '&w=600&no-upscale',
$nc . '&h=400',
$nc . '&h=400&no-upscale',
$nc . '&w=600&h=400',
$nc . '&w=600&h=400&no-upscale',
$nc . '&w=700&h=400&stretch',
$nc . '&w=700&h=400&no-upscale&stretch',
$nc . '&h=420',
$nc . '&h=420&no-upscale',
$nc . '&w=600&h=420',
$nc . '&w=600&h=420&no-upscale',
$nc . '&w=700&h=420&stretch',
$nc . '&w=700&h=420&no-upscale&stretch',
$nc . '&w=700&h=200&stretch',
$nc . '&w=700&h=200&no-upscale&stretch',
$nc . '&w=250&h=400&stretch',
$nc . '&w=250&h=400&no-upscale&stretch',
$nc . '&w=700&h=400&crop-to-fit',
$nc . '&w=700&h=400&no-upscale&crop-to-fit',
$nc . '&w=250&h=420&stretch',
$nc . '&w=250&h=420&no-upscale&stretch',
$nc . '&w=700&h=420&crop-to-fit',
$nc . '&w=700&h=420&no-upscale&crop-to-fit',
$nc . '&w=700&h=200&crop-to-fit',
$nc . '&w=700&h=200&no-upscale&crop-to-fit',
$nc . '&w=250&h=400&crop-to-fit',
$nc . '&w=250&h=400&no-upscale&crop-to-fit',
$nc . '&w=250&h=420&crop-to-fit',
$nc . '&w=250&h=420&no-upscale&crop-to-fit',
$nc . '&w=600&h=500&fill-to-fit',
$nc . '&w=600&h=500&no-upscale&fill-to-fit',
$nc . '&w=250&h=400&fill-to-fit',
$nc . '&w=250&h=400&no-upscale&fill-to-fit',
$nc . '&w=700&h=400&fill-to-fit',
$nc . '&w=700&h=400&no-upscale&fill-to-fit',
/*
$nc . '&w=600&ar=1.6',
$nc . '&w=600&ar=1.6&no-upscale',
$nc . '&h=400&ar=1.6',
$nc . '&h=400&ar=1.6&no-upscale',
*/
$nc . '&w=250&h=420&fill-to-fit',
$nc . '&w=250&h=420&no-upscale&fill-to-fit',
$nc . '&w=700&h=420&fill-to-fit',
$nc . '&w=700&h=420&no-upscale&fill-to-fit',
);