Skip to content

Added method resizeToBestFit #26

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
2 commits merged into from
Jun 22, 2015
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ $image->resizeToWidth(300);
$image->save('image2.jpg');
```

To resize an image to best fit a given set of dimensions (keeping aspet ratio):
```php
$image = new ImageResize('image.jpg');
$image->resizeToBestFit(500, 300);
$image->save('image2.jpg');
```

To to crop an image:

```php
Expand Down
30 changes: 28 additions & 2 deletions src/ImageResize.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ImageResize
/**
* Create instance from a strng
*
* @param string $imageData
* @param string $image_data
* @return ImageResize
* @throws \exception
*/
Expand Down Expand Up @@ -251,10 +251,36 @@ public function resizeToWidth($width, $allow_enlarge = false)
return $this;
}

/**
* Resizes image to best fit inside the given dimensions
*
* @param integer $max_width
* @param integer $max_height
* @param boolean $allow_enlarge
* @return \static
*/
public function resizeToBestFit($max_width, $max_height, $allow_enlarge = false)
{
if($this->getSourceWidth() <= $max_width && $this->getSourceHeight() <= $max_height && $allow_enlarge === false){
return $this;
}

$ratio = $this->getSourceHeight() / $this->getSourceWidth();
$width = $max_width;
$height = $width * $ratio;

if($height > $max_height){
$height = $max_height;
$width = $height / $ratio;
}

return $this->resize($width, $height, $allow_enlarge);
}

/**
* Resizes image according to given scale (proportionally)
*
* @param type $scale
* @param integer|float $scale
* @return \Eventviva\ImageResize
*/
public function scale($scale)
Expand Down
23 changes: 22 additions & 1 deletion test/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,28 @@ public function testResizeToWidth()
$this->assertEquals(50, $resize->getDestHeight());
}

public function testResizeToBestFit()
{
$image = $this->createImage(200, 500, 'png');
$resize = new ImageResize($image);

$resize->resizeToBestFit(100, 100);

$this->assertEquals(40, $resize->getDestWidth());
$this->assertEquals(100, $resize->getDestHeight());
}

public function testResizeToBestFitNoEnlarge()
{
$image = $this->createImage(200, 100, 'png');
$resize = new ImageResize($image);

$resize->resizeToBestFit(250, 250);

$this->assertEquals(200, $resize->getDestWidth());
$this->assertEquals(100, $resize->getDestHeight());
}

public function testScale()
{
$image = $this->createImage(200, 100, 'png');
Expand Down Expand Up @@ -172,7 +194,6 @@ public function testResizeLargerNotAllowed()
$this->assertEquals(100, $resize->getDestHeight());
}


/**
* Crop tests
*/
Expand Down