depage-graphics
Loading...
Searching...
No Matches
Imagick.php
Go to the documentation of this file.
1<?php
2
12
13namespace Depage\Graphics\Providers;
14
20{
24 protected $image = null;
25
32 public function canRead($ext)
33 {
34 return parent::canRead($ext) || in_array($ext, ['tif', 'tiff', 'pdf', 'eps']);
35 }
36
43 protected function crop($width, $height, $x = 0, $y = 0)
44 {
45 if (!$this->bypassTest($width, $height, $x, $y)) {
46 $this->image->setImageGravity(\Imagick::GRAVITY_NORTHWEST);
47 $this->image->cropImage($width, $height, $x, $y);
48 $this->image->extentImage($width, $height, 0, 0);
49 $this->image->setImagePage(0, 0, 0, 0);
50 $this->size = [$width, $height];
51 }
52 }
53
59 protected function resize($width, $height)
60 {
61 $newSize = $this->dimensions($width, $height);
62 $blur = 0.5;
63
64 if (in_array($this->outputFormat, ["webp", "png"]) && $this->inputFormat == "png") {
65 $blur = 0.9;
66 }
67
68 if (!$this->bypassTest($newSize[0], $newSize[1])) {
69 $filter = $this->getResizeFilter($newSize[0], $newSize[1]);
70
71 $this->image->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN);
72 $this->image->setImageGravity(\Imagick::GRAVITY_CENTER);
73 $this->image->resizeImage($newSize[0], $newSize[1], $filter, $blur);
74 $this->image->setImageExtent($newSize[0], $newSize[1]);
75 $this->size = $newSize;
76 }
77 }
78
84 protected function thumb($width, $height)
85 {
86 [$width, $height] = $this->dimensions($width, $height);
87
88 if (!$this->bypassTest($width, $height)) {
89 $newSize = $this->dimensions($width, null);
90 $xOffset = 0;
91 $yOffset = 0;
92
93 if ($newSize[1] > $height) {
94 $newSize = $this->dimensions(null, $height);
95 $xOffset = -1 * round(($width - $newSize[0]) / 2);
96 } else {
97 $yOffset = -1 * round(($height - $newSize[1]) / 2);
98 }
99 $filter = $this->getResizeFilter($newSize[0], $newSize[1]);
100
101 $this->image->resizeImage($newSize[0], $newSize[1], $filter, 1, true);
102 $this->image->extentImage($width, $height, $xOffset, $yOffset);
103 $this->size = [$newSize[0], $newSize[1]];
104 }
105 }
106
112 protected function thumbfill($width, $height, $centerX = 50, $centerY = 50)
113 {
114 [$width, $height] = $this->dimensions($width, $height);
115
116 if (!$this->bypassTest($width, $height, $centerX - 50, $centerY - 50)) {
117 $newSize = $this->dimensions($width, null);
118 $centerX /= 100;
119 $centerY /= 100;
120
121 if ($newSize[1] < $height) {
122 $newSize = $this->dimensions(null, $height);
123 $xOffset = -1 * round(($width - $newSize[0]) * $centerX);
124 $yOffset = 0;
125 } else {
126 $xOffset = 0;
127 $yOffset = -1 * round(($height - $newSize[1]) * $centerY);
128 }
129 $filter = $this->getResizeFilter($newSize[0], $newSize[1]);
130
131 $this->image->resizeImage($newSize[0], $newSize[1], $filter, 1, true);
132 $this->image->extentImage($width, $height, $xOffset, $yOffset);
133 $this->size = [$width, $height];
134 }
135 }
136
143 protected function load(): void
144 {
145 $pageNumber = $this->getPageNumber();
146
147 $this->image = new \Imagick(realpath($this->input) . $pageNumber);
148 $this->image->transformImageColorspace(\Imagick::COLORSPACE_SRGB);
149 $this->setBackground();
150 }
151
157 protected function save()
158 {
159 $this->autoOrient();
160 if (in_array($this->outputFormat, ["webp"]) && $this->inputFormat == "png") {
161 $this->image->setImageCompressionQuality(100);
162 }
163 $result = $this->image->writeImage($this->output);
164
165 $this->image->clear();
166
167 if (!$result) {
168 throw new \Depage\Graphics\Exceptions\Exception('Could not save output image.');
169 }
170 }
171
177 protected function autoOrient(): void
178 {
179 switch ($this->image->getImageOrientation()) {
180 case \Imagick::ORIENTATION_TOPLEFT:
181 break;
182 case \Imagick::ORIENTATION_TOPRIGHT:
183 $this->image->flopImage();
184 break;
185 case \Imagick::ORIENTATION_BOTTOMRIGHT:
186 $this->image->rotateImage("#000", 180);
187 break;
188 case \Imagick::ORIENTATION_BOTTOMLEFT:
189 $this->image->flopImage();
190 $this->image->rotateImage("#000", 180);
191 break;
192 case \Imagick::ORIENTATION_LEFTTOP:
193 $this->image->flopImage();
194 $this->image->rotateImage("#000", -90);
195 break;
196 case \Imagick::ORIENTATION_RIGHTTOP:
197 $this->image->rotateImage("#000", 90);
198 break;
199 case \Imagick::ORIENTATION_RIGHTBOTTOM:
200 $this->image->flopImage();
201 $this->image->rotateImage("#000", 90);
202 break;
203 case \Imagick::ORIENTATION_LEFTBOTTOM:
204 $this->image->rotateImage("#000", -90);
205 break;
206 default: // Invalid orientation
207 break;
208 }
209 $this->image->setImageOrientation(\Imagick::ORIENTATION_TOPLEFT);
210 }
211
217 protected function setBackground()
218 {
219 if ($this->background[0] === '#') {
220 $this->image->setImageBackgroundColor($this->background);
221 } elseif ($this->background == 'checkerboard') {
222 } else {
223 if ($this->outputFormat == 'jpg') {
224 $this->image->setImageBackgroundColor('#fff');
225 } else {
226 $this->image->setImageBackgroundColor('transparent');
227 }
228 }
229 }
230
238 protected function getResizeFilter($width, $height)
239 {
240 if ($width <= 160 && $height <= 160) {
241 return \Imagick::FILTER_TRIANGLE;
242 }
243 return \Imagick::FILTER_LANCZOS;
244 }
245
250 protected function getImageSize(): array
251 {
252 $this->image = new \Imagick(realpath($this->input));
253
254 $imageSize = [
255 $this->image->getImageWidth(),
256 $this->image->getImageHeight(),
257 ];
258
259 return $imageSize;
260 }
261
271 public function render($input, $output = null): void
272 {
273 parent::render($input, $output);
274
275 $this->load();
276 $this->processQueue();
277
278 if ($this->otherRender && file_exists($this->output)) {
279 // do nothing file is already generated
280 } elseif ($this->bypass
281 && $this->inputFormat == $this->outputFormat
282 ) {
283 $this->bypass();
284 } else {
285 $this->save();
286
287 if ($this->optimize) {
288 $this->optimizeImage($this->output);
289 }
290 }
291
292 parent::renderFinished();
293 }
294}
295
296// vim:set ft=php sw=4 sts=4 fdm=marker et :
Main graphics class.
Definition Graphics.php:42
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
$input
Input filename.
Definition Graphics.php:46
$output
Output filename.
Definition Graphics.php:50
getPageNumber()
getPageNumber
Definition Graphics.php:540
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
Imagick Class Imagick.
Definition Imagick.php:20
thumbfill($width, $height, $centerX=50, $centerY=50)
thumbfill
Definition Imagick.php:112
setBackground()
Generates background command.
Definition Imagick.php:217
getResizeFilter($width, $height)
Gets the resize filter depending on target size.
Definition Imagick.php:238
canRead($ext)
Checks if extension support reading file type.
Definition Imagick.php:32
getImageSize()
Determine size of input image.
Definition Imagick.php:250
render($input, $output=null)
Main method for image handling.
Definition Imagick.php:271
resize($width, $height)
resize
Definition Imagick.php:59
crop($width, $height, $x=0, $y=0)
crop
Definition Imagick.php:43
thumb($width, $height)
thumb
Definition Imagick.php:84