Skip to content

Commit 66c0d10

Browse files
committed
Refactored the code to use findAspectRatioValues
Again, removing code duplication This should fix #120 and fix #121
1 parent 35cbbaa commit 66c0d10

1 file changed

Lines changed: 27 additions & 42 deletions

File tree

filters/filter.resizeandcrop.php

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,55 +45,40 @@ public function parseParameters($parameter_string)
4545

4646
public function run(\Image $res, $settings)
4747
{
48-
$src_w = $res->Meta()->width;
49-
$src_h = $res->Meta()->height;
50-
51-
if ($settings['settings']['height'] == 0) {
52-
$ratio = ($src_h / $src_w);
53-
$dst_h = round($settings['meta']['width'] * $ratio);
54-
} elseif ($settings['settings']['width'] == 0) {
55-
$ratio = ($src_w / $src_h);
56-
$dst_w = round($settings['meta']['height'] * $ratio);
57-
}
58-
59-
$src_r = ($src_w / $src_h);
60-
$dst_r = ($settings['meta']['width'] / $settings['meta']['height']);
61-
62-
if ($src_r < $dst_r) {
63-
$width = $settings['meta']['width'];
64-
$height = null;
65-
} else {
66-
$width = null;
67-
$height = $settings['meta']['height'];
68-
}
69-
7048
$resource = $res->Resource();
49+
$src_w = Image::width($resource);
50+
$src_h = Image::height($resource);
51+
52+
$width = $settings['meta']['width'];
53+
$height = $settings['meta']['height'];
54+
55+
// We must always preserve aspect ratio.
56+
// Resize accordingly
57+
if ($width == 0 || $height == 0) {
58+
list($dst_w, $dst_h) = static::findAspectRatioValues($width, $height, $src_w, $src_h);
59+
// Overrides parameters, since one is null
60+
// no cropping will occur
61+
$width = $dst_w;
62+
$height = $dst_h;
63+
} else {
64+
$src_r = ($src_w / $src_h);
65+
$dst_r = ($width / $height);
7166

72-
$dst_w = Image::width($resource);
73-
$dst_h = Image::height($resource);
74-
75-
if (!empty($width) && !empty($height)) {
76-
$dst_w = $width;
77-
$dst_h = $height;
78-
} elseif (empty($height)) {
79-
$ratio = ($dst_h / $dst_w);
80-
$dst_w = $width;
81-
$dst_h = round($dst_w * $ratio);
82-
} elseif (empty($width)) {
83-
$ratio = ($dst_w / $dst_h);
84-
$dst_h = $height;
85-
$dst_w = round($dst_h * $ratio);
67+
if ($src_r < $dst_r) {
68+
list($dst_w, $dst_h) = static::findAspectRatioValues($width, null, $src_w, $src_h);
69+
} else {
70+
list($dst_w, $dst_h) = static::findAspectRatioValues(null, $height, $src_w, $src_h);
71+
}
8672
}
8773

88-
$image_width = Image::width($resource);
89-
$image_height = Image::height($resource);
90-
91-
$tmp = imagecreatetruecolor($dst_w, $dst_h);
74+
$tmp = imagecreatetruecolor($width, $height);
9275
static::__fill($resource, $tmp, $settings['settings']['background']);
9376

94-
list($src_x, $src_y, $dst_x, $dst_y) = static::__calculateDestSrcXY($dst_w, $dst_h, $src_w, $src_h, $width, $height, $settings['settings']['position']);
77+
// Find crop according to resize (dst) size
78+
list($src_x, $src_y, $dst_x, $dst_y) = static::__calculateDestSrcXY($width, $height, $dst_w, $dst_h, $dst_w, $dst_h, $settings['settings']['position']);
9579

96-
imagecopyresampled($tmp, $resource, $src_x, $src_y, $dst_x, $dst_y, $image_width, $image_height, $image_width, $image_height);
80+
// Copy image
81+
imagecopyresampled($tmp, $resource, $src_x, $src_y, $dst_x, $dst_y, $dst_w, $dst_h, $src_w, $src_h);
9782

9883
if (is_resource($resource)) {
9984
imagedestroy($resource);

0 commit comments

Comments
 (0)