depage-graphics
Loading...
Searching...
No Matches
Gd.php
Go to the documentation of this file.
1<?php
2
10
12
20{
24 protected $image;
25
37 protected function crop($width, $height, $x = 0, $y = 0)
38 {
39 if (!$this->bypassTest($width, $height, $x, $y)) {
40 $newImage = $this->createCanvas($width, $height);
41
42 imagecopy(
43 $newImage,
44 $this->image,
45 ($x > 0) ? 0 : abs($x),
46 ($y > 0) ? 0 : abs($y),
47 ($x < 0) ? 0 : $x,
48 ($y < 0) ? 0 : $y,
49 $this->size[0] - abs($x),
50 $this->size[1] - abs($y),
51 );
52
53 $this->image = $newImage;
54 $this->size = [$width, $height];
55 }
56 }
57
66 protected function resize($width, $height)
67 {
68 $newSize = $this->dimensions($width, $height);
69
70 if (!$this->bypassTest($newSize[0], $newSize[1])) {
71 $newImage = $this->createCanvas($newSize[0], $newSize[1]);
72 imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
73
74 $this->image = $newImage;
75 $this->size = $newSize;
76 }
77 }
78
87 protected function thumb($width, $height)
88 {
89 list($width, $height) = $this->dimensions($width, $height);
90
91 if (!$this->bypassTest($width, $height)) {
92 $newSize = $this->dimensions($width, null);
93
94 if ($newSize[1] > $height) {
95 $newSize = $this->dimensions(null, $height);
96 $xOffset = round(($width - $newSize[0]) / 2);
97 $yOffset = 0;
98 } else {
99 $xOffset = 0;
100 $yOffset = round(($height - $newSize[1]) / 2);
101 }
102
103 $newImage = $this->createCanvas($width, $height);
104
105 imagecopyresampled($newImage, $this->image, $xOffset, $yOffset, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
106
107 $this->image = $newImage;
108 $this->size = [$width, $height];
109 }
110 }
111
122 protected function thumbfill($width, $height, $centerX = 50, $centerY = 50)
123 {
124 list($width, $height) = $this->dimensions($width, $height);
125
126 if (!$this->bypassTest($width, $height, $centerX - 50, $centerY - 50)) {
127 $newSize = $this->dimensions($width, null);
128 $centerX /= 100;
129 $centerY /= 100;
130
131 if ($newSize[1] < $height) {
132 $newSize = $this->dimensions(null, $height);
133 $xOffset = round(($width - $newSize[0]) * $centerX);
134 $yOffset = 0;
135 } else {
136 $xOffset = 0;
137 $yOffset = round(($height - $newSize[1]) * $centerY);
138 }
139
140 $newImage = $this->createCanvas($width, $height);
141
142 imagecopyresampled($newImage, $this->image, $xOffset, $yOffset, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
143
144 $this->image = $newImage;
145 $this->size = [$width, $height];
146 }
147 }
148
156 protected function load()
157 {
158 if ($this->inputFormat == 'gif' && function_exists('imagecreatefromgif')) {
159 //GIF
160 $this->image = imagecreatefromgif($this->input);
161 } elseif ($this->inputFormat == 'jpg') {
162 //JPEG
163 $this->image = imagecreatefromjpeg($this->input);
164 } elseif ($this->inputFormat == 'png') {
165 //PNG
166 $this->image = imagecreatefrompng($this->input);
167 } elseif ($this->inputFormat == 'webp' && function_exists('imagecreatefromwebp')) {
168 //WEBP
169 $this->image = imagecreatefromwebp($this->input);
170 } else {
171 throw new \Depage\Graphics\Exceptions\Exception('Unknown image format.');
172 }
173 }
174
181 protected function save()
182 {
183 $this->autoOrient();
184
185 $bg = $this->createBackground($this->size[0], $this->size[1]);
186 imagecopy($bg, $this->image, 0, 0, 0, 0, $this->size[0], $this->size[1]);
187 $this->image = $bg;
188
189 $result = false;
190
191 if ($this->outputFormat == 'gif' && function_exists('imagegif')) {
192 $result = imagegif($this->image, $this->output);
193 } elseif ($this->outputFormat == 'jpg') {
194 $result = imagejpeg($this->image, $this->output, $this->getQuality());
195 } elseif ($this->outputFormat == 'png') {
196 $quality = (int) ($this->getQuality() / 10);
197 $result = imagepng($this->image, $this->output, $quality, PNG_ALL_FILTERS);
198 } elseif ($this->outputFormat == 'webp' && function_exists('imagewebp')) {
199 $result = imagewebp($this->image, $this->output, $this->getQuality());
200 }
201 if (!$result) {
202 throw new \Depage\Graphics\Exceptions\Exception('Could not save output image.');
203 }
204 }
205
211 protected function autoOrient(): void
212 {
213 $exif = @exif_read_data($this->input);
214
215 if (isset($exif['Orientation'])) {
216 switch ($exif['Orientation']) {
217 case 2:
218 imageflip($this->image, IMG_FLIP_HORIZONTAL);
219 break;
220 case 3:
221 $this->image = imagerotate($this->image, 180, 0);
222 break;
223 case 4:
224 imageflip($this->image, IMG_FLIP_VERTICAL);
225 break;
226 case 5:
227 $this->image = imagerotate($this->image, -90, 0);
228 imageflip($this->image, IMG_FLIP_HORIZONTAL);
229 break;
230 case 6:
231 $this->image = imagerotate($this->image, -90, 0);
232 break;
233 case 7:
234 $this->image = imagerotate($this->image, -90, 0);
235 imageflip($this->image, IMG_FLIP_VERTICAL);
236 break;
237 case 8:
238 $this->image = imagerotate($this->image, 90, 0);
239 break;
240 }
241 }
242 }
243
249 protected function getImageSize(): array
250 {
251 return getimagesize($this->input);
252 }
253
263 public function render($input, $output = null): void
264 {
265 parent::render($input, $output);
266
267 $this->load();
268 $this->processQueue();
269
270 if ($this->otherRender && file_exists($this->output)) {
271 // do nothing file is already generated
272 } elseif ($this->bypass
273 && $this->inputFormat == $this->outputFormat
274 ) {
275 $this->bypass();
276 } else {
277 $this->save();
278
279 if ($this->optimize) {
280 $this->optimizeImage($this->output);
281 }
282 }
283
284 parent::renderFinished();
285 }
286
294 private function createCanvas($width, $height)
295 {
296 $canvas = imagecreatetruecolor($width, $height);
297 $bg = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
298 imagefill($canvas, 0, 0, $bg);
299
300 return $canvas;
301 }
311 private function createBackground($width, $height)
312 {
313 $newImage = imagecreatetruecolor($width, $height);
314
315 if ($this->background[0] == '#') {
319 $color = substr($this->background, 1);
320
321 if (strlen($color) == 6) {
322 list($r, $g, $b) = [
323 $color[0] . $color[1],
324 $color[2] . $color[3],
325 $color[4] . $color[5],
326 ];
327 } elseif (strlen($color) == 3) {
328 list($r, $g, $b) = [$color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]];
329 }
330
331 $r = hexdec($r);
332 $g = hexdec($g);
333 $b = hexdec($b);
334
335 imagefill($newImage, 0, 0, imagecolorallocate($newImage, $r, $g, $b));
336 } elseif ($this->background == 'checkerboard') {
337 $transLen = 15;
338 $transColor = [];
339 $transColor[0] = imagecolorallocate($newImage, 153, 153, 153);
340 $transColor[1] = imagecolorallocate($newImage, 102, 102, 102);
341 for ($i = 0; $i * $transLen < $width; $i++) {
342 for ($j = 0; $j * $transLen < $height; $j++) {
343 imagefilledrectangle(
344 $newImage,
345 $i * $transLen,
346 $j * $transLen,
347 ($i + 1) * $transLen,
348 ($j + 1) * $transLen,
349 $transColor[$j % 2 == 0 ? $i % 2 : ($i % 2 == 0 ? 1 : 0)],
350 );
351 }
352 }
353 } elseif ($this->background == 'transparent') {
354 imagefill($newImage, 0, 0, imagecolorallocatealpha($newImage, 255, 255, 255, 127));
355 if ($this->outputFormat == 'gif') {
356 imagecolortransparent($newImage, imagecolorallocatealpha($newImage, 255, 255, 255, 127));
357 }
358 imagesavealpha($newImage, true);
359 }
360
361 return $newImage;
362 }
363}
364
365/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
Main graphics class.
Definition Graphics.php:42
$quality
Image quality string.
Definition Graphics.php:78
processQueue()
Process action queue.
Definition Graphics.php:329
bypassTest($width, $height, $x=0, $y=0)
Tests if action would change current image.
Definition Graphics.php:558
getQuality()
Returns quality-index for current image format.
Definition Graphics.php:495
$input
Input filename.
Definition Graphics.php:46
$output
Output filename.
Definition Graphics.php:50
dimensions($width, $height)
Scales image dimensions.
Definition Graphics.php:301
optimizeImage($filename)
Opimizes final image through one of the optimization programs.
Definition Graphics.php:382
bypass()
Runs bypass (copies file)
Definition Graphics.php:586
PHP GD extension interface.
Definition Gd.php:20
thumbfill($width, $height, $centerX=50, $centerY=50)
Thumb-Fill action.
Definition Gd.php:122
load()
Loads image from file.
Definition Gd.php:156
autoOrient()
Auto orient image.
Definition Gd.php:211
getImageSize()
Determine size of input image.
Definition Gd.php:249
render($input, $output=null)
Main method for image handling.
Definition Gd.php:263
resize($width, $height)
Resize action.
Definition Gd.php:66
$image
Image resource identifier.
Definition Gd.php:24
crop($width, $height, $x=0, $y=0)
Crop action.
Definition Gd.php:37
thumb($width, $height)
Thumb action.
Definition Gd.php:87
save()
Saves image to file.
Definition Gd.php:181