diff options
author | Arnold Daniels <arnold@jasny.net> | 2014-01-26 00:52:45 +0100 |
---|---|---|
committer | Arnold Daniels <arnold@jasny.net> | 2014-01-26 00:52:45 +0100 |
commit | b5b63916537ad2315e80d472372f1280a09b53c9 (patch) | |
tree | 54d389bc4d7dd157324b87d63979ad33d8ef521d | |
parent | 2b58bcde5205956e4b439cfaa86fb88113442d72 (diff) | |
download | ImageWorkshop-b5b63916537ad2315e80d472372f1280a09b53c9.zip ImageWorkshop-b5b63916537ad2315e80d472372f1280a09b53c9.tar.gz ImageWorkshop-b5b63916537ad2315e80d472372f1280a09b53c9.tar.bz2 |
Added method ImageWorkshopLayer::resizeToFix()
This methods resizes an image to fit a bounding box.
-rw-r--r-- | src/PHPImageWorkshop/Core/ImageWorkshopLayer.php | 25 | ||||
-rw-r--r-- | tests/Core/ImageWorkshopLayerTest.php | 48 |
2 files changed, 71 insertions, 2 deletions
diff --git a/src/PHPImageWorkshop/Core/ImageWorkshopLayer.php b/src/PHPImageWorkshop/Core/ImageWorkshopLayer.php index 37944fd..621b871 100644 --- a/src/PHPImageWorkshop/Core/ImageWorkshopLayer.php +++ b/src/PHPImageWorkshop/Core/ImageWorkshopLayer.php @@ -686,6 +686,27 @@ class ImageWorkshopLayer }
/**
+ * Resize the layer to fit a bounding box by specifying pixel
+ *
+ * @param integer $width
+ * @param integer $height
+ * @param boolean $converseProportion
+ */
+ public function resizeToFit($width, $height, $converseProportion = false)
+ {
+ if ($this->getWidth() <= $width && $this->getHeight() <= $height) {
+ return;
+ }
+
+ if (!$converseProportion) {
+ $width = min($width, $this->getWidth());
+ $height = min($height, $this->getHeight());
+ }
+
+ $this->resize(self::UNIT_PIXEL, $width, $height, $converseProportion ? 2 : false);
+ }
+
+ /**
* Resize the layer
*
* @param string $unit Use one of `UNIT_*` constants, "UNIT_PIXEL" by default
@@ -744,7 +765,7 @@ class ImageWorkshopLayer }
}
- if ($this->getWidth() != $newWidth || $this->getHeight() != $newHeight) {
+ if ($converseProportion !== 2 && ($this->getWidth() != $newWidth || $this->getHeight() != $newHeight)) {
$layerTmp = ImageWorkshop::initVirginLayer($newWidth, $newHeight);
@@ -1074,7 +1095,7 @@ class ImageWorkshopLayer $this->cropInPixel($narrowSide, $narrowSide, $positionX, $positionY, $position);
}
-
+
/**
* Rotate the layer (in degree)
*
diff --git a/tests/Core/ImageWorkshopLayerTest.php b/tests/Core/ImageWorkshopLayerTest.php index b75d1cc..74a3025 100644 --- a/tests/Core/ImageWorkshopLayerTest.php +++ b/tests/Core/ImageWorkshopLayerTest.php @@ -912,6 +912,54 @@ class ImageWorkshopLayerTest extends \PHPUnit_Framework_TestCase }
/**
+ * Test resizeToFitInPixel
+ */
+ public function testResizeToFit()
+ {
+ $layer = $this->initializeLayer(1);
+
+ $layer->resizeToFit(20, 10);
+ $this->assertTrue($layer->getWidth() == 20, 'Expect $layer to have a width of 20px');
+ $this->assertTrue($layer->getHeight() == 10, 'Expect $layer to have a height of 10px');
+
+ $layer = $this->initializeLayer(1);
+
+ $layer->resizeToFit(120, 50);
+ $this->assertTrue($layer->getWidth() == 100, 'Expect $layer to have a width of 100px');
+ $this->assertTrue($layer->getHeight() == 50, 'Expect $layer to have a height of 50px');
+
+ $layer = $this->initializeLayer(1);
+
+ $layer->resizeToFit(60, 100);
+ $this->assertTrue($layer->getWidth() == 60, 'Expect $layer to have a width of 100px');
+ $this->assertTrue($layer->getHeight() == 75, 'Expect $layer to have a height of 50px');
+
+ $layer = $this->initializeLayer(1);
+
+ $layer->resizeToFit(120, 100);
+ $this->assertTrue($layer->getWidth() == 100, 'Expect $layer to have a width of 100px');
+ $this->assertTrue($layer->getHeight() == 75, 'Expect $layer to have a height of 75px');
+
+ $layer = $this->initializeLayer(1);
+
+ $layer->resizeToFit(20, 10, true);
+ $this->assertTrue($layer->getWidth() == 13, 'Expect $layer to have a width of 13px');
+ $this->assertTrue($layer->getHeight() == 10, 'Expect $layer to have a height of 10px');
+
+ $layer = $this->initializeLayer(1);
+
+ $layer->resizeToFit(20, 18, true);
+ $this->assertTrue($layer->getWidth() == 20, 'Expect $layer to have a width of 20px');
+ $this->assertTrue($layer->getHeight() == 15, 'Expect $layer to have a height of 15px');
+
+ $layer = $this->initializeLayer(1);
+
+ $layer->resizeToFit(120, 100, true);
+ $this->assertTrue($layer->getWidth() == 100, 'Expect $layer to have a width of 100px');
+ $this->assertTrue($layer->getHeight() == 75, 'Expect $layer to have a height of 75px');
+ }
+
+ /**
* Test resizeByLargestSideInPixel
*/
public function testResizeByLargestSideInPixel()
|