1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the kaloa/image package. |
5
|
|
|
* |
6
|
|
|
* For full copyright and license information, please view the LICENSE file |
7
|
|
|
* that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Kaloa\Image\Filter; |
11
|
|
|
|
12
|
|
|
use Kaloa\Image\Image; |
13
|
|
|
|
14
|
|
|
final class ResizeFilter extends AbstractFilter |
15
|
|
|
{ |
16
|
|
|
private $newWidth; |
17
|
|
|
private $newHeight; |
18
|
|
|
private $enlargeSmallImages; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
1 |
|
public function __construct($newWidth, $newHeight) |
24
|
|
|
{ |
25
|
1 |
|
$this->newWidth = $newWidth; |
26
|
1 |
|
$this->newHeight = $newHeight; |
27
|
|
|
|
28
|
1 |
|
$this->enlargeSmallImages = false; |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
* @param resource $image |
34
|
|
|
* @return resource GD image resource |
35
|
|
|
*/ |
36
|
1 |
|
private function resize($image) |
37
|
|
|
{ |
38
|
1 |
|
$maxWidth = $this->newWidth; |
39
|
1 |
|
$maxHeight = $this->newHeight; |
40
|
|
|
|
41
|
1 |
|
$width = imagesx($image); |
42
|
1 |
|
$height = imagesy($image); |
43
|
|
|
|
44
|
|
|
// If both dimensions of the image are below specified maxima and no |
45
|
|
|
// enlargement of small images is wanted, there is nothing to do |
46
|
1 |
|
if ($this->enlargeSmallImages === false |
47
|
1 |
|
&& $width < $maxWidth && $height < $maxHeight |
48
|
1 |
|
) { |
49
|
|
|
return $image; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
$ratioX = $width / $maxWidth; |
53
|
1 |
|
$ratioY = $height / $maxHeight; |
54
|
|
|
|
55
|
1 |
|
$newWidth = 0; |
|
|
|
|
56
|
1 |
|
$newHeight = 0; |
|
|
|
|
57
|
|
|
|
58
|
1 |
|
if ($ratioX > $ratioY) { |
59
|
1 |
|
$newWidth = $maxWidth; |
60
|
1 |
|
$newHeight = (int) round(imagesy($image) / $ratioX); |
61
|
1 |
|
if ($newHeight > $maxHeight) { |
62
|
|
|
$newHeight = $maxHeight; |
63
|
|
|
} |
64
|
1 |
|
} else { |
65
|
|
|
$newHeight = $maxHeight; |
66
|
|
|
$newWidth = (int) round(imagesx($image) / $ratioY); |
67
|
|
|
if ($newWidth > $maxWidth) { |
68
|
|
|
$newWidth = $maxWidth; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
$newImage = imagecreatetruecolor($newWidth, $newHeight); |
73
|
|
|
|
74
|
1 |
|
imagecopyresampled($newImage, $image, 0, 0, 0, 0, |
75
|
1 |
|
$newWidth, $newHeight, $width, $height); |
76
|
|
|
|
77
|
1 |
|
imagedestroy($image); |
78
|
|
|
|
79
|
1 |
|
return $newImage; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function render(Image $srcImage) |
83
|
|
|
{ |
84
|
1 |
|
return $this->resize($srcImage->getResource()); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.