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 = array($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 list($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 = array($newSize[0], $newSize[1]);
104 }
105 }
106
112 protected function thumbfill($width, $height, $centerX = 50, $centerY = 50)
113 {
114 list($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 = array($width, $height);
134 }
135 }
136
143 protected function load()
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 if (in_array($this->outputFormat, ["webp"]) && $this->inputFormat == "png") {
160 $this->image->setImageCompressionQuality(100);
161 }
162 $result = $this->image->writeImage($this->output);
163
164 $this->image->clear();
165
166 if (!$result) {
167 throw new \Depage\Graphics\Exceptions\Exception('Could not save output image.');
168 }
169 }
170
176 protected function setBackground()
177 {
178 if ($this->background[0] === '#') {
179 $this->image->setImageBackgroundColor($this->background);
180 } elseif ($this->background == 'checkerboard') {
181 } else {
182 if ($this->outputFormat == 'jpg') {
183 $this->image->setImageBackgroundColor('#fff');
184 } else {
185 $this->image->setImageBackgroundColor('transparent');
186 }
187 }
188 }
189
197 protected function getResizeFilter($width, $height)
198 {
199 if ($width <= 160 && $height <= 160) {
200 return \Imagick::FILTER_TRIANGLE;
201 }
202 return \Imagick::FILTER_LANCZOS;
203 }
204
209 protected function getImageSize()
210 {
211 $this->image = new \Imagick(realpath($this->input));
212
213 $imageSize = [
214 $this->image->getImageWidth(),
215 $this->image->getImageHeight()
216 ];
217
218 return $imageSize;
219 }
220
230 public function render($input, $output = null)
231 {
232 parent::render($input, $output);
233
234 $this->load();
235 $this->processQueue();
236
237 if ($this->otherRender && file_exists($this->output)) {
238 // do nothing file is already generated
239 } elseif ($this->bypass
240 && $this->inputFormat == $this->outputFormat
241 ) {
242 $this->bypass();
243 } else {
244 $this->save();
245
246 if ($this->optimize) {
247 $this->optimizeImage($this->output);
248 }
249 }
250
251 parent::renderFinished();
252 }
253}
254
255// 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:322
bypassTest($width, $height, $x=0, $y=0)
Tests if action would change current image.
Definition Graphics.php:551
$input
Input filename.
Definition Graphics.php:46
$output
Output filename.
Definition Graphics.php:50
getPageNumber()
getPageNumber
Definition Graphics.php:533
dimensions($width, $height)
Scales image dimensions.
Definition Graphics.php:301
optimizeImage($filename)
Opimizes final image through one of the optimization programs.
Definition Graphics.php:375
bypass()
Runs bypass (copies file)
Definition Graphics.php:579
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:176
getResizeFilter($width, $height)
Gets the resize filter depending on target size.
Definition Imagick.php:197
canRead($ext)
Checks if extension support reading file type.
Definition Imagick.php:32
getImageSize()
Determine size of input image.
Definition Imagick.php:209
render($input, $output=null)
Main method for image handling.
Definition Imagick.php:230
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