|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Bluz PHP Team |
|
4
|
|
|
* @link https://github.com/bluzphp/skeleton |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @namespace |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Image; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Wrapper over Gd for support some Imagick functions |
|
15
|
|
|
* |
|
16
|
|
|
* @category Application |
|
17
|
|
|
* @package Library |
|
18
|
|
|
*/ |
|
19
|
|
|
class Gd |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $file; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var resource |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $image; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var integer |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $width; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var integer |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $height; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Compression quality for JPEG |
|
43
|
|
|
* |
|
44
|
|
|
* @var integer |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $quality = 86; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Constructor of Gd |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $file |
|
52
|
|
|
* |
|
53
|
|
|
* @throws Exception |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct($file) |
|
56
|
|
|
{ |
|
57
|
|
|
if (!file_exists($file)) { |
|
58
|
|
|
throw new Exception("Image `$file` file not found"); |
|
59
|
|
|
} |
|
60
|
|
|
$this->file = $file; |
|
61
|
|
|
|
|
62
|
|
|
// Retrieve image information |
|
63
|
|
|
[$this->width, $this->height, $type] = getimagesize($file); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
// Check support of file type |
|
66
|
|
|
if (!(imagetypes() & $type)) { |
|
67
|
|
|
throw new Exception("Server does not support `$type` image type"); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// Using imagecreatefromstring will automatically detect the file type |
|
71
|
|
|
$this->image = imagecreatefromstring(file_get_contents($file)); |
|
72
|
|
|
|
|
73
|
|
|
if (!$this->image) { |
|
74
|
|
|
throw new Exception('Could not load image'); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param integer $quality |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
public function setImageCompressionQuality($quality): void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->quality = $quality; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Create a crop thumbnail image from source image |
|
90
|
|
|
* cropped by smaller side |
|
91
|
|
|
* |
|
92
|
|
|
* @param integer $width |
|
93
|
|
|
* @param integer $height |
|
94
|
|
|
* |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
|
|
public function cropThumbnailImage($width, $height): bool |
|
98
|
|
|
{ |
|
99
|
|
|
// Compare image size with required thumbnail size |
|
100
|
|
|
if (($this->width < $width) && |
|
101
|
|
|
($this->height < $height) |
|
102
|
|
|
) { |
|
103
|
|
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$widthScale = round($this->width / $width); |
|
107
|
|
|
$heightScale = round($this->height / $height); |
|
108
|
|
|
|
|
109
|
|
|
if ($heightScale < $widthScale) { |
|
110
|
|
|
// Crop width |
|
111
|
|
|
$cropWidth = $heightScale * $width; |
|
112
|
|
|
$cropHeight = $this->height; |
|
113
|
|
|
$srcX = round(($this->width - $cropWidth) / 2); |
|
114
|
|
|
$srcY = 0; |
|
115
|
|
|
} else { |
|
116
|
|
|
// Crop height |
|
117
|
|
|
$cropWidth = $this->width; |
|
118
|
|
|
$cropHeight = $widthScale * $height; |
|
119
|
|
|
$srcX = 0; |
|
120
|
|
|
$srcY = round(($this->height - $cropHeight) / 2); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$thumb = imagecreatetruecolor($width, $height); |
|
124
|
|
|
|
|
125
|
|
|
// Copy resampled makes a smooth thumbnail |
|
126
|
|
|
imagecopyresampled($thumb, $this->image, 0, 0, $srcX, $srcY, $width, $height, $cropWidth, $cropHeight); |
|
127
|
|
|
imagedestroy($this->image); |
|
128
|
|
|
|
|
129
|
|
|
$this->width = $width; |
|
130
|
|
|
$this->height = $height; |
|
131
|
|
|
$this->image = $thumb; |
|
132
|
|
|
|
|
133
|
|
|
return $thumb ? true : false; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Save the image to a file. Type is determined from the extension |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $fileName |
|
140
|
|
|
* |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
|
|
public function writeImage($fileName): bool |
|
144
|
|
|
{ |
|
145
|
|
|
if (!$this->image || file_exists($fileName)) { |
|
146
|
|
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$ext = strtolower(substr($fileName, strrpos($fileName, '.'))); |
|
150
|
|
|
|
|
151
|
|
|
switch ($ext) { |
|
152
|
|
|
case '.gif': |
|
153
|
|
|
return imagegif($this->image, $fileName); |
|
154
|
|
|
// break |
|
155
|
|
|
case '.jpg': |
|
156
|
|
|
case '.jpeg': |
|
157
|
|
|
return imagejpeg($this->image, $fileName, $this->quality); |
|
158
|
|
|
// break |
|
159
|
|
|
case '.png': |
|
160
|
|
|
return imagepng($this->image, $fileName); |
|
161
|
|
|
// break |
|
162
|
|
|
case '.bmp': |
|
163
|
|
|
return imagewbmp($this->image, $fileName); |
|
164
|
|
|
// break |
|
165
|
|
|
default: |
|
166
|
|
|
return false; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Destroy image source |
|
172
|
|
|
*/ |
|
173
|
|
|
public function __destruct() |
|
174
|
|
|
{ |
|
175
|
|
|
imagedestroy($this->image); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.