Imagick.php
Go to the documentation of this file.
1<?php
13
19{
23 protected $image = null;
24
31 protected function crop($width, $height, $x = 0, $y = 0)
32 {
33 if (!$this->bypassTest($width, $height, $x, $y)) {
34 $this->image->setImageGravity(\Imagick::GRAVITY_NORTHWEST);
35 $this->image->cropImage($width, $height, $x, $y);
36 $this->image->extentImage($width, $height, 0, 0);
37 $this->image->setImagePage(0, 0, 0, 0);
38 $this->size = array($width, $height);
39 }
40 }
47 protected function resize($width, $height)
48 {
49 $newSize = $this->dimensions($width, $height);
50
51 if (!$this->bypassTest($newSize[0], $newSize[1])) {
52 $filter = $this->getResizeFilter($newSize[0], $newSize[1]);
53
54 $this->image->setImageGravity(\Imagick::GRAVITY_CENTER);
55 $this->image->resizeImage($newSize[0], $newSize[1], $filter, 1);
56 $this->image->setImageExtent($newSize[0], $newSize[1]);
57 $this->size = $newSize;
58 }
59 }
66 protected function thumb($width, $height)
67 {
68 list($width, $height) = $this->dimensions($width, $height);
69
70 if (!$this->bypassTest($width, $height)) {
71 $newSize = $this->dimensions($width, null);
72 $xOffset = 0;
73 $yOffset = 0;
74
75 if ($newSize[1] > $height) {
76 $newSize = $this->dimensions(null, $height);
77 $xOffset = -1 * round(($width - $newSize[0]) / 2);
78 } else {
79 $yOffset = -1 * round(($height - $newSize[1]) / 2);
80 }
81 $filter = $this->getResizeFilter($newSize[0], $newSize[1]);
82
83 $this->image->resizeImage($newSize[0], $newSize[1], $filter, 1, true);
84 $this->image->extentImage($width, $height, $xOffset, $yOffset);
85 $this->size = array($newSize[0], $newSize[1]);
86 }
87 }
94 protected function thumbfill($width, $height, $centerX = 50, $centerY = 50)
95 {
96 list($width, $height) = $this->dimensions($width, $height);
97
98 if (!$this->bypassTest($width, $height, $centerX - 50, $centerY - 50)) {
99 $newSize = $this->dimensions($width, null);
100 $centerX /= 100;
101 $centerY /= 100;
102
103 if ($newSize[1] < $height) {
104 $newSize = $this->dimensions(null, $height);
105 $xOffset = -1 * round(($width - $newSize[0]) * $centerX);
106 $yOffset = 0;
107 } else {
108 $xOffset = 0;
109 $yOffset = -1 * round(($height - $newSize[1]) * $centerY);
110 }
111 $filter = $this->getResizeFilter($newSize[0], $newSize[1]);
112
113 $this->image->resizeImage($newSize[0], $newSize[1], $filter, 1, true);
114 $this->image->extentImage($width, $height, $xOffset, $yOffset);
115 $this->size = array($width, $height);
116 }
117 }
118
125 protected function load()
126 {
127 $this->image = new \Imagick(realpath($this->input));
128 $this->image->transformImageColorspace(\Imagick::COLORSPACE_SRGB);
129 $this->setBackground();
130 }
137 protected function save()
138 {
139
140 $result = $this->image->writeImage($this->output);
141
142 $this->image->clear();
143
144 if (!$result) {
145 throw new \Depage\Graphics\Exceptions\Exception('Could not save output image.');
146 }
147 }
148
154 protected function setBackground()
155 {
156 if ($this->background[0] === '#') {
157 $this->image->setImageBackgroundColor($this->background);
158 } elseif ($this->background == 'checkerboard') {
159 } else {
160 if ($this->outputFormat == 'jpg') {
161 $this->image->setImageBackgroundColor('#fff');
162 } else {
163 $this->image->setImageBackgroundColor('transparent');
164 }
165 }
166 }
175 protected function getResizeFilter($width, $height)
176 {
177 if ($width <= 160 && $height <= 160) {
178 return \Imagick::FILTER_TRIANGLE;
179 } else {
180 return \Imagick::FILTER_LANCZOS;
181 }
182 }
188 protected function getImageSize()
189 {
190 $this->image = new \Imagick(realpath($this->input));
191
192 $imageSize = [
193 $this->image->getImageWidth(),
194 $this->image->getImageHeight()
195 ];
196
197 return $imageSize;
198 }
199
209 public function render($input, $output = null)
210 {
211 parent::render($input, $output);
212
213 $this->load();
214 $this->processQueue();
215
216 if ($this->otherRender && file_exists($this->output)) {
217 // do nothing file is already generated
218 } else if ($this->bypass
219 && $this->inputFormat == $this->outputFormat
220 ) {
221 $this->bypass();
222 } else {
223 $this->save();
224
225 if ($this->optimize) {
226 $this->optimizeImage($this->output);
227 }
228 }
229
230 parent::renderFinished();
231 }
232}
233
234// vim:set ft=php sw=4 sts=4 fdm=marker et :
Main graphics class.
Definition: Graphics.php:41
processQueue()
Process action queue.
Definition: Graphics.php:299
bypassTest($width, $height, $x=0, $y=0)
Tests if action would change current image.
Definition: Graphics.php:512
$input
Input filename.
Definition: Graphics.php:45
$output
Output filename.
Definition: Graphics.php:49
dimensions($width, $height)
Scales image dimensions.
Definition: Graphics.php:278
optimizeImage($filename)
Opimizes final image through one of the optimization programs.
Definition: Graphics.php:350
bypass()
Runs bypass (copies file)
Definition: Graphics.php:540
Imagick Class Imagick.
Definition: Imagick.php:19
thumbfill($width, $height, $centerX=50, $centerY=50)
thumbfill
Definition: Imagick.php:94
setBackground()
Generates background command.
Definition: Imagick.php:154
getResizeFilter($width, $height)
Gets the resize filter depending on target size.
Definition: Imagick.php:175
getImageSize()
Determine size of input image.
Definition: Imagick.php:188
render($input, $output=null)
Main method for image handling.
Definition: Imagick.php:209
resize($width, $height)
resize
Definition: Imagick.php:47
crop($width, $height, $x=0, $y=0)
crop
Definition: Imagick.php:31
thumb($width, $height)
thumb
Definition: Imagick.php:66