Gd.php
Go to the documentation of this file.
1<?php
11
19{
31 protected function crop($width, $height, $x = 0, $y = 0)
32 {
33 if (!$this->bypassTest($width, $height, $x, $y)) {
34 $newImage = $this->createCanvas($width, $height);
35
36 imagecopy(
37 $newImage,
38 $this->image,
39 ($x > 0) ? 0 : abs($x),
40 ($y > 0) ? 0 : abs($y),
41 ($x < 0) ? 0 : $x,
42 ($y < 0) ? 0 : $y,
43 $this->size[0] - abs($x),
44 $this->size[1] - abs($y)
45 );
46
47 $this->image = $newImage;
48 $this->size = array($width, $height);
49 }
50 }
60 protected function resize($width, $height)
61 {
62 $newSize = $this->dimensions($width, $height);
63
64 if (!$this->bypassTest($newSize[0], $newSize[1])) {
65 $newImage = $this->createCanvas($newSize[0], $newSize[1]);
66 imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
67
68 $this->image = $newImage;
69 $this->size = $newSize;
70 }
71 }
81 protected function thumb($width, $height)
82 {
83 list($width, $height) = $this->dimensions($width, $height);
84
85 if (!$this->bypassTest($width, $height)) {
86 $newSize = $this->dimensions($width, null);
87
88 if ($newSize[1] > $height) {
89 $newSize = $this->dimensions(null, $height);
90 $xOffset = round(($width - $newSize[0]) / 2);
91 $yOffset = 0;
92 } else {
93 $xOffset = 0;
94 $yOffset = round(($height - $newSize[1]) / 2);
95 }
96
97 $newImage = $this->createCanvas($width, $height);
98
99 imagecopyresampled($newImage, $this->image, $xOffset, $yOffset, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
100
101 $this->image = $newImage;
102 $this->size = array($width, $height);
103 }
104 }
116 protected function thumbfill($width, $height, $centerX = 50, $centerY = 50)
117 {
118 list($width, $height) = $this->dimensions($width, $height);
119
120 if (!$this->bypassTest($width, $height, $centerX - 50, $centerY - 50)) {
121 $newSize = $this->dimensions($width, null);
122 $centerX /= 100;
123 $centerY /= 100;
124
125 if ($newSize[1] < $height) {
126 $newSize = $this->dimensions(null, $height);
127 $xOffset = round(($width - $newSize[0]) * $centerX);
128 $yOffset = 0;
129 } else {
130 $xOffset = 0;
131 $yOffset = round(($height - $newSize[1]) * $centerY);
132 }
133
134 $newImage = $this->createCanvas($width, $height);
135
136 imagecopyresampled($newImage, $this->image, $xOffset, $yOffset, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
137
138 $this->image = $newImage;
139 $this->size = array($width, $height);
140 }
141 }
142
150 protected function load()
151 {
152 if ($this->inputFormat == 'gif' && function_exists('imagecreatefromgif')) {
153 //GIF
154 $this->image = imagecreatefromgif($this->input);
155 } elseif ($this->inputFormat == 'jpg') {
156 //JPEG
157 $this->image = imagecreatefromjpeg($this->input);
158 } elseif ($this->inputFormat == 'png') {
159 //PNG
160 $this->image = imagecreatefrompng($this->input);
161 } elseif ($this->inputFormat == 'webp' && function_exists('imagecreatefromwebp')) {
162 //WEBP
163 $this->image = imagecreatefromwebp($this->input);
164 } else {
165 throw new \Depage\Graphics\Exceptions\Exception('Unknown image format.');
166 }
167 }
175 protected function save()
176 {
177 $bg = $this->createBackground($this->size[0], $this->size[1]);
178 imagecopy($bg, $this->image, 0, 0, 0, 0, $this->size[0], $this->size[1]);
179 $this->image = $bg;
180 $result = false;
181
182 if ($this->outputFormat == 'gif' && function_exists('imagegif')) {
183 $result = imagegif($this->image, $this->output);
184 } elseif ($this->outputFormat == 'jpg') {
185 $result = imagejpeg($this->image, $this->output, $this->getQuality());
186 } elseif ($this->outputFormat == 'png') {
187 $quality = (int) ($this->getQuality() / 10);
188 $result = imagepng($this->image, $this->output, $quality, PNG_ALL_FILTERS);
189 } elseif ($this->outputFormat == 'webp' && function_exists('imagewebp')) {
190 $result = imagewebp($this->image, $this->output, $this->getQuality());
191 }
192 if (!$result) {
193 throw new \Depage\Graphics\Exceptions\Exception('Could not save output image.');
194 }
195 }
196
202 protected function getImageSize()
203 {
204 return getimagesize($this->input);
205 }
206
216 public function render($input, $output = null)
217 {
218 parent::render($input, $output);
219
220 $this->load();
221 $this->processQueue();
222
223 if ($this->otherRender && file_exists($this->output)) {
224 // do nothing file is already generated
225 } else if ($this->bypass
226 && $this->inputFormat == $this->outputFormat
227 ) {
228 $this->bypass();
229 } else {
230 $this->save();
231
232 if ($this->optimize) {
233 $this->optimizeImage($this->output);
234 }
235 }
236
237 parent::renderFinished();
238 }
239
247 private function createCanvas($width, $height)
248 {
249 $canvas = imagecreatetruecolor($width, $height);
250 $bg = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
251 imagefill($canvas, 0, 0, $bg);
252
253 return $canvas;
254 }
264 private function createBackground($width, $height)
265 {
266 $newImage = imagecreatetruecolor($width, $height);
267
268 if ($this->background[0] == '#') {
272 $color = substr($this->background, 1);
273
274 if (strlen($color) == 6) {
275 list($r, $g, $b) = array(
276 $color[0].$color[1],
277 $color[2].$color[3],
278 $color[4].$color[5]
279 );
280 } elseif (strlen($color) == 3) {
281 list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
282 }
283
284 $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
285
286 imagefill($newImage, 0, 0, imagecolorallocate($newImage, $r, $g, $b));
287 } elseif ($this->background == 'checkerboard') {
288 $transLen = 15;
289 $transColor = array();
290 $transColor[0] = imagecolorallocate ($newImage, 153, 153, 153);
291 $transColor[1] = imagecolorallocate ($newImage, 102, 102, 102);
292 for ($i = 0; $i * $transLen < $width; $i++) {
293 for ($j = 0; $j * $transLen < $height; $j++) {
294 imagefilledrectangle(
295 $newImage,
296 $i * $transLen,
297 $j * $transLen,
298 ($i + 1) * $transLen,
299 ($j + 1) * $transLen,
300 $transColor[$j % 2 == 0 ? $i % 2 : ($i % 2 == 0 ? 1 : 0)]
301 );
302 }
303 }
304 } elseif ($this->background == 'transparent') {
305 imagefill($newImage, 0, 0, imagecolorallocatealpha($newImage, 255, 255, 255, 127));
306 if ($this->outputFormat == 'gif') imagecolortransparent($newImage, imagecolorallocatealpha($newImage, 255, 255, 255, 127));
307 imagesavealpha($newImage, true);
308 }
309
310 return $newImage;
311 }
312}
313
314/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
Main graphics class.
Definition: Graphics.php:41
$quality
Image quality string.
Definition: Graphics.php:77
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
getQuality()
Returns quality-index for current image format.
Definition: Graphics.php:463
$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
PHP GD extension interface.
Definition: Gd.php:19
thumbfill($width, $height, $centerX=50, $centerY=50)
Thumb-Fill action.
Definition: Gd.php:116
load()
Loads image from file.
Definition: Gd.php:150
getImageSize()
Determine size of input image.
Definition: Gd.php:202
render($input, $output=null)
Main method for image handling.
Definition: Gd.php:216
resize($width, $height)
Resize action.
Definition: Gd.php:60
crop($width, $height, $x=0, $y=0)
Crop action.
Definition: Gd.php:31
thumb($width, $height)
Thumb action.
Definition: Gd.php:81
save()
Saves image to file.
Definition: Gd.php:175