1
0
mirror of https://github.com/mosbth/cimage.git synced 2025-07-23 17:51:34 +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

@@ -5,6 +5,7 @@ Image conversion on the fly using PHP
[![Build Status](https://travis-ci.org/mosbth/cimage.svg?branch=resize)](https://travis-ci.org/mosbth/cimage)
[![Build Status](https://scrutinizer-ci.com/g/mosbth/cimage/badges/build.png?b=resize)](https://scrutinizer-ci.com/g/mosbth/cimage/build-status/resize)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mosbth/cimage/badges/quality-score.png?b=resize)](https://scrutinizer-ci.com/g/mosbth/cimage/?branch=resize)
[![Code Coverage](https://scrutinizer-ci.com/g/mosbth/cimage/badges/coverage.png?b=resize)](https://scrutinizer-ci.com/g/mosbth/cimage/?branch=resize)

View File

@@ -8,7 +8,7 @@ v0.8.* (2015-12-05) (branch resize)
* Improving build phase using travis and scrutinizer.
* Code validating with phpunit and phpcs.
* Moved classes to src/, adding namespace and support PSR-4.
* Moved classes to src/, adding namespace and (only) support PSR-4.
* Require PHP 5.4.

View File

@@ -954,9 +954,9 @@ class CImage
public function initDimensions()
{
$this->imageResizer->setBaseWidthHeight($this->newWidth, $this->newHeight)
->setBaseAspecRatio($this->aspectRatio)
->setBaseDevicePixelRate($this->dpr)
->prepareTargetDimensions();
->setBaseAspecRatio($this->aspectRatio)
->setBaseDevicePixelRate($this->dpr)
->prepareTargetDimensions();
return $this;
}
@@ -973,21 +973,26 @@ class CImage
$imres = $this->imageResizer;
$strategy = null;
if ($this->keepRatio == true) {
$strategy = $imres::KEEP_RATIO;
$strategy = $imres::KEEP_RATIO;
if ($this->keepRatio == false) {
$strategy = $imres::STRETCH;
}
if ($this->cropToFit == true) {
$strategy = $imres::CROP_TO_FIT;
}
if ($this->fillToFit == true) {
$strategy = $imres::FILL_TO_FIT;
}
$imres->setResizeStrategy($strategy)
->allowUpscale($this->upscale)
->calculateTargetWidthAndHeight();
$this->newWidth = $imres->width();
$this->newHeight = $imres->height();
//$this->newWidth = $imres->getTargetWidth();
//$this->newHeight = $imres->getTargetHeight();
/*
// Crop, use cropped width and height as base for calulations
@@ -1275,6 +1280,7 @@ class CImage
$filename = basename($this->pathToImage);
$cropToFit = $this->cropToFit ? '_cf' : null;
$fillToFit = $this->fillToFit ? '_ff' : null;
$stretch = $this->keepRatio === false ? '_st' : null;
$crop_x = $this->crop_x ? "_x{$this->crop_x}" : null;
$crop_y = $this->crop_y ? "_y{$this->crop_y}" : null;
$scale = $this->scale ? "_s{$this->scale}" : null;
@@ -1344,7 +1350,7 @@ class CImage
}
$file = $prefix . $subdir . $filename . $width . $height
. $offset . $crop . $cropToFit . $fillToFit
. $offset . $crop . $cropToFit . $fillToFit . $stretch
. $crop_x . $crop_y . $upscale
. $quality . $filters . $sharpen . $emboss . $blur . $palette
. $optimize . $compress
@@ -1392,8 +1398,7 @@ class CImage
/**
* Load image from disk. Try to load image without verbose error message,
* if fail, load again and display error messages.
* Load image from disk.
*
* @param string $src of image.
* @param string $dir as base directory where images are.
@@ -1627,10 +1632,32 @@ class CImage
*/
public function resize()
{
$imgres = $this->imageResizer;
$res = $this->imageResizer;
$this->log("### Starting to Resize()");
$this->log("Upscale = '$this->upscale'");
$this->log(" Upscale = '$this->upscale'");
$sw = $res->getSourceWidth();
$sh = $res->getSourceHeight();
$tw = $res->getTargetWidth();
$th = $res->getTargetHeight();
$cx = $res->getCropX();
$cy = $res->getCropY();
$cw = $res->getCropWidth();
$ch = $res->getCropHeight();
$dx = $res->getDestinationX();
$dy = $res->getDestinationY();
$dw = $res->getDestinationWidth();
$dh = $res->getDestinationHeight();
$img = $this->CreateImageKeepTransparency($tw, $th);
$this->imageCopyResampled($img, $this->image, $dx, $dy, $cx, $cy, $dw, $dh, $cw, $ch);
$this->image = $img;
$this->width = $tw;
$this->height = $th;
return $this;
// Only use a specified area of the image, $this->offset is defining the area to use
if (isset($this->offset)) {
@@ -2417,7 +2444,7 @@ class CImage
}
}
// Only covert if cachedir is writable
// Only convert if cachedir is writable
if (is_writable($this->saveFolder)) {
// Load file and check if conversion is needed
$image = new Imagick($this->pathToImage);

View File

@@ -24,12 +24,20 @@ class CImageResizer
/**
* Set as expected target image dimensions.
* Set as expected target image/canvas dimensions.
*/
private $targetWidth;
//private $targetWidthOrig; // Save original value
private $targetHeight;
//private $targetheightOrig; // Save original value
/**
* Where should the image go on the canvas.
*/
private $destinationX;
private $destinationY;
private $destinationWidth;
private $destinationHeight;
@@ -114,7 +122,14 @@ class CImageResizer
/**
* The currently selected resize strategy.
*/
private $resizeStrategy;
private $resizeStrategy = self::KEEP_RATIO;
/**
* Allow upscale of smaller images by default, set to false to disallow.
*/
private $upscale = true;
@@ -186,6 +201,10 @@ class CImageResizer
return "FILL_TO_FIT";
break;
case self::STRETCH:
return "STRETCH";
break;
default:
return "UNKNOWN";
}
@@ -210,16 +229,52 @@ class CImageResizer
/**
* Set base for requested width and height.
*
* @param numeric|null $width as requested target width
* @param numeric|null $height as requested target height
*
* @throws Exception
*
* @return $this
*/
/**
* Allow or disallow upscale smaller images.
*
* @param boolean $upscale
*
* @return $this
*/
public function allowUpscale($upscale)
{
$this->upscale = $upscale;
$this->log("# Allow upscale is $this->upscale.");
return $this;
}
/**
* Check if a value is upscaled or not by compare $current to $orig,
* if $current is larger than $orig then check if upscaled is allowed.
*
* @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.
*/
public function respectUpscale(&$current, $orig)
{
if (!$this->upscale && $current > $orig) {
$this->log("# Disallowed upscale of $orig to $current");
$current = $orig;
}
}
/**
* Set base for requested width and height.
*
* @param numeric|null $width as requested target width
* @param numeric|null $height as requested target height
*
* @throws Exception
*
* @return $this
*/
public function setBaseWidthHeight($width = null, $height = null)
{
$this->log("# Set base for width and height.");
@@ -437,18 +492,6 @@ class CImageResizer
$this->log("# Calculate new width and height.");
$this->log(" Source size {$this->srcWidth}x{$this->srcHeight}.");
$this->log(" Target dimension (before) {$this->targetWidth}x{$this->targetHeight}.");
/*
// Set default values to crop area to be whole source image
$aspectRatio = $this->srcWidth / $this->srcHeight;
$this->cropX = 0;
$this->cropY = 0;
$this->cropWidth = $this->srcWidth;
$this->cropHeight = $this->srcHeight;
// Get relations of original & target image
$width = $this->srcWidth;
$height = $this->srcHeight;
*/
// Set default values to crop area to be whole source image
$sw = $this->srcWidth;
@@ -456,10 +499,17 @@ class CImageResizer
$ar = $sw / $sh;
$tw = $this->targetWidth;
$th = $this->targetHeight;
$dx = 0;
$dy = 0;
$dw = null;
$dh = null;
$cx = 0;
$cy = 0;
$cw = $this->srcWidth;
$ch = $this->srcHeight;
$cw = $sw;
$ch = $sh;
$rs = $this->resizeStrategy;
$both = isset($tw) && isset($th);
$ratio = $both ? $tw / $th : null;
if (is_null($tw) && is_null($th)) {
@@ -471,109 +521,103 @@ class CImageResizer
} elseif (isset($tw) && is_null($th)) {
// Keep aspect ratio, make th based on tw
$this->respectUpscale($tw, $sw);
$th = $tw / $ar;
$this->log(" New th x{$th}");
} elseif (is_null($tw) && isset($th)) {
// Keep aspect ratio, make tw based on th
$this->respectUpscale($th, $sh);
$tw = $th * $ar;
$this->log(" New tw {$tw}x");
} elseif (isset($tw) && isset($th)) {
} elseif ($rs === CImageResizer::KEEP_RATIO && $both) {
// Keep aspect ratio, make fit in imaginary box
if ($ar < 1) {
// Keep aspect ratio, make fit in box not larger than tw/th
$this->log(" Keep ratio, ratio target=$ratio, source=$ar");
if ($ratio > $ar) {
$this->respectUpscale($th, $sh);
$tw = $th * $ar;
$this->log(" New tw {$tw}x");
} else {
$this->log(" New tw {$tw}x");
} elseif ($ratio < $ar) {
$this->respectUpscale($tw, $sw);
$th = $tw / $ar;
$this->log(" New th x{$th}");
$this->log(" New th x{$th}");
} else {
$this->respectUpscale($tw, $sw);
$this->respectUpscale($th, $sh);
}
}
/*
if (isset($tw) && isset($th)) {
} elseif ($rs === CImageResizer::STRETCH && $both) {
// Both new width and height are set.
// Use targetWidth and targetHeight as max width/height, image
// should not be larger.
$ratioWidth = $width / $this->targetWidth;
$ratioHeight = $height / $this->targetHeight;
$ratio = ($ratioWidth > $ratioHeight) ? $ratioWidth : $ratioHeight;
$this->targetWidth = round($width / $ratio);
$this->targetHeight = round($height / $ratio);
$this->log(" New width and height was set.");
// Stretch to fit, leave as is
$this->log(" Stretch, leave as is");
$this->respectUpscale($tw, $sw);
$this->respectUpscale($th, $sh);
} elseif (isset($this->targetWidth)) {
} elseif ($rs === CImageResizer::CROP_TO_FIT && $both) {
// Use new width as max-width
$factor = (float)$this->targetWidth / (float)$width;
$this->targetHeight = round($factor * $height);
$this->log(" New height x$this->targetHeight.");
// Crop to fit image in box
// Ignores respectUpscale by intention
$this->log(" Crop to fit, ratio target=$ratio, source=$ar");
} elseif (isset($this->targetHeight)) {
if ($ratio > $ar) {
$ch = $sw / $ratio;
$cy = ($sh - $ch) / 2;
} elseif ($ratio < $ar) {
$cw = $sh * $ratio;
$cx = ($sw - $cw) / 2;
}
// Use new height as max-hight
$factor = (float)$this->targetHeight / (float)$height;
$this->targetWidth = round($factor * $width);
$this->log(" New width {$this->targetWidth}x.");
$this->log(" Parts cx=$cx, cy=$cy, cw=$cw, ch=$ch");
} elseif ($rs === CImageResizer::FILL_TO_FIT && $both) {
// Fill to fit image in box
$this->log(" Fill to fit, ratio target=$ratio, source=$ar");
$dw = $tw;
$dh = $th;
if ($ratio > $ar) {
$dw = $th * $ar;
$dh = $th;
} elseif ($ratio < $ar) {
$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
$dw = is_null($dw) ? $tw : $dw;
$dh = is_null($dh) ? $th : $dh;
// No new height or width is set, use existing measures.
/*
$this->targetWidth = isset($this->targetWidth)
? $this->targetWidth
: $this->srcWidth;
$this->targetHeight = isset($this->targetHeight)
? $this->targetHeight
: $this->srcHeight;
*/
$this->targetWidth = round($tw);
$this->targetHeight = round($th);
$this->cropX = round($cx);
$this->cropY = round($cy);
$this->cropWidth = round($cw);
$this->cropHeight = round($ch);
$this->targetWidth = round($tw);
$this->targetHeight = round($th);
$this->destinationX = round($dx);
$this->destinationY = round($dy);
$this->destinationWidth = round($dw);
$this->destinationHeight = round($dh);
$this->cropX = round($cx);
$this->cropY = round($cy);
$this->cropWidth = round($cw);
$this->cropHeight = round($ch);
$this->log(" Target dimension (after) {$this->targetWidth}x{$this->targetHeight}.");
$this->log(" Crop {$this->cropX}x{$this->cropY} by {$this->cropWidth}x{$this->cropHeight}.");
$this->log(" Crop area {$this->cropX}x{$this->cropY} by {$this->cropWidth}x{$this->cropHeight}.");
$this->log(" Destination area {$this->destinationX}x{$this->destinationY} by {$this->destinationWidth}x{$this->destinationHeight}.");
/*
$ratioWidth = $this->srcWidth / $this->targetWidth;
$ratioHeight = $this->srcHeight / $this->targetHeight;
if ($this->resizeStrategy === self::CROP_TO_FIT) {
// Use targetWidth and targetHeight as defined
// width/height, image should fit the area.
$this->log(" Crop to fit.");
$ratio = ($ratioWidth < $ratioHeight) ? $ratioWidth : $ratioHeight;
$this->cropWidth = round($width / $ratio);
$this->cropHeight = round($height / $ratio);
$this->log(" Crop width, height, ratio: $this->cropWidth x $this->cropHeight ($ratio).");
} elseif ($this->resizeStrategy === self::FILL_TO_FIT) {
// Use targetWidth and targetHeight as defined
// width/height, image should fit the area.
$this->log(" Fill to fit.");
$ratio = ($ratioWidth < $ratioHeight) ? $ratioHeight : $ratioWidth;
$this->fillWidth = round($width / $ratio);
$this->fillHeight = round($height / $ratio);
$this->log(" Fill width, height, ratio: $this->fillWidth x $this->fillHeight ($ratio).");
}
*/
// Check if there is an area to crop off
if (isset($this->area)) {
@@ -614,85 +658,6 @@ class CImageResizer
$this->log(" Crop area is width {$width}px, height {$height}px, start_x {$this->crop['start_x']}px, start_y {$this->crop['start_y']}px.");
}
/*
// Calculate new width and height if keeping aspect-ratio.
if ($this->resizeStrategy === self::KEEP_RATIO) {
$this->log(" Keep aspect ratio.");
// Crop-to-fit and both new width and height are set.
if (($this->resizeStrategy === self::CROP_TO_FIT
|| $this->resizeStrategy === self::FILL_TO_FIT)
&& isset($this->targetWidth)
&& isset($this->targetHeight)
) {
// Use targetWidth and targetHeight as width/height, image should
// fit in box.
$this->log(" Use targetWidth and targetHeight as width/height, image should fit in box.");
} elseif (isset($this->targetWidth) && isset($this->targetHeight)) {
// Both new width and height are set.
// Use targetWidth and targetHeight as max width/height, image
// should not be larger.
$ratioWidth = $width / $this->targetWidth;
$ratioHeight = $height / $this->targetHeight;
$ratio = ($ratioWidth > $ratioHeight) ? $ratioWidth : $ratioHeight;
$this->targetWidth = round($width / $ratio);
$this->targetHeight = round($height / $ratio);
$this->log(" New width and height was set.");
} elseif (isset($this->targetWidth)) {
// Use new width as max-width
$factor = (float)$this->targetWidth / (float)$width;
$this->targetHeight = round($factor * $height);
$this->log(" New height x$this->targetHeight.");
} elseif (isset($this->targetHeight)) {
// Use new height as max-hight
$factor = (float)$this->targetHeight / (float)$height;
$this->targetWidth = round($factor * $width);
$this->log(" New width {$this->targetWidth}x.");
}
}
/*
// Get image dimensions for pre-resize image.
if ($this->resizeStrategy === self::CROP_TO_FIT
|| $this->resizeStrategy === self::FILL_TO_FIT
) {
// Get relations of original & target image
$ratioWidth = $width / $this->targetWidth;
$ratioHeight = $height / $this->targetHeight;
if ($this->resizeStrategy === self::CROP_TO_FIT) {
// Use targetWidth and targetHeight as defined
// width/height, image should fit the area.
$this->log(" Crop to fit.");
$ratio = ($ratioWidth < $ratioHeight) ? $ratioWidth : $ratioHeight;
$this->cropWidth = round($width / $ratio);
$this->cropHeight = round($height / $ratio);
$this->log(" Crop width, height, ratio: $this->cropWidth x $this->cropHeight ($ratio).");
} elseif ($this->resizeStrategy === self::FILL_TO_FIT) {
// Use targetWidth and targetHeight as defined
// width/height, image should fit the area.
$this->log(" Fill to fit.");
$ratio = ($ratioWidth < $ratioHeight) ? $ratioHeight : $ratioWidth;
$this->fillWidth = round($width / $ratio);
$this->fillHeight = round($height / $ratio);
$this->log(" Fill width, height, ratio: $this->fillWidth x $this->fillHeight ($ratio).");
}
}
*/
// Crop, ensure to set new width and height
@@ -706,38 +671,105 @@ class CImageResizer
: $this->crop['height']);
}
// Fill to fit, ensure to set new width and height
/*if ($this->fillToFit) {
$this->log("FillToFit.");
$this->targetWidth = round(isset($this->targetWidth) ? $this->targetWidth : $this->crop['width']);
$this->targetHeight = round(isset($this->targetHeight) ? $this->targetHeight : $this->crop['height']);
}*/
*/
return $this;
}
/**
* Get target width.
*
* @return integer as target width
*/
public function getTargetwidth()
/**
* Get source width.
*
* @return integer as source width
*/
public function getSourceWidth()
{
return $this->targetWidth ? round($this->targetWidth) : null;
return $this->srcWidth;
}
/**
* Get target height.
*
* @return integer as target height
*/
public function getTargetheight()
/**
* Get source height.
*
* @return integer as source height
*/
public function getSourceHeight()
{
return $this->targetHeight ? round($this->targetHeight) : null;
return $this->srcHeight;
}
/**
* Get target width.
*
* @return integer as target width
*/
public function getTargetWidth()
{
return $this->targetWidth;
}
/**
* Get target height.
*
* @return integer as target height
*/
public function getTargetHeight()
{
return $this->targetHeight;
}
/**
* Get destination x.
*
* @return integer as destination x
*/
public function getDestinationX()
{
return $this->destinationX;
}
/**
* Get destination y.
*
* @return integer as destination y
*/
public function getDestinationY()
{
return $this->destinationY;
}
/**
* Get destination width.
*
* @return integer as destination width
*/
public function getDestinationWidth()
{
return $this->destinationWidth;
}
/**
* Get destination height.
*
* @return integer as destination height
*/
public function getDestinationHeight()
{
return $this->destinationHeight;
}
@@ -750,14 +782,6 @@ class CImageResizer
public function getCropX()
{
return $this->cropX;
/*
$cropX = 0;
if ($this->cropWidth) {
$cropX = round(($this->cropWidth/2) - ($this->targetWidth/2));
};
return $cropX;*/
}
@@ -770,14 +794,6 @@ class CImageResizer
public function getCropY()
{
return $this->cropY;
/*
$cropY = 0;
if ($this->cropHeight) {
$cropY = round(($this->cropHeight/2) - ($this->targetHeight/2));
}
return $cropY;*/
}
@@ -790,14 +806,6 @@ class CImageResizer
public function getCropWidth()
{
return $this->cropWidth;
/*
$cropWidth = $this->srcWidth;
if ($this->cropWidth) {
$cropWidth = round($this->cropWidth);
}
return $cropWidth;*/
}
@@ -810,13 +818,5 @@ class CImageResizer
public function getCropHeight()
{
return $this->cropHeight;
/*
$cropHeight = $this->srcHeight;
if ($this->cropHeight) {
$cropHeight = round($this->cropHeight);
}
return $cropHeight;*/
}
}

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.");
}
}

View File

@@ -8,7 +8,7 @@
*
*/
$version = "v0.7.7* (2015-10-24)";
$version = "v0.8.0dev (2015-12-05)";
@@ -315,12 +315,9 @@ verbose("referer host = $refererHost");
* Get the source files.
*/
$autoloader = getConfig('autoloader', false);
$cimageClass = getConfig('cimage_class', false);
if ($autoloader) {
require $autoloader;
} elseif ($cimageClass) {
require $cimageClass;
}

View File

@@ -13,7 +13,8 @@ return array(
* Default values:
* mode: 'production'
*/
//'mode' => 'production', // 'development', 'strict'
//'mode' => 'production', // 'development', 'strict'
'mode' => 'development', // 'development', 'strict'
@@ -21,11 +22,9 @@ return array(
* Where are the sources for the classfiles.
*
* Default values:
* autoloader: null // used from v0.6.2
* cimage_class: null // used until v0.6.1
* autoloader: null
*/
'autoloader' => __DIR__ . '/../autoload.php',
//'cimage_class' => __DIR__ . '/../CImage.php',

View File

@@ -75,7 +75,7 @@ window.CImage = (function() {
area.classList.remove("hidden");
$.getJSON(this.value + "&json", function(data) {
json.innerHTML = "filename: " + data.filename + "\ncolors: " + data.colors + "\nsize: " + data.size + "\nwidth: " + data.width + "\nheigh: " + data.height + "\naspect-ratio: " + data.aspectRatio + "\npng-type: " + data.pngType;
json.innerHTML = "filename: " + data.filename + "\ncolors: " + data.colors + "\nsize: " + data.size + "\nwidth: " + data.width + "\nheight: " + data.height + "\naspect-ratio: " + data.aspectRatio + "\npng-type: " + data.pngType;
})
.fail(function() {
json.innerHTML = "Details not available."

View File

@@ -22,7 +22,8 @@ $images = array(
// For each image, apply these testcases
$nc = null; //"&nc"; //null; //&nc';
$nc = empty($_SERVER['QUERY_STRING']) ? "" : "&" . $_SERVER['QUERY_STRING'];
$testcase = array(
$nc . '&w=600',
$nc . '&w=600&no-upscale',
@@ -42,8 +43,8 @@ $testcase = array(
$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',
$nc . '&w=600&h=500&fill-to-fit',
$nc . '&w=600&h=500&no-upscale&fill-to-fit',
/*
$nc . '&w=600&ar=1.6',
$nc . '&w=600&ar=1.6&no-upscale',

View File

@@ -0,0 +1,43 @@
<?php
// Include config for all testcases
include __DIR__ . "/config.php";
// The title of the test case
$title = "Testing resize landscape image";
// Provide a short description of the testcase.
$description = "Resize landscape image";
// Use these images in the test
$images = array(
'car.png',
);
// For each image, apply these testcases
$nc = empty($_SERVER['QUERY_STRING']) ? "" : "&" . $_SERVER['QUERY_STRING'];
$testcase = array(
$nc . '&w=500',
$nc . '&h=200',
$nc . '&w=500&h=500',
$nc . '&w=500&h=200',
$nc . '&w=500&h=200&crop-to-fit',
$nc . '&w=200&h=500&crop-to-fit',
$nc . '&w=500&h=200&fill-to-fit',
$nc . '&w=200&h=500&fill-to-fit',
$nc . '&w=500&h=200&stretch',
$nc . '&w=200&h=500&stretch',
);
// Apply testcases and present results
include __DIR__ . "/template.php";

View File

@@ -0,0 +1,43 @@
<?php
// Include config for all testcases
include __DIR__ . "/config.php";
// The title of the test case
$title = "Testing resize portrait image";
// Provide a short description of the testcase.
$description = "Resize portrait image";
// Use these images in the test
$images = array(
'kodim04.png',
);
// For each image, apply these testcases
$nc = empty($_SERVER['QUERY_STRING']) ? "" : "&" . $_SERVER['QUERY_STRING'];
$testcase = array(
$nc . '&w=500',
$nc . '&h=200',
$nc . '&w=500&h=500',
$nc . '&w=500&h=200',
$nc . '&w=500&h=200&crop-to-fit',
$nc . '&w=200&h=500&crop-to-fit',
$nc . '&w=500&h=200&fill-to-fit',
$nc . '&w=200&h=500&fill-to-fit',
$nc . '&w=500&h=200&stretch',
$nc . '&w=200&h=500&stretch',
);
// Apply testcases and present results
include __DIR__ . "/template.php";