Skip to content

Commit 703e249

Browse files
author
Mitch
committed
Slightly refactor enlarging logic, and add enlarging option to the rest of the size manipulation methods
1 parent 0885fc0 commit 703e249

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/ImageResize.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,22 @@ public function output($image_type = null, $quality = null)
156156
$this->save(null, $image_type, $quality);
157157
}
158158

159-
public function resizeToHeight($height)
159+
public function resizeToHeight($height, $allow_enlarge = false)
160160
{
161161
$ratio = $height / $this->getSourceHeight();
162162
$width = $this->getSourceWidth() * $ratio;
163163

164-
$this->resize($width, $height);
164+
$this->resize($width, $height, $allow_enlarge);
165165

166166
return $this;
167167
}
168168

169-
public function resizeToWidth($width)
169+
public function resizeToWidth($width, $allow_enlarge = false)
170170
{
171171
$ratio = $width / $this->getSourceWidth();
172172
$height = $this->getSourceHeight() * $ratio;
173173

174-
$this->resize($width, $height);
174+
$this->resize($width, $height, $allow_enlarge);
175175

176176
return $this;
177177
}
@@ -189,6 +189,10 @@ public function scale($scale)
189189
public function resize($width, $height, $allow_enlarge = false)
190190
{
191191
if (!$allow_enlarge) {
192+
// if the user hasn't explicitly allowed enlarging,
193+
// but either of the dimensions are larger then the original,
194+
// then just use original dimensions - this logic may need rethinking
195+
192196
if ($width > $this->getSourceWidth() || $height > $this->getSourceHeight()) {
193197
$width = $this->getSourceWidth();
194198
$height = $this->getSourceHeight();
@@ -210,8 +214,15 @@ public function resize($width, $height, $allow_enlarge = false)
210214
public function crop($width, $height, $allow_enlarge = false)
211215
{
212216
if (!$allow_enlarge) {
213-
if ($width > $this->getSourceWidth() || $height > $this->getSourceHeight()) {
217+
// this logic is slightly different to resize(),
218+
// it will only reset dimensions to the original
219+
// if that particular dimenstion is larger
220+
221+
if ($width > $this->getSourceWidth()) {
214222
$width = $this->getSourceWidth();
223+
}
224+
225+
if ($height > $this->getSourceHeight()) {
215226
$height = $this->getSourceHeight();
216227
}
217228
}
@@ -220,7 +231,7 @@ public function crop($width, $height, $allow_enlarge = false)
220231
$ratio_dest = $width / $height;
221232

222233
if ($ratio_dest < $ratio_source) {
223-
$this->resizeToHeight($height);
234+
$this->resizeToHeight($height, $allow_enlarge);
224235

225236
$excess_width = ($this->getDestWidth() - $width) / $this->getDestWidth() * $this->getSourceWidth();
226237

@@ -229,7 +240,7 @@ public function crop($width, $height, $allow_enlarge = false)
229240

230241
$this->dest_w = $width;
231242
} else {
232-
$this->resizeToWidth($width);
243+
$this->resizeToWidth($width, $allow_enlarge);
233244

234245
$excess_height = ($this->getDestHeight() - $height) / $this->getDestHeight() * $this->getSourceHeight();
235246

0 commit comments

Comments
 (0)