1
0
mirror of https://github.com/mosbth/cimage.git synced 2025-07-24 02:01:23 +02:00

made passing behat

This commit is contained in:
Mikael Roos
2017-03-03 22:53:29 +01:00
parent 0f0d954e61
commit 026e01b2cd
15 changed files with 180 additions and 98 deletions

View File

@@ -2,13 +2,11 @@
<ruleset name="PHPCS rule set"> <ruleset name="PHPCS rule set">
<description>Custom rule set.</description> <description>Custom rule set.</description>
<file>.</file> <file>src</file>
<file>test</file> <file>test</file>
<file>autoload.php</file>
<exclude-pattern>docs/*</exclude-pattern> <exclude-pattern>docs/*</exclude-pattern>
<exclude-pattern>build/*</exclude-pattern> <exclude-pattern>build/*</exclude-pattern>
<exclude-pattern>coverage/*</exclude-pattern>
<exclude-pattern>test/config.php</exclude-pattern> <exclude-pattern>test/config.php</exclude-pattern>
<exclude-pattern>webroot/imgs.php</exclude-pattern> <exclude-pattern>webroot/imgs.php</exclude-pattern>
<exclude-pattern>webroot/imgp.php</exclude-pattern> <exclude-pattern>webroot/imgp.php</exclude-pattern>

View File

@@ -6,6 +6,8 @@ use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode; use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode; use Behat\Gherkin\Node\TableNode;
require __DIR__ . "/assert.php";
/** /**
* Defines application features from the specific context. * Defines application features from the specific context.
*/ */
@@ -76,7 +78,7 @@ class FeatureContext implements Context, SnippetAcceptingContext
{ {
//echo $this->url; //echo $this->url;
$res = file_get_contents($this->url); $res = file_get_contents($this->url);
PHPUnit_Framework_Assert::assertNotEquals(false, $res); assertNotEquals(false, $res);
$this->imageString = $res; $this->imageString = $res;
$this->headers = $http_response_header; $this->headers = $http_response_header;
@@ -94,10 +96,10 @@ class FeatureContext implements Context, SnippetAcceptingContext
public function getImageAsJson() public function getImageAsJson()
{ {
$res = file_get_contents($this->url . "&json"); $res = file_get_contents($this->url . "&json");
PHPUnit_Framework_Assert::assertNotEquals(false, $res); assertNotEquals(false, $res);
$res = json_decode($res, true); $res = json_decode($res, true);
PHPUnit_Framework_Assert::assertNotEquals(null, $res); assertNotEquals(null, $res);
$this->imageJSON = $res; $this->imageJSON = $res;
} }
@@ -111,7 +113,7 @@ class FeatureContext implements Context, SnippetAcceptingContext
{ {
//echo $this->url; //echo $this->url;
$res = get_headers($this->url); $res = get_headers($this->url);
PHPUnit_Framework_Assert::assertNotEquals(false, $res); assertNotEquals(false, $res);
$this->headers = $http_response_header; $this->headers = $http_response_header;
} }
@@ -123,7 +125,7 @@ class FeatureContext implements Context, SnippetAcceptingContext
*/ */
public function returnsStatusCode($arg1) public function returnsStatusCode($arg1)
{ {
PHPUnit_Framework_Assert::assertNotEquals( assertNotEquals(
false, false,
strpos($this->headers[0], $arg1) strpos($this->headers[0], $arg1)
); );
@@ -136,19 +138,19 @@ class FeatureContext implements Context, SnippetAcceptingContext
private function compareImageJsonToHeaders() private function compareImageJsonToHeaders()
{ {
$contentLength = "Content-Length: " . $this->imageJSON["size"]; $contentLength = "Content-Length: " . $this->imageJSON["size"];
PHPUnit_Framework_Assert::assertContains( assertContains(
$contentLength, $contentLength,
$this->headers $this->headers
); );
$contentType = "Content-Type: " . $this->imageJSON["mimeType"]; $contentType = "Content-Type: " . $this->imageJSON["mimeType"];
PHPUnit_Framework_Assert::assertContains( assertContains(
$contentType, $contentType,
$this->headers $this->headers
); );
$lastModified = "Last-Modified: " . $this->imageJSON["cacheGmdate"] . " GMT"; $lastModified = "Last-Modified: " . $this->imageJSON["cacheGmdate"] . " GMT";
PHPUnit_Framework_Assert::assertContains( assertContains(
$lastModified, $lastModified,
$this->headers $this->headers
); );
@@ -162,10 +164,10 @@ class FeatureContext implements Context, SnippetAcceptingContext
private function compareImageJsonToSavedJson($file) private function compareImageJsonToSavedJson($file)
{ {
$res = file_get_contents("$file.json"); $res = file_get_contents("$file.json");
PHPUnit_Framework_Assert::assertNotEquals(false, $res); assertNotEquals(false, $res);
$res = json_decode($res, true); $res = json_decode($res, true);
PHPUnit_Framework_Assert::assertNotEquals(null, $res); assertNotEquals(null, $res);
$keys = [ $keys = [
"mimeType", "mimeType",
@@ -179,7 +181,7 @@ class FeatureContext implements Context, SnippetAcceptingContext
if (array_key_exists($key, $res) if (array_key_exists($key, $res)
&& array_key_exists($key, $this->imageJSON) && array_key_exists($key, $this->imageJSON)
) { ) {
PHPUnit_Framework_Assert::assertEquals( assertEquals(
$res[$key], $res[$key],
$this->imageJSON[$key] $this->imageJSON[$key]
); );
@@ -196,9 +198,9 @@ class FeatureContext implements Context, SnippetAcceptingContext
{ {
$base = __DIR__ . "/../img"; $base = __DIR__ . "/../img";
$res = file_get_contents("$base/$arg1"); $res = file_get_contents("$base/$arg1");
PHPUnit_Framework_Assert::assertNotEquals(false, $res); assertNotEquals(false, $res);
PHPUnit_Framework_Assert::assertEquals($this->imageString, $res); assertEquals($this->imageString, $res);
$this->compareImageJsonToHeaders(); $this->compareImageJsonToHeaders();
$this->compareImageJsonToSavedJson("$base/$arg1"); $this->compareImageJsonToSavedJson("$base/$arg1");

View File

@@ -0,0 +1,32 @@
<?php
/**
* Assert functions.
*/
function assertNotEquals($expected, $actual)
{
if (!($expected !== $actual)) {
throw new Exception("Failed asserting that '$expected' is not equal to '$actual'.");
}
};
function assertEquals($expected, $actual)
{
if (!($expected === $actual)) {
throw new Exception("Failed asserting that '$expected' is equal to '$actual'.");
}
};
/**
* Check that $needle is an element of $haystack.
*/
function assertContains($needle, $haystack)
{
if (!in_array($needle, $haystack)) {
throw new Exception("Failed asserting that '$needle' is not in haystack.");
}
}

View File

@@ -1220,7 +1220,10 @@ class CImage
*/ */
public function reCalculateDimensions() public function reCalculateDimensions()
{ {
$this->log("Re-calculate image dimensions, newWidth x newHeigh was: " . $this->newWidth . " x " . $this->newHeight); $this->log("Re-calculate image dimensions, newWidth x newHeigh was: "
. $this->newWidth
. " x "
. $this->newHeight);
$this->newWidth = $this->newWidthOrig; $this->newWidth = $this->newWidthOrig;
$this->newHeight = $this->newHeightOrig; $this->newHeight = $this->newHeightOrig;
@@ -1586,7 +1589,6 @@ class CImage
} }
switch ($pngType) { switch ($pngType) {
case self::PNG_GREYSCALE: case self::PNG_GREYSCALE:
$text = "PNG is type 0, Greyscale$transparent"; $text = "PNG is type 0, Greyscale$transparent";
break; break;
@@ -1754,7 +1756,6 @@ class CImage
// Only use a specified area of the image, $this->offset is defining the area to use // Only use a specified area of the image, $this->offset is defining the area to use
if (isset($this->offset)) { if (isset($this->offset)) {
$this->log("Offset for area to use, cropping it width={$this->offset['width']}, height={$this->offset['height']}, start_x={$this->offset['left']}, start_y={$this->offset['top']}"); $this->log("Offset for area to use, cropping it width={$this->offset['width']}, height={$this->offset['height']}, start_x={$this->offset['left']}, start_y={$this->offset['top']}");
$img = $this->CreateImageKeepTransparency($this->offset['width'], $this->offset['height']); $img = $this->CreateImageKeepTransparency($this->offset['width'], $this->offset['height']);
imagecopy($img, $this->image, 0, 0, $this->offset['left'], $this->offset['top'], $this->offset['width'], $this->offset['height']); imagecopy($img, $this->image, 0, 0, $this->offset['left'], $this->offset['top'], $this->offset['width'], $this->offset['height']);
@@ -1764,7 +1765,6 @@ class CImage
} }
if ($this->crop) { if ($this->crop) {
// Do as crop, take only part of image // Do as crop, take only part of image
$this->log("Cropping area width={$this->crop['width']}, height={$this->crop['height']}, start_x={$this->crop['start_x']}, start_y={$this->crop['start_y']}"); $this->log("Cropping area width={$this->crop['width']}, height={$this->crop['height']}, start_x={$this->crop['start_x']}, start_y={$this->crop['start_y']}");
$img = $this->CreateImageKeepTransparency($this->crop['width'], $this->crop['height']); $img = $this->CreateImageKeepTransparency($this->crop['width'], $this->crop['height']);
@@ -1781,11 +1781,10 @@ class CImage
} }
if ($this->cropToFit) { if ($this->cropToFit) {
// Resize by crop to fit // Resize by crop to fit
$this->log("Resizing using strategy - Crop to fit"); $this->log("Resizing using strategy - Crop to fit");
if (!$this->upscale if (!$this->upscale
&& ($this->width < $this->newWidth || $this->height < $this->newHeight)) { && ($this->width < $this->newWidth || $this->height < $this->newHeight)) {
$this->log("Resizing - smaller image, do not upscale."); $this->log("Resizing - smaller image, do not upscale.");
@@ -1842,9 +1841,7 @@ class CImage
$this->image = $imageResized; $this->image = $imageResized;
$this->width = $this->newWidth; $this->width = $this->newWidth;
$this->height = $this->newHeight; $this->height = $this->newHeight;
} elseif ($this->fillToFit) { } elseif ($this->fillToFit) {
// Resize by fill to fit // Resize by fill to fit
$this->log("Resizing using strategy - Fill to fit"); $this->log("Resizing using strategy - Fill to fit");
@@ -1864,13 +1861,11 @@ class CImage
if (!$this->upscale if (!$this->upscale
&& ($this->width < $this->newWidth && $this->height < $this->newHeight) && ($this->width < $this->newWidth && $this->height < $this->newHeight)
) { ) {
$this->log("Resizing - smaller image, do not upscale."); $this->log("Resizing - smaller image, do not upscale.");
$posX = round(($this->newWidth - $this->width) / 2); $posX = round(($this->newWidth - $this->width) / 2);
$posY = round(($this->newHeight - $this->height) / 2); $posY = round(($this->newHeight - $this->height) / 2);
$imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight);
imagecopy($imageResized, $this->image, $posX, $posY, 0, 0, $this->width, $this->height); imagecopy($imageResized, $this->image, $posX, $posY, 0, 0, $this->width, $this->height);
} else { } else {
$imgPreFill = $this->CreateImageKeepTransparency($this->fillWidth, $this->fillHeight); $imgPreFill = $this->CreateImageKeepTransparency($this->fillWidth, $this->fillHeight);
$imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight);
@@ -1881,9 +1876,7 @@ class CImage
$this->image = $imageResized; $this->image = $imageResized;
$this->width = $this->newWidth; $this->width = $this->newWidth;
$this->height = $this->newHeight; $this->height = $this->newHeight;
} elseif (!($this->newWidth == $this->width && $this->newHeight == $this->height)) { } elseif (!($this->newWidth == $this->width && $this->newHeight == $this->height)) {
// Resize it // Resize it
$this->log("Resizing, new height and/or width"); $this->log("Resizing, new height and/or width");
@@ -1948,12 +1941,10 @@ class CImage
// Apply filters // Apply filters
if (isset($this->filters) && is_array($this->filters)) { if (isset($this->filters) && is_array($this->filters)) {
foreach ($this->filters as $filter) { foreach ($this->filters as $filter) {
$this->log("Applying filter {$filter['type']}."); $this->log("Applying filter {$filter['type']}.");
switch ($filter['argc']) { switch ($filter['argc']) {
case 0: case 0:
imagefilter($this->image, $filter['type']); imagefilter($this->image, $filter['type']);
break; break;
@@ -2298,7 +2289,6 @@ class CImage
$img = isset($img) ? $img : $this->image; $img = isset($img) ? $img : $this->image;
if ($this->bgColorDefault) { if ($this->bgColorDefault) {
$red = $this->bgColorDefault['red']; $red = $this->bgColorDefault['red'];
$green = $this->bgColorDefault['green']; $green = $this->bgColorDefault['green'];
$blue = $this->bgColorDefault['blue']; $blue = $this->bgColorDefault['blue'];
@@ -2311,7 +2301,6 @@ class CImage
} }
return $color; return $color;
} else { } else {
return 0; return 0;
} }
@@ -2339,16 +2328,13 @@ class CImage
: -1; : -1;
if ($index != -1) { if ($index != -1) {
imagealphablending($img, true); imagealphablending($img, true);
$transparent = imagecolorsforindex($this->image, $index); $transparent = imagecolorsforindex($this->image, $index);
$color = imagecolorallocatealpha($img, $transparent['red'], $transparent['green'], $transparent['blue'], $transparent['alpha']); $color = imagecolorallocatealpha($img, $transparent['red'], $transparent['green'], $transparent['blue'], $transparent['alpha']);
imagefill($img, 0, 0, $color); imagefill($img, 0, 0, $color);
$index = imagecolortransparent($img, $color); $index = imagecolortransparent($img, $color);
$this->Log("Detected transparent color = " . implode(", ", $transparent) . " at index = $index"); $this->Log("Detected transparent color = " . implode(", ", $transparent) . " at index = $index");
} elseif ($this->bgColorDefault) { } elseif ($this->bgColorDefault) {
$color = $this->getBackgroundColor($img); $color = $this->getBackgroundColor($img);
imagefill($img, 0, 0, $color); imagefill($img, 0, 0, $color);
$this->Log("Filling image with background color."); $this->Log("Filling image with background color.");
@@ -2374,7 +2360,7 @@ class CImage
$this->jpegOptimizeCmd = null; $this->jpegOptimizeCmd = null;
} }
if (array_key_exists("png_lossy", $options) if (array_key_exists("png_lossy", $options)
&& $options['png_lossy'] !== false) { && $options['png_lossy'] !== false) {
$this->pngLossy = $options['png_lossy']; $this->pngLossy = $options['png_lossy'];
$this->pngLossyCmd = $options['png_lossy_cmd']; $this->pngLossyCmd = $options['png_lossy_cmd'];
@@ -2444,7 +2430,6 @@ class CImage
$type = $this->getTargetImageExtension(); $type = $this->getTargetImageExtension();
$this->Log("Saving image as " . $type); $this->Log("Saving image as " . $type);
switch ($type) { switch ($type) {
case 'jpeg': case 'jpeg':
case 'jpg': case 'jpg':
$this->Log("Saving image as JPEG to cache using quality = {$this->quality}."); $this->Log("Saving image as JPEG to cache using quality = {$this->quality}.");
@@ -2716,7 +2701,6 @@ class CImage
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
&& strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified) { && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified) {
if ($this->verbose) { if ($this->verbose) {
$this->log("304 not modified"); $this->log("304 not modified");
$this->verboseOutput(); $this->verboseOutput();
@@ -2727,9 +2711,7 @@ class CImage
if (CIMAGE_DEBUG) { if (CIMAGE_DEBUG) {
trace(__CLASS__ . " 304"); trace(__CLASS__ . " 304");
} }
} else { } else {
$this->loadImageDetails($file); $this->loadImageDetails($file);
$mime = $this->getMimeType(); $mime = $this->getMimeType();
$size = filesize($file); $size = filesize($file);

View File

@@ -385,7 +385,6 @@ class CImageResizer
// Both null, use source as base for target // Both null, use source as base for target
if (is_null($this->targetWidth) && is_null($this->targetHeight)) { if (is_null($this->targetWidth) && is_null($this->targetHeight)) {
$this->targetWidth = ($this->aspectRatio >= 1) $this->targetWidth = ($this->aspectRatio >= 1)
? $this->srcWidth ? $this->srcWidth
: null; : null;
@@ -395,12 +394,10 @@ class CImageResizer
: $this->srcHeight; : $this->srcHeight;
$this->log(" Using source as base {$this->targetWidth}x{$this->targetHeight}"); $this->log(" Using source as base {$this->targetWidth}x{$this->targetHeight}");
} }
// Both or either set, calculate the other // Both or either set, calculate the other
if (isset($this->targetWidth) && isset($this->targetHeight)) { if (isset($this->targetWidth) && isset($this->targetHeight)) {
$this->targetWidth = ($this->aspectRatio >= 1) $this->targetWidth = ($this->aspectRatio >= 1)
? $this->targetWidth ? $this->targetWidth
: $this->targetHeight * $this->aspectRatio; : $this->targetHeight * $this->aspectRatio;
@@ -410,17 +407,12 @@ class CImageResizer
: $this->targetHeight; : $this->targetHeight;
$this->log(" New target width height {$this->targetWidth}x{$this->targetHeight}"); $this->log(" New target width height {$this->targetWidth}x{$this->targetHeight}");
} elseif (isset($this->targetWidth)) { } elseif (isset($this->targetWidth)) {
$this->targetHeight = $this->targetWidth / $this->aspectRatio; $this->targetHeight = $this->targetWidth / $this->aspectRatio;
$this->log(" New target height x{$this->targetHeight}"); $this->log(" New target height x{$this->targetHeight}");
} elseif (isset($this->targetHeight)) { } elseif (isset($this->targetHeight)) {
$this->targetWidth = $this->targetHeight * $this->aspectRatio; $this->targetWidth = $this->targetHeight * $this->aspectRatio;
$this->log(" New target width {$this->targetWidth}x"); $this->log(" New target width {$this->targetWidth}x");
} }
return $this; return $this;
@@ -518,28 +510,21 @@ class CImageResizer
$ratio = $both ? $tw / $th : null; $ratio = $both ? $tw / $th : null;
if (is_null($tw) && is_null($th)) { if (is_null($tw) && is_null($th)) {
// No tw/th use sw/sh // No tw/th use sw/sh
$tw = $sw; $tw = $sw;
$th = $sh; $th = $sh;
$this->log(" New tw x th {$tw}x{$th}"); $this->log(" New tw x th {$tw}x{$th}");
} elseif (isset($tw) && is_null($th)) { } elseif (isset($tw) && is_null($th)) {
// Keep aspect ratio, make th based on tw // Keep aspect ratio, make th based on tw
$this->respectUpscale($tw, $sw); $this->respectUpscale($tw, $sw);
$th = $tw / $ar; $th = $tw / $ar;
$this->log(" New th x{$th}"); $this->log(" New th x{$th}");
} elseif (is_null($tw) && isset($th)) { } elseif (is_null($tw) && isset($th)) {
// Keep aspect ratio, make tw based on th // Keep aspect ratio, make tw based on th
$this->respectUpscale($th, $sh); $this->respectUpscale($th, $sh);
$tw = $th * $ar; $tw = $th * $ar;
$this->log(" New tw {$tw}x"); $this->log(" New tw {$tw}x");
} elseif ($rs === CImageResizer::KEEP_RATIO && $both) { } elseif ($rs === CImageResizer::KEEP_RATIO && $both) {
// Keep aspect ratio, make fit in box not larger than tw/th // Keep aspect ratio, make fit in box not larger than tw/th
$this->log(" Keep ratio, ratio target=$ratio, source=$ar"); $this->log(" Keep ratio, ratio target=$ratio, source=$ar");
@@ -555,9 +540,7 @@ class CImageResizer
$this->respectUpscale($tw, $sw); $this->respectUpscale($tw, $sw);
$this->respectUpscale($th, $sh); $this->respectUpscale($th, $sh);
} }
} elseif ($rs === CImageResizer::STRETCH && $both) { } elseif ($rs === CImageResizer::STRETCH && $both) {
// Stretch to fit, leave as is // Stretch to fit, leave as is
$this->log(" Stretch"); $this->log(" Stretch");
@@ -570,9 +553,7 @@ class CImageResizer
$dy = ($th - $dh) / 2; $dy = ($th - $dh) / 2;
$this->log(" Destination area dx=$dx, dy=$dy, dw=$dw, dh=$dh"); $this->log(" Destination area dx=$dx, dy=$dy, dw=$dw, dh=$dh");
} elseif ($rs === CImageResizer::CROP_TO_FIT && $both) { } elseif ($rs === CImageResizer::CROP_TO_FIT && $both) {
// Crop to fit image in box // Crop to fit image in box
$this->log(" Crop to fit, ratio target=$ratio, source=$ar"); $this->log(" Crop to fit, ratio target=$ratio, source=$ar");
@@ -607,9 +588,7 @@ class CImageResizer
$this->log(" Parts cx=$cx, cy=$cy, cw=$cw, ch=$ch"); $this->log(" Parts cx=$cx, cy=$cy, cw=$cw, ch=$ch");
$this->log(" Destination area dx=$dx, dy=$dy, dw=$dw, dh=$dh"); $this->log(" Destination area dx=$dx, dy=$dy, dw=$dw, dh=$dh");
} elseif ($rs === CImageResizer::FILL_TO_FIT && $both) { } elseif ($rs === CImageResizer::FILL_TO_FIT && $both) {
// Fill to fit image in box // Fill to fit image in box
$this->log(" Fill to fit, ratio target=$ratio, source=$ar"); $this->log(" Fill to fit, ratio target=$ratio, source=$ar");
$dw = $tw; $dw = $tw;
@@ -647,10 +626,12 @@ class CImageResizer
$this->cropWidth = round($cw); $this->cropWidth = round($cw);
$this->cropHeight = round($ch); $this->cropHeight = round($ch);
$this->log(" Target dimension (after) {$this->targetWidth}x{$this->targetHeight}."); $str = <<<EOD
$this->log(" Crop area {$this->cropX}x{$this->cropY} by {$this->cropWidth}x{$this->cropHeight}."); Target dimension (after) {$this->targetWidth}x{$this->targetHeight}.
$this->log(" Destination area {$this->destinationX}x{$this->destinationY} by {$this->destinationWidth}x{$this->destinationHeight}."); Crop area {$this->cropX}x{$this->cropY} by {$this->cropWidth}x{$this->cropHeight}.
Destination area {$this->destinationX}x{$this->destinationY} by {$this->destinationWidth}x{$this->destinationHeight}.
EOD;
$this->log($str);
/* /*

22
src/CImage/Exception.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
namespace Mos\CImage;
/**
* Anax class for wrapping sessions.
*
*/
class Exception extends \Exception
{
/**
* Construct.
*
* @param string $message the Exception message to throw.
* @param int $code the Exception code.
* @param Exception previous the previous exception used for the exception chaining.
*/
public function __construct($message = "", $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
}
}

View File

@@ -67,22 +67,6 @@ function errorPage($msg, $type = 500)
/**
* Custom exception handler.
*/
set_exception_handler(function ($exception) {
errorPage(
"<p><b>img.php: Uncaught exception:</b> <p>"
. $exception->getMessage()
. "</p><pre>"
. $exception->getTraceAsString()
. "</pre>",
500
);
});
/** /**
* Get input from query string or return default value if not set. * Get input from query string or return default value if not set.
* *

View File

@@ -50,8 +50,15 @@ class CImageResizerByAspectRatioTest extends \PHPUnit_Framework_TestCase
* *
* @return void * @return void
*/ */
public function testResize1($aspectRatio, $srcWidth, $srcHeight, $targetWidth, $targetHeight, $expectedWidth, $expectedHeight) public function testResize1(
{ $aspectRatio,
$srcWidth,
$srcHeight,
$targetWidth,
$targetHeight,
$expectedWidth,
$expectedHeight
) {
$img = new CImageResizer(/*'logger'*/); $img = new CImageResizer(/*'logger'*/);
//$img = new CImageResizer('logger'); //$img = new CImageResizer('logger');

View File

@@ -50,8 +50,15 @@ class CImageResizerByDevicePixelRatioTest extends \PHPUnit_Framework_TestCase
* *
* @return void * @return void
*/ */
public function testResize1($dpr, $srcWidth, $srcHeight, $targetWidth, $targetHeight, $expectedWidth, $expectedHeight) public function testResize1(
{ $dpr,
$srcWidth,
$srcHeight,
$targetWidth,
$targetHeight,
$expectedWidth,
$expectedHeight
) {
$img = new CImageResizer(/*'logger'*/); $img = new CImageResizer(/*'logger'*/);
//$img = new CImageResizer('logger'); //$img = new CImageResizer('logger');

View File

@@ -107,7 +107,7 @@ class CImageResizerStrategyCropToFitTest extends \PHPUnit_Framework_TestCase
*/ */
public function testResize2($sw, $sh, $tw, $th, $cx, $cy, $cw, $ch, $dx, $dy, $dw, $dh) public function testResize2($sw, $sh, $tw, $th, $cx, $cy, $cw, $ch, $dx, $dy, $dw, $dh)
{ {
$img = new CImageResizer(/*'logger'/**/); $img = new CImageResizer(/*'logger'/**/);
$img->setSource($sw, $sh) $img->setSource($sw, $sh)
->setBaseWidthHeight($tw, $th) ->setBaseWidthHeight($tw, $th)

View File

@@ -62,7 +62,6 @@ class CImageResizerStrategyFillToFitTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($dy, $img->getDestinationY(), "DestinationY not correct."); $this->assertEquals($dy, $img->getDestinationY(), "DestinationY not correct.");
$this->assertEquals($dw, $img->getDestinationWidth(), "DestinationWidth not correct."); $this->assertEquals($dw, $img->getDestinationWidth(), "DestinationWidth not correct.");
$this->assertEquals($dh, $img->getDestinationHeight(), "DestinationHeight not correct."); $this->assertEquals($dh, $img->getDestinationHeight(), "DestinationHeight not correct.");
} }
@@ -116,6 +115,5 @@ class CImageResizerStrategyFillToFitTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($dy, $img->getDestinationY(), "DestinationY not correct."); $this->assertEquals($dy, $img->getDestinationY(), "DestinationY not correct.");
$this->assertEquals($dw, $img->getDestinationWidth(), "DestinationWidth not correct."); $this->assertEquals($dw, $img->getDestinationWidth(), "DestinationWidth not correct.");
$this->assertEquals($dh, $img->getDestinationHeight(), "DestinationHeight not correct."); $this->assertEquals($dh, $img->getDestinationHeight(), "DestinationHeight not correct.");
} }
} }

View File

@@ -53,6 +53,5 @@ class CImageResizerStrategyStretchTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($tw, $img->getTargetWidth(), "Target width not correct."); $this->assertEquals($tw, $img->getTargetWidth(), "Target width not correct.");
$this->assertEquals($th, $img->getTargetHeight(), "Target height not correct."); $this->assertEquals($th, $img->getTargetHeight(), "Target height not correct.");
} }
} }

54
test/CImgTest.php Normal file
View File

@@ -0,0 +1,54 @@
<?php
namespace Mos\CImage;
/**
* A testclass for img.php
*
*/
class CImgTest extends \PHPUnit_Framework_TestCase
{
/**
* Provider
*
* @return array
*/
public function providerQueryString()
{
return [
//
[[
"src" => "car.png",
"json" => true,
"rotate" => 90,
]],
];
}
/**
* Test
*
* @-preserveGlobalState disabled
* @runInSeparateProcess
*
* @dataProvider providerQueryString
*
* @return void
*/
public function testResize($query)
{
//$_GET = $query;
#ob_start();
//$res = require "webroot/img.php";
#$res = ob_get_clean();
//echo "MOPED $res";
}
}

View File

@@ -8,6 +8,22 @@
* *
*/ */
/**
* Custom exception handler.
*/
set_exception_handler(function ($exception) {
errorPage(
"<p><b>img.php: Uncaught exception:</b> <p>"
. $exception->getMessage()
. "</p><pre>"
. $exception->getTraceAsString()
. "</pre>",
500
);
});
/** /**
* Get configuration options from file, if the file exists, else use $config * Get configuration options from file, if the file exists, else use $config
* if its defined or create an empty $config. * if its defined or create an empty $config.
@@ -198,7 +214,7 @@ verbose("referer host = $refererHost");
/** /**
* Create the class for the image. * Create the class for the image.
*/ */
$CImage = getConfig('CImage', 'CImage'); $CImage = getConfig('CImage', '\Mos\CImage\CImage');
$img = new $CImage(); $img = new $CImage();
$img->setVerbose($verbose || $verboseFile); $img->setVerbose($verbose || $verboseFile);
@@ -207,7 +223,7 @@ $img->setVerbose($verbose || $verboseFile);
/** /**
* Get the cachepath from config. * Get the cachepath from config.
*/ */
$CCache = getConfig('CCache', 'CCache'); $CCache = getConfig('CCache', '\Mos\CImage\CCache');
$cachePath = getConfig('cache_path', __DIR__ . '/../cache/'); $cachePath = getConfig('cache_path', __DIR__ . '/../cache/');
$cache = new $CCache(); $cache = new $CCache();
$cache->setDir($cachePath); $cache->setDir($cachePath);
@@ -229,7 +245,7 @@ verbose("use cache = $useCache");
$fastTrackCache = "fasttrack"; $fastTrackCache = "fasttrack";
$allowFastTrackCache = getConfig('fast_track_allow', false); $allowFastTrackCache = getConfig('fast_track_allow', false);
$CFastTrackCache = getConfig('CFastTrackCache', 'CFastTrackCache'); $CFastTrackCache = getConfig('CFastTrackCache', '\Mos\CImage\CFastTrackCache');
$ftc = new $CFastTrackCache(); $ftc = new $CFastTrackCache();
$ftc->setCacheDir($cache->getPathToSubdir($fastTrackCache)) $ftc->setCacheDir($cache->getPathToSubdir($fastTrackCache))
->enable($allowFastTrackCache) ->enable($allowFastTrackCache)

View File

@@ -91,13 +91,13 @@ return [
* the codebase. * the codebase.
* *
* Default values: * Default values:
* CImage: CImage * CImage: \Mos\CImage\CImage
* CCache: CCache * CCache: \Mos\CImage\CCache
* CFastTrackCache: CFastTrackCache * CFastTrackCache: \Mos\CImage\CFastTrackCache
*/ */
//'CImage' => 'CImage', //'CImage' => '\Mos\CImage\CImage',
//'CCache' => 'CCache', //'CCache' => '\Mos\CImage\CCache',
//'CFastTrackCache' => 'CFastTrackCache', //'CFastTrackCache' => '\Mos\CImage\CFastTrackCache',