Skip to content

Commit 19a557b

Browse files
committed
Merge pull request #26 from Sjaakmoes/master
Added method resizeToBestFit
2 parents c75bc2c + 063abf8 commit 19a557b

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ $image->resizeToWidth(300);
6464
$image->save('image2.jpg');
6565
```
6666

67+
To resize an image to best fit a given set of dimensions (keeping aspet ratio):
68+
```php
69+
$image = new ImageResize('image.jpg');
70+
$image->resizeToBestFit(500, 300);
71+
$image->save('image2.jpg');
72+
```
73+
6774
To to crop an image:
6875

6976
```php

src/ImageResize.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ImageResize
4343
/**
4444
* Create instance from a strng
4545
*
46-
* @param string $imageData
46+
* @param string $image_data
4747
* @return ImageResize
4848
* @throws \exception
4949
*/
@@ -251,10 +251,36 @@ public function resizeToWidth($width, $allow_enlarge = false)
251251
return $this;
252252
}
253253

254+
/**
255+
* Resizes image to best fit inside the given dimensions
256+
*
257+
* @param integer $max_width
258+
* @param integer $max_height
259+
* @param boolean $allow_enlarge
260+
* @return \static
261+
*/
262+
public function resizeToBestFit($max_width, $max_height, $allow_enlarge = false)
263+
{
264+
if($this->getSourceWidth() <= $max_width && $this->getSourceHeight() <= $max_height && $allow_enlarge === false){
265+
return $this;
266+
}
267+
268+
$ratio = $this->getSourceHeight() / $this->getSourceWidth();
269+
$width = $max_width;
270+
$height = $width * $ratio;
271+
272+
if($height > $max_height){
273+
$height = $max_height;
274+
$width = $height / $ratio;
275+
}
276+
277+
return $this->resize($width, $height, $allow_enlarge);
278+
}
279+
254280
/**
255281
* Resizes image according to given scale (proportionally)
256282
*
257-
* @param type $scale
283+
* @param integer|float $scale
258284
* @return \Eventviva\ImageResize
259285
*/
260286
public function scale($scale)

test/Test.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,28 @@ public function testResizeToWidth()
139139
$this->assertEquals(50, $resize->getDestHeight());
140140
}
141141

142+
public function testResizeToBestFit()
143+
{
144+
$image = $this->createImage(200, 500, 'png');
145+
$resize = new ImageResize($image);
146+
147+
$resize->resizeToBestFit(100, 100);
148+
149+
$this->assertEquals(40, $resize->getDestWidth());
150+
$this->assertEquals(100, $resize->getDestHeight());
151+
}
152+
153+
public function testResizeToBestFitNoEnlarge()
154+
{
155+
$image = $this->createImage(200, 100, 'png');
156+
$resize = new ImageResize($image);
157+
158+
$resize->resizeToBestFit(250, 250);
159+
160+
$this->assertEquals(200, $resize->getDestWidth());
161+
$this->assertEquals(100, $resize->getDestHeight());
162+
}
163+
142164
public function testScale()
143165
{
144166
$image = $this->createImage(200, 100, 'png');
@@ -172,7 +194,6 @@ public function testResizeLargerNotAllowed()
172194
$this->assertEquals(100, $resize->getDestHeight());
173195
}
174196

175-
176197
/**
177198
* Crop tests
178199
*/

0 commit comments

Comments
 (0)