Skip to content

Commit 118202e

Browse files
Merge pull request #82 from erigoni/erigoni-patch-1
Add functions for resize regardingless the image orientation
2 parents d4c41f4 + 79a14f4 commit 118202e

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ $image->resizeToWidth(300);
6666
$image->save('image2.jpg');
6767
```
6868

69+
To resize an image according to a given measure regardingless its orientation (keeping aspect ratio):
70+
71+
```php
72+
$image = new ImageResize('image.jpg');
73+
$image->resizeToLongSide(500);
74+
$image->save('image2.jpg');
75+
76+
$image = new ImageResize('image.jpg');
77+
$image->resizeToShortSide(300);
78+
$image->save('image2.jpg');
79+
```
80+
6981
To resize an image to best fit a given set of dimensions (keeping aspet ratio):
7082
```php
7183
$image = new ImageResize('image.jpg');
@@ -253,3 +265,4 @@ API Doc
253265
-------
254266

255267
https://eventviva.github.io/php-image-resize/class-Eventviva.ImageResize.html
268+
>>>>>>> f84e175149be8a6058e196408d59a1c05b509345

lib/ImageResize.php

+48
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,54 @@ public function output($image_type = null, $quality = null)
270270
$this->save(null, $image_type, $quality);
271271
}
272272

273+
/**
274+
* Resizes image according to the given short side (short side proportional)
275+
*
276+
* @param integer $max_short
277+
* @param boolean $allow_enlarge
278+
* @return \static
279+
*/
280+
public function resizeToShortSide($max_short, $allow_enlarge = false)
281+
{
282+
if ($this->getSourceHeight() < $this->getSourceWidth()) {
283+
$ratio = $max_short / $this->getSourceHeight();
284+
$long = $this->getSourceWidth() * $ratio;
285+
286+
$this->resize($long, $max_short, $allow_enlarge);
287+
} else {
288+
$ratio = $max_short / $this->getSourceWidth();
289+
$long = $this->getSourceHeight() * $ratio;
290+
291+
$this->resize($max_short, $long, $allow_enlarge);
292+
}
293+
294+
return $this;
295+
}
296+
297+
/**
298+
* Resizes image according to the given long side (short side proportional)
299+
*
300+
* @param integer $max_long
301+
* @param boolean $allow_enlarge
302+
* @return \static
303+
*/
304+
public function resizeToLongSide($max_long, $allow_enlarge = false)
305+
{
306+
if ($this->getSourceHeight() > $this->getSourceWidth()) {
307+
$ratio = $max_long / $this->getSourceHeight();
308+
$short = $this->getSourceWidth() * $ratio;
309+
310+
$this->resize($short, $max_long, $allow_enlarge);
311+
} else {
312+
$ratio = $max_long / $this->getSourceWidth();
313+
$short = $this->getSourceHeight() * $ratio;
314+
315+
$this->resize($max_long, $short, $allow_enlarge);
316+
}
317+
318+
return $this;
319+
}
320+
273321
/**
274322
* Resizes image according to the given height (width proportional)
275323
*

test/Test.php

+44
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,50 @@ public function testInvalidString()
121121
* Resize tests
122122
*/
123123

124+
public function testResizeToLongSide()
125+
{
126+
$image = $this->createImage(200, 100, 'png');
127+
$resize = new ImageResize($image);
128+
129+
$resize->resizeToLongSide(100);
130+
131+
$this->assertEquals(100, $resize->getDestWidth());
132+
$this->assertEquals(50, $resize->getDestHeight());
133+
}
134+
135+
public function testResizeToLongSideVertical()
136+
{
137+
$image = $this->createImage(100, 200, 'png');
138+
$resize = new ImageResize($image);
139+
140+
$resize->resizeToLongSide(100);
141+
142+
$this->assertEquals(50, $resize->getDestWidth());
143+
$this->assertEquals(100, $resize->getDestHeight());
144+
}
145+
146+
public function testResizeToShortSide()
147+
{
148+
$image = $this->createImage(200, 100, 'png');
149+
$resize = new ImageResize($image);
150+
151+
$resize->resizeToShortSide(50);
152+
153+
$this->assertEquals(100, $resize->getDestWidth());
154+
$this->assertEquals(50, $resize->getDestHeight());
155+
}
156+
157+
public function testResizeToShortSideVertical()
158+
{
159+
$image = $this->createImage(100, 200, 'png');
160+
$resize = new ImageResize($image);
161+
162+
$resize->resizeToShortSide(50);
163+
164+
$this->assertEquals(50, $resize->getDestWidth());
165+
$this->assertEquals(100, $resize->getDestHeight());
166+
}
167+
124168
public function testResizeToHeight()
125169
{
126170
$image = $this->createImage(200, 100, 'png');

0 commit comments

Comments
 (0)