1
0
mirror of https://github.com/mosbth/cimage.git synced 2025-08-06 08:07:42 +02:00

resize-strategies together with no upscale

This commit is contained in:
Mikael Roos
2015-12-06 01:24:30 +01:00
parent 679714422d
commit 5a09c38f5d
15 changed files with 733 additions and 431 deletions

View File

@@ -15,22 +15,21 @@ class CImageResizerStrategyCropToFitTest extends \PHPUnit_Framework_TestCase
return array(
// Square
array(100, 100, null, null, 100, 100, 0, 0, 100, 100),
array(100, 100, null, 200, 200, 200, 0, 0, 100, 100),
array(100, 100, 200, null, 200, 200, 0, 0, 100, 100),
array(100, 100, 200, 200, 200, 200, 0, 0, 100, 100),
array(100, 100, 200, 200, 0, 0, 100, 100),
array(100, 100, 200, 100, 0, 25, 100, 50),
array(100, 100, 100, 200, 25, 0, 50, 100),
// Landscape
array(100, 200, null, null, 100, 200, 0, 0, 100, 200),
array(100, 200, null, 100, 50, 100, 0, 0, 100, 200),
array(100, 200, 50, null, 50, 100, 0, 0, 100, 200),
array(100, 200, 50, 100, 50, 100, 0, 0, 100, 200),
array(200, 100, 400, 200, 0, 0, 200, 100),
array(200, 100, 50, 50, 50, 0, 100, 100),
array(200, 100, 400, 100, 0, 25, 200, 50),
array(200, 100, 100, 400, round(175/2), 0, 25, 100),
// Portrait
array(200, 100, null, null, 200, 100, 0, 0, 200, 100),
array(200, 100, null, 200, 400, 200, 0, 0, 200, 100),
array(200, 100, 400, null, 400, 200, 0, 0, 200, 100),
array(200, 100, 400, 200, 400, 200, 0, 0, 200, 100),
array(100, 200, 50, 100, 0, 0, 100, 200),
array(100, 200, 50, 50, 0, 50, 100, 100),
array(100, 200, 200, 50, 0, round(175/2), 100, 25),
array(100, 200, 50, 200, 25, 0, 50, 200),
);
}
@@ -44,21 +43,74 @@ class CImageResizerStrategyCropToFitTest extends \PHPUnit_Framework_TestCase
*
* @return void
*/
public function testResize1($srcWidth, $srcHeight, $targetWidth, $targetHeight, $targetWidthAfter, $targetHeightAfter, $cropX, $cropY, $cropWidth, $cropHeight)
public function testResize1($sw, $sh, $tw, $th, $cx, $cy, $cw, $ch)
{
$img = new CImageResizer(/*'logger'/**/);
$img->setSource($srcWidth, $srcHeight)
->setBaseWidthHeight($targetWidth, $targetHeight)
$img->setSource($sw, $sh)
->setBaseWidthHeight($tw, $th)
->setResizeStrategy(CImageResizer::CROP_TO_FIT)
->calculateTargetWidthAndHeight();
$this->assertEquals($targetWidthAfter, $img->getTargetWidth(), "Target width not correct.");
$this->assertEquals($targetHeightAfter, $img->getTargetHeight(), "Target height not correct.");
$this->assertEquals($tw, $img->getTargetWidth(), "Target width not correct.");
$this->assertEquals($th, $img->getTargetHeight(), "Target height not correct.");
$this->assertEquals($cropX, $img->getCropX(), "CropX not correct.");
$this->assertEquals($cropY, $img->getCropY(), "CropY not correct.");
$this->assertEquals($cropWidth, $img->getCropWidth(), "CropWidth not correct.");
$this->assertEquals($cropHeight, $img->getCropHeight(), "CropHeight not correct.");
$this->assertEquals($cx, $img->getCropX(), "CropX not correct.");
$this->assertEquals($cy, $img->getCropY(), "CropY not correct.");
$this->assertEquals($cw, $img->getCropWidth(), "CropWidth not correct.");
$this->assertEquals($ch, $img->getCropHeight(), "CropHeight not correct.");
}
/**
* Provider
*
* @return array
*/
public function providerImages2()
{
return [
// Square
[100, 100, 200, 200, 50, 50, 100, 100],
[100, 100, 400, 100, 150, 0, 100, 100],
[100, 100, 100, 400, 0, 150, 100, 100],
[100, 100, 400, 400, 150, 150, 100, 100],
[491, 323, 600, 400, 55, 39, 491, 323],
// Landscape
// Portrait
];
}
/**
* Test
*
* @dataProvider providerImages
*
* @return void
*/
public function testResize2($sw, $sh, $tw, $th, $cx, $cy, $cw, $ch)
{
$img = new CImageResizer(/*'logger'/**/);
$img->setSource($sw, $sh)
->setBaseWidthHeight($tw, $th)
->setResizeStrategy(CImageResizer::CROP_TO_FIT)
->allowUpscale(false)
->calculateTargetWidthAndHeight();
$this->assertEquals($tw, $img->getTargetWidth(), "Target width not correct.");
$this->assertEquals($th, $img->getTargetHeight(), "Target height not correct.");
$this->assertEquals($cx, $img->getCropX(), "CropX not correct.");
$this->assertEquals($cy, $img->getCropY(), "CropY not correct.");
$this->assertEquals($cw, $img->getCropWidth(), "CropWidth not correct.");
$this->assertEquals($ch, $img->getCropHeight(), "CropHeight not correct.");
}
}

View File

@@ -15,22 +15,21 @@ class CImageResizerStrategyFillToFitTest extends \PHPUnit_Framework_TestCase
return array(
// Square
array(100, 100, null, null, 100, 100, 0, 0, 100, 100),
array(100, 100, null, 200, 200, 200, 0, 0, 100, 100),
array(100, 100, 200, null, 200, 200, 0, 0, 100, 100),
array(100, 100, 200, 200, 200, 200, 0, 0, 100, 100),
array(100, 100, 200, 200, 0, 0, 200, 200),
array(100, 100, 100, 50, 25, 0, 50, 50),
array(100, 100, 50, 100, 0, 25, 50, 50),
// Landscape
array(100, 200, null, null, 100, 200, 0, 0, 100, 200),
array(100, 200, null, 100, 50, 100, 0, 0, 100, 200),
array(100, 200, 50, null, 50, 100, 0, 0, 100, 200),
array(100, 200, 50, 100, 50, 100, 0, 0, 100, 200),
array(200, 100, 400, 200, 0, 0, 400, 200),
array(200, 100, 100, 100, 0, 25, 100, 50),
array(200, 100, 400, 100, 100, 0, 200, 100),
array(200, 100, 100, 400, 0, 175, 100, 50),
// Portrait
array(200, 100, null, null, 200, 100, 0, 0, 200, 100),
array(200, 100, null, 200, 400, 200, 0, 0, 200, 100),
array(200, 100, 400, null, 400, 200, 0, 0, 200, 100),
array(200, 100, 400, 200, 400, 200, 0, 0, 200, 100),
array(100, 200, 200, 400, 0, 0, 200, 400),
array(100, 200, 100, 100, 25, 0, 50, 100),
array(100, 200, 400, 100, 175, 0, 50, 100),
array(100, 200, 100, 400, 0, 100, 100, 200),
);
}
@@ -44,21 +43,76 @@ class CImageResizerStrategyFillToFitTest extends \PHPUnit_Framework_TestCase
*
* @return void
*/
public function testResize1($srcWidth, $srcHeight, $targetWidth, $targetHeight, $targetWidthAfter, $targetHeightAfter, $cropX, $cropY, $cropWidth, $cropHeight)
public function testResize1($sw, $sh, $tw, $th, $dx, $dy, $dw, $dh)
{
$img = new CImageResizer(/*'logger'/**/);
$img->setSource($srcWidth, $srcHeight)
->setBaseWidthHeight($targetWidth, $targetHeight)
$img->setSource($sw, $sh)
->setBaseWidthHeight($tw, $th)
->setResizeStrategy(CImageResizer::FILL_TO_FIT)
->calculateTargetWidthAndHeight();
$this->assertEquals($targetWidthAfter, $img->getTargetWidth(), "Target width not correct.");
$this->assertEquals($targetHeightAfter, $img->getTargetHeight(), "Target height not correct.");
$this->assertEquals($tw, $img->getTargetWidth(), "Target width not correct.");
$this->assertEquals($th, $img->getTargetHeight(), "Target height not correct.");
$this->assertEquals($dx, $img->getDestinationX(), "DestinationX not correct.");
$this->assertEquals($dy, $img->getDestinationY(), "DestinationY not correct.");
$this->assertEquals($dw, $img->getDestinationWidth(), "DestinationWidth not correct.");
$this->assertEquals($dh, $img->getDestinationHeight(), "DestinationHeight not correct.");
}
/**
* Provider
*
* @return array
*/
public function providerImages2()
{
return [
// Square
[100, 100, 200, 200, 50, 50, 100, 100],
[100, 100, 400, 100, 150, 0, 100, 100],
[100, 100, 100, 400, 0, 150, 100, 100],
[100, 100, 400, 400, 150, 150, 100, 100],
[491, 323, 600, 400, 55, 39, 491, 323],
// Landscape
// Portrait
];
}
/**
* Test
*
* @dataProvider providerImages2
*
* @return void
*/
public function testResize2($sw, $sh, $tw, $th, $dx, $dy, $dw, $dh)
{
$img = new CImageResizer(/*'logger'/**/);
$img->setSource($sw, $sh)
->setBaseWidthHeight($tw, $th)
->setResizeStrategy(CImageResizer::FILL_TO_FIT)
->allowUpscale(false)
->calculateTargetWidthAndHeight();
$this->assertEquals($tw, $img->getTargetWidth(), "Target width not correct.");
$this->assertEquals($th, $img->getTargetHeight(), "Target height not correct.");
$this->assertEquals($dx, $img->getDestinationX(), "DestinationX not correct.");
$this->assertEquals($dy, $img->getDestinationY(), "DestinationY not correct.");
$this->assertEquals($dw, $img->getDestinationWidth(), "DestinationWidth not correct.");
$this->assertEquals($dh, $img->getDestinationHeight(), "DestinationHeight not correct.");
$this->assertEquals($cropX, $img->getCropX(), "CropX not correct.");
$this->assertEquals($cropY, $img->getCropY(), "CropY not correct.");
$this->assertEquals($cropWidth, $img->getCropWidth(), "CropWidth not correct.");
$this->assertEquals($cropHeight, $img->getCropHeight(), "CropHeight not correct.");
}
}

View File

@@ -21,17 +21,17 @@ class CImageResizerStrategyKeepAspectRatioTest extends \PHPUnit_Framework_TestCa
array(100, 100, 200, 200, 200, 200, 0, 0, 100, 100),
// Landscape
array(100, 200, null, null, 100, 200, 0, 0, 100, 200),
array(100, 200, null, 100, 50, 100, 0, 0, 100, 200),
array(100, 200, 50, null, 50, 100, 0, 0, 100, 200),
array(100, 200, 50, 100, 50, 100, 0, 0, 100, 200),
// Portrait
array(200, 100, null, null, 200, 100, 0, 0, 200, 100),
array(200, 100, null, 200, 400, 200, 0, 0, 200, 100),
array(200, 100, 400, null, 400, 200, 0, 0, 200, 100),
array(200, 100, 400, 200, 400, 200, 0, 0, 200, 100),
// Portrait
array(100, 200, null, null, 100, 200, 0, 0, 100, 200),
array(100, 200, null, 100, 50, 100, 0, 0, 100, 200),
array(100, 200, 50, null, 50, 100, 0, 0, 100, 200),
array(100, 200, 50, 100, 50, 100, 0, 0, 100, 200),
);
}
@@ -61,4 +61,66 @@ class CImageResizerStrategyKeepAspectRatioTest extends \PHPUnit_Framework_TestCa
$this->assertEquals($cw, $img->getCropWidth(), "CropWidth not correct.");
$this->assertEquals($ch, $img->getCropHeight(), "CropHeight not correct.");
}
/**
* Provider
*
* @return array
*/
public function providerImages2()
{
return array(
// Square
array(100, 100, 100, 100, 100, 100, 0, 0, 100, 100),
array(100, 100, null, 200, 100, 100, 0, 0, 100, 100),
array(100, 100, 200, null, 100, 100, 0, 0, 100, 100),
array(100, 100, 200, 100, 100, 100, 0, 0, 100, 100),
array(100, 100, 100, 200, 100, 100, 0, 0, 100, 100),
array(100, 100, 200, 200, 100, 100, 0, 0, 100, 100),
// Landscape
//array(200, 100, null, null, 200, 100, 0, 0, 200, 100),
//array(200, 100, null, 200, 400, 200, 0, 0, 200, 100),
//array(200, 100, 400, null, 400, 200, 0, 0, 200, 100),
//array(200, 100, 400, 200, 400, 200, 0, 0, 200, 100),
// Portrait
//array(100, 200, null, null, 100, 200, 0, 0, 100, 200),
//array(100, 200, null, 100, 50, 100, 0, 0, 100, 200),
//array(100, 200, 50, null, 50, 100, 0, 0, 100, 200),
//array(100, 200, 50, 100, 50, 100, 0, 0, 100, 200),
);
}
/**
* Test
*
* @dataProvider providerImages2
*
* @return void
*/
public function testResize2($sw, $sh, $tw, $th, $twa, $tha, $cx, $cy, $cw, $ch)
{
$img = new CImageResizer(/*'logger'/**/);
$img->setSource($sw, $sh)
->setBaseWidthHeight($tw, $th)
->setResizeStrategy(CImageResizer::KEEP_RATIO)
->allowUpscale(false)
->calculateTargetWidthAndHeight();
$this->assertEquals($twa, $img->getTargetWidth(), "Target width not correct.");
$this->assertEquals($tha, $img->getTargetHeight(), "Target height not correct.");
$this->assertEquals($cx, $img->getCropX(), "CropX not correct.");
$this->assertEquals($cy, $img->getCropY(), "CropY not correct.");
$this->assertEquals($cw, $img->getCropWidth(), "CropWidth not correct.");
$this->assertEquals($ch, $img->getCropHeight(), "CropHeight not correct.");
}
}

View File

@@ -15,22 +15,17 @@ class CImageResizerStrategyStretchTest extends \PHPUnit_Framework_TestCase
return array(
// Square
array(100, 100, null, null, 100, 100, 0, 0, 100, 100),
array(100, 100, null, 200, 200, 200, 0, 0, 100, 100),
array(100, 100, 200, null, 200, 200, 0, 0, 100, 100),
array(100, 100, 200, 200, 200, 200, 0, 0, 100, 100),
array(100, 100, 200, 200),
array(100, 100, 200, 100),
array(100, 100, 100, 200),
// Landscape
array(100, 200, null, null, 100, 200, 0, 0, 100, 200),
array(100, 200, null, 100, 50, 100, 0, 0, 100, 200),
array(100, 200, 50, null, 50, 100, 0, 0, 100, 200),
array(100, 200, 50, 100, 50, 100, 0, 0, 100, 200),
array(200, 100, 400, 200),
array(200, 100, 100, 200),
// Portrait
array(200, 100, null, null, 200, 100, 0, 0, 200, 100),
array(200, 100, null, 200, 400, 200, 0, 0, 200, 100),
array(200, 100, 400, null, 400, 200, 0, 0, 200, 100),
array(200, 100, 400, 200, 400, 200, 0, 0, 200, 100),
array(100, 200, 50, 100),
array(100, 200, 100, 100),
);
}
@@ -44,7 +39,7 @@ class CImageResizerStrategyStretchTest extends \PHPUnit_Framework_TestCase
*
* @return void
*/
public function testResize1($sw, $sh, $tw, $th, $twa, $tha, $cx, $cy, $cw, $ch)
public function testResize1($sw, $sh, $tw, $th)
{
$img = new CImageResizer(/*'logger'/**/);
@@ -53,12 +48,8 @@ class CImageResizerStrategyStretchTest extends \PHPUnit_Framework_TestCase
->setResizeStrategy(CImageResizer::STRETCH)
->calculateTargetWidthAndHeight();
$this->assertEquals($twa, $img->getTargetWidth(), "Target width not correct.");
$this->assertEquals($tha, $img->getTargetHeight(), "Target height not correct.");
$this->assertEquals($tw, $img->getTargetWidth(), "Target width not correct.");
$this->assertEquals($th, $img->getTargetHeight(), "Target height not correct.");
$this->assertEquals($cx, $img->getCropX(), "CropX not correct.");
$this->assertEquals($cy, $img->getCropY(), "CropY not correct.");
$this->assertEquals($cw, $img->getCropWidth(), "CropWidth not correct.");
$this->assertEquals($ch, $img->getCropHeight(), "CropHeight not correct.");
}
}

View File

@@ -8,97 +8,13 @@ function logger($str)
echo "$str\n";
}
function loggerDummy($str)
{
;
}
class CImageResizerTest extends \PHPUnit_Framework_TestCase
{
/**
* Provider
*
* @return array
*/
public function providerImages()
{
return array(
// $strategy
// $srcWidth, $srcHeight, $targetWidth, $targetHeight,
// $aspectRatio, $dpr,
// $expectedWidth, $expectedHeight,
// $expectedWidth2, $expectedHeight2
// Same as source, does not set target
// ===== Keep aspect ratio
/*
array(1, 100, 100, null, null, null, 1, null, null, 100, 100),
array(1, 100, 150, null, null, null, 1, null, null, 100, 150),
array(1, 150, 100, null, null, null, 1, null, null, 150, 100),
// Width & Height as %
array(1, 100, 100, '200%', null, null, 1, 200, null, 200, 200),
array(1, 100, 100, null, '50%', null, 1, null, 50, 50, 50),
// dpr
//array(1, 100, 100, null, null, null, 2, null, null, 100, 100), // do dpr?
/*
array(1, 100, 100, 100, null, null, 2, 200, null, 200, 200),
array(1, 100, 100, null, 100, null, 2, null, 200, 200, 200),
array(1, 100, 100, 100, 100, null, 2, 200, 200, 200, 200),
*/
// ===== Need crop to fit or fill to fit
// Aspect ratio
/*
array(2, 100, 100, null, null, 2, 1, 100, 50, 100, 50),
array(2, 100, 200, null, null, 4, 1, 100, 25, 100, 25),
array(2, 200, 100, null, null, 4, 1, 200, 50, 200, 50),
// Aspect ratio inverted
array(2, 100, 100, null, null, 1/2, 1, 50, 100, 50, 100),
array(2, 100, 200, null, null, 1/4, 1, 50, 200, 50, 200),
array(2, 200, 100, null, null, 1/4, 1, 25, 100, 25, 100),
// Aspect ratio & width
array(2, 100, 100, 200, null, 2, 1, 200, 100, 200, 100),
// Aspect ratio & height
array(2, 100, 100, null, 200, 1/2, 1, 100, 200, 100, 200),
*/
);
}
/**
* Test
*
* @dataProvider providerImages
*
* @return void
*/
/*
public function testResize1($strategy, $srcWidth, $srcHeight, $targetWidth, $targetHeight, $aspectRatio, $dpr, $expectedWidth, $expectedHeight, $expectedWidth2, $expectedHeight2)
{
$img = new CImageResizer(/*'logger'*/ /*);
//$img = new CImageResizer('logger');
$img->setSource($srcWidth, $srcHeight)
->setResizeStrategy($strategy)
->setBaseWidthHeight($targetWidth, $targetHeight)
->setBaseAspecRatio($aspectRatio)
->setBaseDevicePixelRate($dpr)
->prepareTargetDimensions();
$this->assertEquals($expectedWidth, $img->getTargetWidth(), "Width not correct.");
$this->assertEquals($expectedHeight, $img->getTargetHeight(), "Height not correct.");
$img->calculateTargetWidthAndHeight();
$this->assertEquals($expectedWidth2, $img->getTargetWidth(), "Width not correct.");
$this->assertEquals($expectedHeight2, $img->getTargetHeight(), "Height not correct.");
}
*/
/**
* Provider
*
@@ -136,6 +52,21 @@ class CImageResizerTest extends \PHPUnit_Framework_TestCase
/**
* Test
*
* @return void
*/
public function testLogger()
{
$img = new CImageResizer('loggerDummy');
$img->setBaseWidthHeight(100, 100);
}
/**
* Provider
*
@@ -147,6 +78,7 @@ class CImageResizerTest extends \PHPUnit_Framework_TestCase
array(CImageResizer::KEEP_RATIO, "KEEP_RATIO"),
array(CImageResizer::CROP_TO_FIT, "CROP_TO_FIT"),
array(CImageResizer::FILL_TO_FIT, "FILL_TO_FIT"),
array(CImageResizer::STRETCH, "STRETCH"),
array(-1, "UNKNOWN"),
);
}
@@ -169,4 +101,104 @@ class CImageResizerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($str, $res, "Strategy not matching.");
}
/**
* Provider
*
* @return array
*/
public function providerPercent()
{
return array(
array(100, 100, "100%", "100%", 100, 100),
array(100, 100, "50%", "50%", 50, 50),
);
}
/**
* Test
*
* @dataProvider providerPercent
*
* @return void
*/
public function testPercent($sw, $sh, $tw, $th, $w, $h)
{
$img = new CImageResizer(/*'logger'*/);
$img->setSource($sw, $sh)
->setBaseWidthHeight($tw, $th);
$this->assertEquals($w, $img->getTargetWidth(), "Target width not correct.");
$this->assertEquals($h, $img->getTargetHeight(), "Target height not correct.");
}
/**
* Test
*
* @return void
*/
public function testGetSource()
{
$img = new CImageResizer(/*'logger'*/);
$w = 100;
$h = 100;
$img->setSource($w, $h);
$this->assertEquals($w, $img->getSourceWidth(), "Source width not correct.");
$this->assertEquals($h, $img->getSourceHeight(), "Source height not correct.");
}
/**
* Provider
*
* @return array
*/
public function providerImages()
{
return [
// car.png
[CImageResizer::KEEP_RATIO, 491, 324, 500, 200, 303, 200, 0, 0, 491, 324],
[CImageResizer::KEEP_RATIO, 491, 324, 500, 500, 500, 330, 0, 0, 491, 324],
];
}
/**
* Test
*
* @dataProvider providerImages
*
* @return void
*/
public function testResize($strat, $sw, $sh, $tw, $th, $twa, $tha, $cx, $cy, $cw, $ch)
{
$img = new CImageResizer(/*'logger'/**/);
$img->setSource($sw, $sh)
->setBaseWidthHeight($tw, $th)
->setResizeStrategy($strat)
->calculateTargetWidthAndHeight();
$this->assertEquals($twa, $img->getTargetWidth(), "Target width not correct.");
$this->assertEquals($tha, $img->getTargetHeight(), "Target height not correct.");
$this->assertEquals($cx, $img->getCropX(), "CropX not correct.");
$this->assertEquals($cy, $img->getCropY(), "CropY not correct.");
$this->assertEquals($cw, $img->getCropWidth(), "CropWidth not correct.");
$this->assertEquals($ch, $img->getCropHeight(), "CropHeight not correct.");
}
}