Gd.php
Go to the documentation of this file.
1<?php
11
19{
23 protected $image;
24
36 protected function crop($width, $height, $x = 0, $y = 0)
37 {
38 if (!$this->bypassTest($width, $height, $x, $y)) {
39 $newImage = $this->createCanvas($width, $height);
40
41 imagecopy(
42 $newImage,
43 $this->image,
44 ($x > 0) ? 0 : abs($x),
45 ($y > 0) ? 0 : abs($y),
46 ($x < 0) ? 0 : $x,
47 ($y < 0) ? 0 : $y,
48 $this->size[0] - abs($x),
49 $this->size[1] - abs($y)
50 );
51
52 $this->image = $newImage;
53 $this->size = array($width, $height);
54 }
55 }
65 protected function resize($width, $height)
66 {
67 $newSize = $this->dimensions($width, $height);
68
69 if (!$this->bypassTest($newSize[0], $newSize[1])) {
70 $newImage = $this->createCanvas($newSize[0], $newSize[1]);
71 imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
72
73 $this->image = $newImage;
74 $this->size = $newSize;
75 }
76 }
86 protected function thumb($width, $height)
87 {
88 list($width, $height) = $this->dimensions($width, $height);
89
90 if (!$this->bypassTest($width, $height)) {
91 $newSize = $this->dimensions($width, null);
92
93 if ($newSize[1] > $height) {
94 $newSize = $this->dimensions(null, $height);
95 $xOffset = round(($width - $newSize[0]) / 2);
96 $yOffset = 0;
97 } else {
98 $xOffset = 0;
99 $yOffset = round(($height - $newSize[1]) / 2);
100 }
101
102 $newImage = $this->createCanvas($width, $height);
103
104 imagecopyresampled($newImage, $this->image, $xOffset, $yOffset, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
105
106 $this->image = $newImage;
107 $this->size = array($width, $height);
108 }
109 }
121 protected function thumbfill($width, $height, $centerX = 50, $centerY = 50)
122 {
123 list($width, $height) = $this->dimensions($width, $height);
124
125 if (!$this->bypassTest($width, $height, $centerX - 50, $centerY - 50)) {
126 $newSize = $this->dimensions($width, null);
127 $centerX /= 100;
128 $centerY /= 100;
129
130 if ($newSize[1] < $height) {
131 $newSize = $this->dimensions(null, $height);
132 $xOffset = round(($width - $newSize[0]) * $centerX);
133 $yOffset = 0;
134 } else {
135 $xOffset = 0;
136 $yOffset = round(($height - $newSize[1]) * $centerY);
137 }
138
139 $newImage = $this->createCanvas($width, $height);
140
141 imagecopyresampled($newImage, $this->image, $xOffset, $yOffset, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
142
143 $this->image = $newImage;
144 $this->size = array($width, $height);
145 }
146 }
147
155 protected function load()
156 {
157 if ($this->inputFormat == 'gif' && function_exists('imagecreatefromgif')) {
158 //GIF
159 $this->image = imagecreatefromgif($this->input);
160 } elseif ($this->inputFormat == 'jpg') {
161 //JPEG
162 $this->image = imagecreatefromjpeg($this->input);
163 } elseif ($this->inputFormat == 'png') {
164 //PNG
165 $this->image = imagecreatefrompng($this->input);
166 } elseif ($this->inputFormat == 'webp' && function_exists('imagecreatefromwebp')) {
167 //WEBP
168 $this->image = imagecreatefromwebp($this->input);
169 } else {
170 throw new \Depage\Graphics\Exceptions\Exception('Unknown image format.');
171 }
172 }
180 protected function save()
181 {
182 $bg = $this->createBackground($this->size[0], $this->size[1]);
183 imagecopy($bg, $this->image, 0, 0, 0, 0, $this->size[0], $this->size[1]);
184 $this->image = $bg;
185 $result = false;
186
187 if ($this->outputFormat == 'gif' && function_exists('imagegif')) {
188 $result = imagegif($this->image, $this->output);
189 } elseif ($this->outputFormat == 'jpg') {
190 $result = imagejpeg($this->image, $this->output, $this->getQuality());
191 } elseif ($this->outputFormat == 'png') {
192 $quality = (int) ($this->getQuality() / 10);
193 $result = imagepng($this->image, $this->output, $quality, PNG_ALL_FILTERS);
194 } elseif ($this->outputFormat == 'webp' && function_exists('imagewebp')) {
195 $result = imagewebp($this->image, $this->output, $this->getQuality());
196 }
197 if (!$result) {
198 throw new \Depage\Graphics\Exceptions\Exception('Could not save output image.');
199 }
200 }
201
207 protected function getImageSize()
208 {
209 return getimagesize($this->input);
210 }
211
221 public function render($input, $output = null)
222 {
223 parent::render($input, $output);
224
225 $this->load();
226 $this->processQueue();
227
228 if ($this->otherRender && file_exists($this->output)) {
229 // do nothing file is already generated
230 } else if ($this->bypass
231 && $this->inputFormat == $this->outputFormat
232 ) {
233 $this->bypass();
234 } else {
235 $this->save();
236
237 if ($this->optimize) {
238 $this->optimizeImage($this->output);
239 }
240 }
241
242 parent::renderFinished();
243 }
244
252 private function createCanvas($width, $height)
253 {
254 $canvas = imagecreatetruecolor($width, $height);
255 $bg = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
256 imagefill($canvas, 0, 0, $bg);
257
258 return $canvas;
259 }
269 private function createBackground($width, $height)
270 {
271 $newImage = imagecreatetruecolor($width, $height);
272
273 if ($this->background[0] == '#') {
277 $color = substr($this->background, 1);
278
279 if (strlen($color) == 6) {
280 list($r, $g, $b) = array(
281 $color[0].$color[1],
282 $color[2].$color[3],
283 $color[4].$color[5]
284 );
285 } elseif (strlen($color) == 3) {
286 list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
287 }
288
289 $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
290
291 imagefill($newImage, 0, 0, imagecolorallocate($newImage, $r, $g, $b));
292 } elseif ($this->background == 'checkerboard') {
293 $transLen = 15;
294 $transColor = array();
295 $transColor[0] = imagecolorallocate ($newImage, 153, 153, 153);
296 $transColor[1] = imagecolorallocate ($newImage, 102, 102, 102);
297 for ($i = 0; $i * $transLen < $width; $i++) {
298 for ($j = 0; $j * $transLen < $height; $j++) {
299 imagefilledrectangle(
300 $newImage,
301 $i * $transLen,
302 $j * $transLen,
303 ($i + 1) * $transLen,
304 ($j + 1) * $transLen,
305 $transColor[$j % 2 == 0 ? $i % 2 : ($i % 2 == 0 ? 1 : 0)]
306 );
307 }
308 }
309 } elseif ($this->background == 'transparent') {
310 imagefill($newImage, 0, 0, imagecolorallocatealpha($newImage, 255, 255, 255, 127));
311 if ($this->outputFormat == 'gif') imagecolortransparent($newImage, imagecolorallocatealpha($newImage, 255, 255, 255, 127));
312 imagesavealpha($newImage, true);
313 }
314
315 return $newImage;
316 }
317}
318
319/* 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:121
load()
Loads image from file.
Definition Gd.php:155
getImageSize()
Determine size of input image.
Definition Gd.php:207
render($input, $output=null)
Main method for image handling.
Definition Gd.php:221
resize($width, $height)
Resize action.
Definition Gd.php:65
$image
Image resource identifier.
Definition Gd.php:23
crop($width, $height, $x=0, $y=0)
Crop action.
Definition Gd.php:36
thumb($width, $height)
Thumb action.
Definition Gd.php:86
save()
Saves image to file.
Definition Gd.php:180