Skip to content

Commit 58bfac1

Browse files
committed
If you want resize your image to a given measure (long side or short side) regardingless the image orientation. (resizeToLongSide and resizeToShortSide)
1 parent 96fc9d2 commit 58bfac1

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

lib/ImageResize.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,14 @@ public function resizeToShortSide($max_short, $allow_enlarge = false)
281281
{
282282
if ($this->getSourceHeight() < $this->getSourceWidth()) {
283283
$ratio = $max_short / $this->getSourceHeight();
284-
$width = $this->getSourceWidth() * $ratio;
284+
$long = $this->getSourceWidth() * $ratio;
285285

286-
$this->resize($width, $max_short, $allow_enlarge);
286+
$this->resize($long, $max_short, $allow_enlarge);
287287
} else {
288288
$ratio = $max_short / $this->getSourceWidth();
289-
$width = $this->getSourceHeight() * $ratio;
289+
$long = $this->getSourceHeight() * $ratio;
290290

291-
$this->resize($width, $max_short, $allow_enlarge);
291+
$this->resize($max_short, $long, $allow_enlarge);
292292
}
293293

294294
return $this;
@@ -305,14 +305,14 @@ public function resizeToLongSide($max_long, $allow_enlarge = false)
305305
{
306306
if ($this->getSourceHeight() > $this->getSourceWidth()) {
307307
$ratio = $max_long / $this->getSourceHeight();
308-
$width = $this->getSourceWidth() * $ratio;
308+
$short = $this->getSourceWidth() * $ratio;
309309

310-
$this->resize($width, $max_long, $allow_enlarge);
310+
$this->resize($short, $max_long, $allow_enlarge);
311311
} else {
312312
$ratio = $max_long / $this->getSourceWidth();
313-
$width = $this->getSourceHeight() * $ratio;
313+
$short = $this->getSourceHeight() * $ratio;
314314

315-
$this->resize($width, $max_long, $allow_enlarge);
315+
$this->resize($max_long, $short, $allow_enlarge);
316316
}
317317

318318
return $this;

test/Test.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,17 @@ public function testResizeToLongSide()
132132
$this->assertEquals(50, $resize->getDestHeight());
133133
}
134134

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+
135146
public function testResizeToShortSide()
136147
{
137148
$image = $this->createImage(200, 100, 'png');
@@ -143,6 +154,17 @@ public function testResizeToShortSide()
143154
$this->assertEquals(50, $resize->getDestHeight());
144155
}
145156

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+
146168
public function testResizeToHeight()
147169
{
148170
$image = $this->createImage(200, 100, 'png');

0 commit comments

Comments
 (0)