Debian Codegraph @

lib/imageutils.inc

raw
1 <?php
2
3 namespace LAM\ImageUtils;
4
5 use GdImage;
6 use Imagick;
7 use LAMException;
8
9 /*
10
11 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
12 Copyright (C) 2018 - 2026 Roland Gruber
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */
28
29 /**
30 * Image manipulation functions.
31 *
32 * @author Roland Gruber
33 */
34
35 /**
36 * Factory to create modificators for images.
37 *
38 * @author Roland Gruber
39 */
40 class ImageManipulationFactory {
41
42 /**
43 * Returns an image manipulator based on installed PHP modules.
44 *
45 * @param string $imageData binary string of image data
46 * @return ImageManipulator manipulator
47 */
48 public static function getImageManipulator($imageData) {
49 if (extension_loaded('imagick')) {
50 return new ImageManipulatorImagick($imageData);
51 }
52 return new ImageManipulatorGd($imageData);
53 }
54
55 /**
56 * Returns an image manipulator based on installed PHP modules.
57 *
58 * @param string $path path to image file
59 * @return ImageManipulator manipulator
60 */
61 public static function getImageManipulatorFromFile($path) {
62 $handle = fopen($path, "r");
63 $data = fread($handle, 100000000);
64 fclose($handle);
65 return ImageManipulationFactory::getImageManipulator($data);
66 }
67
68 }
69
70 /**
71 * Modifies images.
72 *
73 * @author Roland Gruber
74 */
75 interface ImageManipulator {
76
77 /**
78 * Returns the height of the image.
79 *
80 * @return int height
81 */
82 public function getHeight(): int;
83
84 /**
85 * Returns the width of the image.
86 *
87 * @return int width
88 */
89 public function getWidth(): int;
90
91 /**
92 * Resizes the image to the given maximum dimensions.
93 *
94 * @param int $width width
95 * @param int $height height
96 */
97 public function thumbnail(int $width, int $height): void;
98
99 /**
100 * Converts the image to JPEG format.
101 */
102 public function convertToJpeg(): void;
103
104 /**
105 * Crops the image.
106 *
107 * @param int $x starting point in original image
108 * @param int $y starting point in original image
109 * @param int $width width of target size
110 * @param int $height height of target size
111 */
112 public function crop(int $x, int $y, int $width, int $height): void;
113
114 /**
115 * Returns the image as binary string.
116 *
117 * @return string image data
118 */
119 public function getImageData(): string;
120
121 }
122
123 /**
124 * Manipulates images using Imagick library.
125 *
126 * @author Roland Gruber
127 */
128 class ImageManipulatorImagick implements ImageManipulator {
129
130 /**
131 * Image
132 *
133 * @var Imagick image
134 */
135 private $image;
136
137 /**
138 * Constructor.
139 *
140 * @param string $imageData original image as binary string
141 */
142 public function __construct($imageData) {
143 $this->image = new Imagick();
144 $this->image->readimageblob($imageData);
145 }
146
147 /**
148 * {@inheritDoc}
149 * @see \LAM\ImageUtils\ImageManipulator::getHeight()
150 */
151 public function getHeight(): int {
152 return $this->image->getimageheight();
153 }
154
155 /**
156 * {@inheritDoc}
157 * @see \LAM\ImageUtils\ImageManipulator::getWidth()
158 */
159 public function getWidth(): int {
160 return $this->image->getimagewidth();
161 }
162
163 /**
164 * {@inheritDoc}
165 * @see \LAM\ImageUtils\ImageManipulator::getAsJpeg()
166 */
167 public function convertToJpeg(): void {
168 $this->image->setImageCompression(Imagick::COMPRESSION_JPEG);
169 $this->image->setImageFormat('jpeg');
170 }
171
172 /**
173 * {@inheritDoc}
174 * @see \LAM\ImageUtils\ImageManipulator::crop()
175 */
176 public function crop(int $x, int $y, int $width, int $height): void {
177 $this->image->cropimage((int) round($width), (int) round($height), (int) round($x), (int) round($y));
178 }
179
180 /**
181 * {@inheritDoc}
182 * @see \LAM\ImageUtils\ImageManipulator::resize()
183 */
184 public function thumbnail(int $width, int $height): void {
185 if (($this->getWidth() <= $width) && ($this->getHeight() <= $height)) {
186 // skip if smaller than target size
187 return;
188 }
189 $this->image->thumbnailimage($width, $height, true);
190 }
191
192 /**
193 * {@inheritDoc}
194 * @see \LAM\ImageUtils\ImageManipulator::getImageData()
195 */
196 public function getImageData(): string {
197 return $this->image->getimageblob();
198 }
199
200 }
201
202 /**
203 * Manipulates images using gd library.
204 *
205 * @author Roland Gruber
206 */
207 class ImageManipulatorGd implements ImageManipulator {
208
209 /**
210 * Image
211 *
212 * @var GdImage|false image
213 */
214 private $image;
215
216 /**
217 * GD image type
218 *
219 * @var int image type
220 */
221 private $type;
222
223 /**
224 * Constructor.
225 *
226 * @param string $imageData original image as binary string
227 * @throws LAMException error reading image
228 */
229 public function __construct($imageData) {
230 $this->image = @imagecreatefromstring($imageData);
231 if ($this->image === false) {
232 throw new LAMException(_('Photo'), _('Unable to read image'));
233 }
234 $info = getimagesizefromstring($imageData);
235 $this->type = $info[2];
236 }
237
238 /**
239 * {@inheritDoc}
240 * @see \LAM\ImageUtils\ImageManipulator::getHeight()
241 */
242 public function getHeight(): int {
243 return imagesy($this->image);
244 }
245
246 /**
247 * {@inheritDoc}
248 * @see \LAM\ImageUtils\ImageManipulator::getWidth()
249 */
250 public function getWidth(): int {
251 return imagesx($this->image);
252 }
253
254 /**
255 * {@inheritDoc}
256 * @see \LAM\ImageUtils\ImageManipulator::getAsJpeg()
257 */
258 public function convertToJpeg(): void {
259 $this->type = IMAGETYPE_JPEG;
260 }
261
262 /**
263 * {@inheritDoc}
264 * @see \LAM\ImageUtils\ImageManipulator::crop()
265 */
266 public function crop(int $x, int $y, int $width, int $height): void {
267 $this->image = imagecrop($this->image, [
268 'x' => $x,
269 'y' => $y,
270 'width' => $width,
271 'height' => $height
272 ]);
273 }
274
275 /**
276 * {@inheritDoc}
277 * @see \LAM\ImageUtils\ImageManipulator::resize()
278 */
279 public function thumbnail(int $width, int $height): void {
280 if (($this->getWidth() <= $width) && ($this->getHeight() <= $height)) {
281 // skip if smaller than target size
282 return;
283 }
284 $thumbWidth = $this->getWidth();
285 $thumbHeight = $this->getHeight();
286 if ($thumbWidth > $width) {
287 $factor = $width / $thumbWidth;
288 $thumbWidth *= $factor;
289 $thumbHeight *= $factor;
290 }
291 if ($thumbHeight > $height) {
292 $factor = $height / $thumbHeight;
293 $thumbWidth *= $factor;
294 $thumbHeight *= $factor;
295 }
296 $thumbnail = imagecreatetruecolor($thumbWidth, $thumbHeight);
297 imagecopyresampled(
298 $thumbnail,
299 $this->image,
300 0, 0, 0, 0,
301 $thumbWidth,
302 $thumbHeight,
303 $this->getWidth(),
304 $this->getHeight());
305 $this->image = $thumbnail;
306 }
307
308
309 /**
310 * {@inheritDoc}
311 * @see \LAM\ImageUtils\ImageManipulator::getImageData()
312 */
313 public function getImageData(): string {
314 ob_start();
315 if ($this->type == IMAGETYPE_JPEG) {
316 imagejpeg($this->image);
317 }
318 elseif ($this->type == IMAGETYPE_PNG) {
319 imagepng($this->image);
320 }
321 $output = ob_get_contents();
322 ob_clean();
323 return $output;
324 }
325
326 }