Skip to content

Commit 7700bc2

Browse files
committed
Refactored the code to use findAspectRatioValues
Removes code duplication yet again!
1 parent 2f1737c commit 7700bc2

1 file changed

Lines changed: 21 additions & 32 deletions

File tree

filters/filter.resizeandfit.php

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,31 @@ public static function about()
1414

1515
public function run(\Image $res, $settings)
1616
{
17-
$src_w = $res->Meta()->width;
18-
$src_h = $res->Meta()->height;
19-
20-
if ($settings['settings']['height'] == 0) {
21-
$ratio = ($src_h / $src_w);
22-
$dst_h = round($settings['meta']['width'] * $ratio);
23-
$dst_w = $settings['meta']['width'];
24-
} elseif ($settings['settings']['width'] == 0) {
25-
$ratio = ($src_w / $src_h);
26-
$dst_w = round($settings['meta']['height'] * $ratio);
27-
$dst_h = $settings['meta']['height'];
28-
} else {
29-
$dst_h = $settings['meta']['height'];
30-
$dst_w = $settings['meta']['width'];
31-
}
17+
$resource = $res->Resource();
18+
$src_w = Image::width($resource);
19+
$src_h = Image::height($resource);
3220

33-
$src_r = ($src_w / $src_h);
34-
$dst_r = ($settings['meta']['width'] / $settings['meta']['height']);
21+
$width = $settings['meta']['width'];
22+
$height = $settings['meta']['height'];
3523

36-
if ($src_h <= $dst_h && $src_w <= $dst_w) {
37-
$settings['settings']['width'] = $src_w;
38-
$settings['settings']['height'] = $src_h;
39-
40-
return parent::run($res, $settings);
41-
}
42-
43-
if ($src_h >= $dst_h && $src_r <= $dst_r) {
44-
$settings['settings']['height'] = $dst_h;
45-
$res = parent::run($res, $settings);
24+
// We must always preserve aspect ratio.
25+
// Resize accordingly
26+
if ($width == 0 || $height == 0) {
27+
list($dst_w, $dst_h) = static::findAspectRatioValues($width, $height, $src_w, $src_h);
28+
} else {
29+
$src_r = ($src_w / $src_h);
30+
$dst_r = ($width / $height);
31+
32+
if ($src_r > $dst_r) {
33+
list($dst_w, $dst_h) = static::findAspectRatioValues($width, null, $src_w, $src_h);
34+
} else {
35+
list($dst_w, $dst_h) = static::findAspectRatioValues(null, $height, $src_w, $src_h);
36+
}
4637
}
4738

48-
if ($src_w >= $dst_w && $src_r >= $dst_r) {
49-
$settings['settings']['width'] = $dst_w;
50-
$res = parent::run($res, $settings);
51-
}
39+
$settings['meta']['width'] = $dst_w;
40+
$settings['meta']['height'] = $dst_h;
5241

53-
return $res;
42+
return parent::run($res, $settings);
5443
}
5544
}

0 commit comments

Comments
 (0)