Skip to content

Added resizeToFit and cropToAspectRatio methods #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 100 additions & 2 deletions src/PHPImageWorkshop/Core/ImageWorkshopLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,27 @@ public function resizeInPercent($percentWidth = null, $percentHeight = null, $co
$this->resize(self::UNIT_PERCENT, $percentWidth, $percentHeight, $converseProportion, $positionX, $positionY, $position);
}

/**
* 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
*
Expand Down Expand Up @@ -744,7 +765,7 @@ public function resize($unit = self::UNIT_PIXEL, $newWidth = null, $newHeight =
}
}

if ($this->getWidth() != $newWidth || $this->getHeight() != $newHeight) {
if ($converseProportion !== 2 && ($this->getWidth() != $newWidth || $this->getHeight() != $newHeight)) {

$layerTmp = ImageWorkshop::initVirginLayer($newWidth, $newHeight);

Expand Down Expand Up @@ -1015,6 +1036,83 @@ public function crop($unit = self::UNIT_PIXEL, $width = 0, $height = 0, $positio
$this->updateLayerPositionsAfterCropping($layerNewPosX, $layerNewPosY);
}
}

/**
* Crop the document to a specific aspect ratio by specifying a shift in pixel
*
* $backgroundColor: can be set transparent (The script will be longer to execute)
* $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
*
* @param integer $width
* @param integer $height
* @param integer $positionX
* @param integer $positionY
* @param string $position
*/
public function cropToAspectRatioInPixel($width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = 'LT')
{
$this->cropToAspectRatio(self::UNIT_PIXEL, $width, $height, $positionX, $positionY, $position);
}

/**
* Crop the document to a specific aspect ratio by specifying a shift in percent
*
* $backgroundColor can be set transparent (but script could be long to execute)
* $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
*
* @param integer $width
* @param integer $height
* @param float $positionXPercent
* @param float $positionYPercent
* @param string $position
*/
public function cropToAspectRatioInPercent($width = 0, $height = 0, $positionXPercent = 0, $positionYPercent = 0, $position = 'LT')
{
$this->cropToAspectRatio(self::UNIT_PERCENT, $width, $height, $positionXPercent, $positionYPercent, $position);
}

/**
* Crop the document to a specific aspect ratio
*
* $backgroundColor can be set transparent (but script could be long to execute)
* $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html
*
* @param string $unit
* @param integer $width (integer or float)
* @param integer $height (integer or float)
* @param mixed $positionX (integer or float)
* @param mixed $positionY (integer or float)
* @param string $position
*/
public function cropToAspectRatio($unit = self::UNIT_PIXEL, $width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = 'LT')
{
if ($width < 0 || $height < 0) {
throw new ImageWorkshopLayerException('You can\'t use negative $width or $height for "'.__METHOD__.'" method.', static::ERROR_NEGATIVE_NUMBER_USED);
}

if ($width == 0) {
$width = 1;
}

if ($height == 0) {
$height = 1;
}

if ($this->width / $this->height <= $width / $height) {
$newWidth = $this->width;
$newHeight = round($height * ($this->width / $width));
} else {
$newWidth = round($width * ($this->height / $height));
$newHeight = $this->height;
}

if ($unit == self::UNIT_PERCENT) {
$positionX = round(($positionX / 100) * ($this->width - $newWidth));
$positionY = round(($positionY / 100) * ($this->height - $newHeight));
}

$this->cropInPixel($newWidth, $newHeight, $positionX, $positionY, $position);
}

/**
* Crop the maximum possible from left top ("LT"), "RT"... by specifying a shift in pixel
Expand Down Expand Up @@ -1074,7 +1172,7 @@ public function cropMaximum($unit = self::UNIT_PIXEL, $positionX = 0, $positionY

$this->cropInPixel($narrowSide, $narrowSide, $positionX, $positionY, $position);
}

/**
* Rotate the layer (in degree)
*
Expand Down
166 changes: 166 additions & 0 deletions tests/Core/ImageWorkshopLayerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,54 @@ public function testResizeInPercent()
$this->assertTrue($layer->getHeight() == 1, 'Expect $layer to have a height of 1px');
}

/**
* 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
*/
Expand Down Expand Up @@ -1145,6 +1193,124 @@ public function testCropInPercent()
$layer->cropInPercent(-1, -1, 0, 0, 'LT');
}

/**
* Test cropToAspectRatioInPixel
*/
public function testCropToAspectRatioInPixel()
{
// Test larger width

$layer = $this->initializeLayer(1);

$layer->cropToAspectRatioInPixel(50, 30, 0, 0, 'LT');
$this->assertTrue($layer->getWidth() == 100, 'Expect $layer to have a width of 100px');
$this->assertTrue($layer->getHeight() == 60, 'Expect $layer to have a height of 60px');

$layer = $this->initializeLayer(1);

$layer->cropToAspectRatioInPixel(50, 30, 20, 20, 'LT');
$this->assertTrue($layer->getWidth() == 100, 'Expect $layer to have a width of 100px');
$this->assertTrue($layer->getHeight() == 60, 'Expect $layer to have a height of 60px');

$layer = $this->initializeLayer(1);

$layer->cropToAspectRatioInPixel(50, 30, -20, -20, 'LT');
$this->assertTrue($layer->getWidth() == 100, 'Expect $layer to have a width of 100px');
$this->assertTrue($layer->getHeight() == 60, 'Expect $layer to have a height of 60px');

// Test larger than initial width

$layer = $this->initializeLayer(1);

$layer->cropToAspectRatioInPixel(60, 50, 0, 0, 'LT');
$this->assertTrue($layer->getWidth() == 90, 'Expect $layer to have a width of 90px');
$this->assertTrue($layer->getHeight() == 75, 'Expect $layer to have a height of 75px');

$layer = $this->initializeLayer(1);

$layer->cropToAspectRatioInPixel(60, 50, 20, 20, 'LT');
$this->assertTrue($layer->getWidth() == 90, 'Expect $layer to have a width of 90px');
$this->assertTrue($layer->getHeight() == 75, 'Expect $layer to have a height of 75px');

$layer = $this->initializeLayer(1);

$layer->cropToAspectRatioInPixel(60, 50, -20, -20, 'LT');
$this->assertTrue($layer->getWidth() == 90, 'Expect $layer to have a width of 90px');
$this->assertTrue($layer->getHeight() == 75, 'Expect $layer to have a height of 75px');

$layer = $this->initializeLayer(1);

$layer->cropToAspectRatioInPixel(0, 0, 0, 0, 'LT');
$this->assertTrue($layer->getWidth() == 75, 'Expect $layer to have a width of 75px');
$this->assertTrue($layer->getHeight() == 75, 'Expect $layer to have a height of 75px');

// Test negative

$layer = $this->initializeLayer(1);

$this->setExpectedException('PHPImageWorkshop\Core\Exception\ImageWorkshopLayerException');
$layer->cropToAspectRatioInPixel(-1, -1, 0, 0, 'LT');
}

/**
* Test cropToAspectRatioInPercent
*/
public function testCropToAspectRatioInPercent()
{
// Test larger width

$layer = $this->initializeLayer(1);

$layer->cropToAspectRatioInPercent(50, 30, 0, 0, 'LT');
$this->assertTrue($layer->getWidth() == 100, 'Expect $layer to have a width of 100px');
$this->assertTrue($layer->getHeight() == 60, 'Expect $layer to have a height of 60px');

$layer = $this->initializeLayer(1);

$layer->cropToAspectRatioInPercent(50, 30, 20, 20, 'LT');
$this->assertTrue($layer->getWidth() == 100, 'Expect $layer to have a width of 100px');
$this->assertTrue($layer->getHeight() == 60, 'Expect $layer to have a height of 60px');

$layer = $this->initializeLayer(1);

$layer->cropToAspectRatioInPercent(50, 30, -20, -20, 'LT');
$this->assertTrue($layer->getWidth() == 100, 'Expect $layer to have a width of 100px');
$this->assertTrue($layer->getHeight() == 60, 'Expect $layer to have a height of 60px');

// Test larger than initial width

$layer = $this->initializeLayer(1);

$layer->cropToAspectRatioInPercent(60, 50, 0, 0, 'LT');
$this->assertTrue($layer->getWidth() == 90, 'Expect $layer to have a width of 90px');
$this->assertTrue($layer->getHeight() == 75, 'Expect $layer to have a height of 75px');

$layer = $this->initializeLayer(1);

$layer->cropToAspectRatioInPercent(60, 50, 20, 20, 'LT');
$this->assertTrue($layer->getWidth() == 90, 'Expect $layer to have a width of 90px');
$this->assertTrue($layer->getHeight() == 75, 'Expect $layer to have a height of 75px');

$layer = $this->initializeLayer(1);

$layer->cropToAspectRatioInPercent(60, 50, -20, -20, 'LT');
$this->assertTrue($layer->getWidth() == 90, 'Expect $layer to have a width of 90px');
$this->assertTrue($layer->getHeight() == 75, 'Expect $layer to have a height of 75px');

$layer = $this->initializeLayer(1);

$layer->cropToAspectRatioInPercent(0, 0, 0, 0, 'LT');
$this->assertTrue($layer->getWidth() == 75, 'Expect $layer to have a width of 75px');
$this->assertTrue($layer->getHeight() == 75, 'Expect $layer to have a height of 75px');

// Test negative

$layer = $this->initializeLayer(1);

$this->setExpectedException('PHPImageWorkshop\Core\Exception\ImageWorkshopLayerException');
$layer->cropToAspectRatioInPercent(-1, -1, 0, 0, 'LT');
}

/**
* Test cropMaximumInPixel
*/
Expand Down