GD.php
25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
<?php
namespace common\components\artboximage\drivers\Kohana\Image;
use common\components\artboximage\drivers\Kohana\Kohana_Image;
use yii\base\ErrorException;
/**
* Support for image manipulation using [GD](http://php.net/GD).
*
* @package Kohana/Image
* @category Drivers
* @author Kohana Team
* @copyright (c) 2008-2009 Kohana Team
* @license http://kohanaphp.com/license.html
*/
class Kohana_Image_GD extends Kohana_Image {
// Which GD functions are available?
const IMAGEROTATE = 'imagerotate';
const IMAGECONVOLUTION = 'imageconvolution';
const IMAGEFILTER = 'imagefilter';
const IMAGELAYEREFFECT = 'imagelayereffect';
protected static $_available_functions = array();
/**
* Checks if GD is enabled and verify that key methods exist, some of which require GD to
* be bundled with PHP. Exceptions will be thrown from those methods when GD is not
* bundled.
*
* @return boolean
*/
public static function check()
{
if ( ! function_exists('gd_info'))
{
throw new ErrorException('GD is either not installed or not enabled, check your configuration');
}
$functions = array(
Image_GD::IMAGEROTATE,
Image_GD::IMAGECONVOLUTION,
Image_GD::IMAGEFILTER,
Image_GD::IMAGELAYEREFFECT
);
foreach ($functions as $function)
{
Image_GD::$_available_functions[$function] = function_exists($function);
}
if (defined('GD_VERSION'))
{
// Get the version via a constant, available in PHP 5.2.4+
$version = GD_VERSION;
}
else
{
// Get the version information
$info = gd_info();
// Extract the version number
preg_match('/\d+\.\d+(?:\.\d+)?/', $info['GD Version'], $matches);
// Get the major version
$version = $matches[0];
}
if ( ! version_compare($version, '2.0.1', '>='))
{
throw new ErrorException(sprintf('Image_GD requires GD version 2.0.1 or greater, you have %s',$version));
}
return Image_GD::$_checked = TRUE;
}
/* @var resource Temporary image resource */
protected $_image;
/* @var string Function name to open Image */
protected $_create_function;
/**
* Runs [Image_GD::check] and loads the image.
*
* @param string $file image file path
* @return void
* @throws ErrorException
*/
public function __construct($file)
{
if ( ! Image_GD::$_checked)
{
// Run the install check
Image_GD::check();
}
parent::__construct($file);
// Set the image creation function name
switch ($this->type)
{
case IMAGETYPE_JPEG:
$create = 'imagecreatefromjpeg';
break;
case IMAGETYPE_GIF:
$create = 'imagecreatefromgif';
break;
case IMAGETYPE_PNG:
$create = 'imagecreatefrompng';
break;
}
if ( ! isset($create) OR ! function_exists($create))
{
throw new ErrorException(sprintf('Installed GD does not support %s images',image_type_to_extension($this->type, FALSE)));
}
// Save function for future use
$this->_create_function = $create;
// Save filename for lazy loading
$this->_image = $this->file;
}
/**
* Destroys the loaded image to free up resources.
*
* @return void
*/
public function __destruct()
{
if (is_resource($this->_image))
{
// Free all resources
imagedestroy($this->_image);
}
}
/**
* Loads an image into GD.
*
* @return void
*/
protected function _load_image()
{
if ( ! is_resource($this->_image))
{
// Gets create function
$create = $this->_create_function;
// Open the temporary image
$this->_image = $create($this->file);
// Preserve transparency when saving
imagesavealpha($this->_image, TRUE);
}
}
/**
* Execute a resize.
*
* @param integer $width new width
* @param integer $height new height
* @return void
*/
protected function _do_resize($width, $height)
{
// Presize width and height
$pre_width = $this->width;
$pre_height = $this->height;
// Loads image if not yet loaded
$this->_load_image();
// Test if we can do a resize without resampling to speed up the final resize
if ($width > ($this->width / 2) AND $height > ($this->height / 2))
{
// The maximum reduction is 10% greater than the final size
$reduction_width = round($width * 1.1);
$reduction_height = round($height * 1.1);
while ($pre_width / 2 > $reduction_width AND $pre_height / 2 > $reduction_height)
{
// Reduce the size using an O(2n) algorithm, until it reaches the maximum reduction
$pre_width /= 2;
$pre_height /= 2;
}
// Create the temporary image to copy to
$image = $this->_create($pre_width, $pre_height);
if (imagecopyresized($image, $this->_image, 0, 0, 0, 0, $pre_width, $pre_height, $this->width, $this->height))
{
// Swap the new image for the old one
imagedestroy($this->_image);
$this->_image = $image;
}
}
// Create the temporary image to copy to
$image = $this->_create($width, $height);
// Execute the resize
if (imagecopyresampled($image, $this->_image, 0, 0, 0, 0, $width, $height, $pre_width, $pre_height))
{
// Swap the new image for the old one
imagedestroy($this->_image);
$this->_image = $image;
// Reset the width and height
$this->width = imagesx($image);
$this->height = imagesy($image);
}
}
/**
* Adaptation the image.
*
* @param integer $width image width
* @param integer $height image height
* @param integer $bg_width background width
* @param integer $bg_height background height
* @param integer $offset_x offset from the left
* @param integer $offset_y offset from the top
*/
protected function _do_adapt($width, $height, $bg_width, $bg_height, $offset_x, $offset_y)
{
$this->_load_image();
$image = $this->_image;
$this->_image = $this->_create($bg_width, $bg_height);
$this->width = $bg_width;
$this->height = $bg_height;
imagealphablending($this->_image, false);
$col = imagecolorallocatealpha($this->_image, 0, 255, 0, 127);
imagefilledrectangle($this->_image, 0, 0, $bg_width, $bg_height, $col);
imagealphablending($this->_image, true);
imagecopy($this->_image, $image, $offset_x, $offset_y, 0, 0, $width, $height);
imagealphablending($this->_image, false);
imagesavealpha($this->_image, true);
imagedestroy($image);
}
/**
* Execute a crop.
*
* @param integer $width new width
* @param integer $height new height
* @param integer $offset_x offset from the left
* @param integer $offset_y offset from the top
* @return void
*/
protected function _do_crop($width, $height, $offset_x, $offset_y)
{
// Create the temporary image to copy to
$image = $this->_create($width, $height);
// Loads image if not yet loaded
$this->_load_image();
// Execute the crop
if (imagecopyresampled($image, $this->_image, 0, 0, $offset_x, $offset_y, $width, $height, $width, $height))
{
// Swap the new image for the old one
imagedestroy($this->_image);
$this->_image = $image;
// Reset the width and height
$this->width = imagesx($image);
$this->height = imagesy($image);
}
}
/**
* Execute a rotation.
*
* @param integer $degrees degrees to rotate
* @return void
*/
protected function _do_rotate($degrees)
{
if (empty(Image_GD::$_available_functions[Image_GD::IMAGEROTATE]))
{
throw new ErrorException('This method requires imagerotate, which is only available in the bundled version of GD');
}
// Loads image if not yet loaded
$this->_load_image();
// Transparent black will be used as the background for the uncovered region
$transparent = imagecolorallocatealpha($this->_image, 0, 0, 0, 127);
// Rotate, setting the transparent color
$image = imagerotate($this->_image, 360 - $degrees, $transparent, 1);
// Save the alpha of the rotated image
imagesavealpha($image, TRUE);
// Get the width and height of the rotated image
$width = imagesx($image);
$height = imagesy($image);
if (imagecopymerge($this->_image, $image, 0, 0, 0, 0, $width, $height, 100))
{
// Swap the new image for the old one
imagedestroy($this->_image);
$this->_image = $image;
// Reset the width and height
$this->width = $width;
$this->height = $height;
}
}
/**
* Execute a flip.
*
* @param integer $direction direction to flip
* @return void
*/
protected function _do_flip($direction)
{
// Create the flipped image
$flipped = $this->_create($this->width, $this->height);
// Loads image if not yet loaded
$this->_load_image();
if ($direction === Image::HORIZONTAL)
{
for ($x = 0; $x < $this->width; $x++)
{
// Flip each row from top to bottom
imagecopy($flipped, $this->_image, $x, 0, $this->width - $x - 1, 0, 1, $this->height);
}
}
else
{
for ($y = 0; $y < $this->height; $y++)
{
// Flip each column from left to right
imagecopy($flipped, $this->_image, 0, $y, 0, $this->height - $y - 1, $this->width, 1);
}
}
// Swap the new image for the old one
imagedestroy($this->_image);
$this->_image = $flipped;
// Reset the width and height
$this->width = imagesx($flipped);
$this->height = imagesy($flipped);
}
/**
* Execute a sharpen.
*
* @param integer $amount amount to sharpen
* @return void
*/
protected function _do_sharpen($amount)
{
if (empty(Image_GD::$_available_functions[Image_GD::IMAGECONVOLUTION]))
{
throw new ErrorException('This method requires imageconvolution, which is only available in the bundled version of GD');
}
// Loads image if not yet loaded
$this->_load_image();
// Amount should be in the range of 18-10
$amount = round(abs(-18 + ($amount * 0.08)), 2);
// Gaussian blur matrix
$matrix = array
(
array(-1, -1, -1),
array(-1, $amount, -1),
array(-1, -1, -1),
);
// Perform the sharpen
if (imageconvolution($this->_image, $matrix, $amount - 8, 0))
{
// Reset the width and height
$this->width = imagesx($this->_image);
$this->height = imagesy($this->_image);
}
}
/**
* Execute a reflection.
*
* @param integer $height reflection height
* @param integer $opacity reflection opacity
* @param boolean $fade_in TRUE to fade out, FALSE to fade in
* @return void
*/
protected function _do_reflection($height, $opacity, $fade_in)
{
if (empty(Image_GD::$_available_functions[Image_GD::IMAGEFILTER]))
{
throw new ErrorException('This method requires imagefilter, which is only available in the bundled version of GD');
}
// Loads image if not yet loaded
$this->_load_image();
// Convert an opacity range of 0-100 to 127-0
$opacity = round(abs(($opacity * 127 / 100) - 127));
if ($opacity < 127)
{
// Calculate the opacity stepping
$stepping = (127 - $opacity) / $height;
}
else
{
// Avoid a "divide by zero" error
$stepping = 127 / $height;
}
// Create the reflection image
$reflection = $this->_create($this->width, $this->height + $height);
// Copy the image to the reflection
imagecopy($reflection, $this->_image, 0, 0, 0, 0, $this->width, $this->height);
for ($offset = 0; $height >= $offset; $offset++)
{
// Read the next line down
$src_y = $this->height - $offset - 1;
// Place the line at the bottom of the reflection
$dst_y = $this->height + $offset;
if ($fade_in === TRUE)
{
// Start with the most transparent line first
$dst_opacity = round($opacity + ($stepping * ($height - $offset)));
}
else
{
// Start with the most opaque line first
$dst_opacity = round($opacity + ($stepping * $offset));
}
// Create a single line of the image
$line = $this->_create($this->width, 1);
// Copy a single line from the current image into the line
imagecopy($line, $this->_image, 0, 0, 0, $src_y, $this->width, 1);
// Colorize the line to add the correct alpha level
imagefilter($line, IMG_FILTER_COLORIZE, 0, 0, 0, $dst_opacity);
// Copy a the line into the reflection
imagecopy($reflection, $line, 0, $dst_y, 0, 0, $this->width, 1);
}
// Swap the new image for the old one
imagedestroy($this->_image);
$this->_image = $reflection;
// Reset the width and height
$this->width = imagesx($reflection);
$this->height = imagesy($reflection);
}
/**
* Execute a watermarking.
*
* @param Kohana_Image $image watermarking Kohana_Image
* @param integer $offset_x offset from the left
* @param integer $offset_y offset from the top
* @param integer $opacity opacity of watermark
* @return void
*/
protected function _do_watermark(Kohana_Image $watermark, $offset_x, $offset_y, $opacity)
{
if (empty(Image_GD::$_available_functions[Image_GD::IMAGELAYEREFFECT]))
{
throw new ErrorException('This method requires imagelayereffect, which is only available in the bundled version of GD');
}
// Loads image if not yet loaded
$this->_load_image();
// Create the watermark image resource
$overlay = imagecreatefromstring($watermark->render());
imagesavealpha($overlay, TRUE);
// Get the width and height of the watermark
$width = imagesx($overlay);
$height = imagesy($overlay);
if ($opacity < 100)
{
// Convert an opacity range of 0-100 to 127-0
$opacity = round(abs(($opacity * 127 / 100) - 127));
// Allocate transparent gray
$color = imagecolorallocatealpha($overlay, 127, 127, 127, $opacity);
// The transparent image will overlay the watermark
imagelayereffect($overlay, IMG_EFFECT_OVERLAY);
// Fill the background with the transparent color
imagefilledrectangle($overlay, 0, 0, $width, $height, $color);
}
// Alpha blending must be enabled on the background!
imagealphablending($this->_image, TRUE);
if (imagecopy($this->_image, $overlay, $offset_x, $offset_y, 0, 0, $width, $height))
{
// Destroy the overlay image
imagedestroy($overlay);
}
}
/**
* Execute a background.
*
* @param integer $r red
* @param integer $g green
* @param integer $b blue
* @param integer $opacity opacity
* @return void
*/
protected function _do_background($r, $g, $b, $opacity)
{
// Loads image if not yet loaded
$this->_load_image();
// Convert an opacity range of 0-100 to 127-0
$opacity = round(abs(($opacity * 127 / 100) - 127));
// Create a new background
$background = $this->_create($this->width, $this->height);
// Allocate the color
$color = imagecolorallocatealpha($background, $r, $g, $b, $opacity);
// Fill the image with white
imagefilledrectangle($background, 0, 0, $this->width, $this->height, $color);
// Alpha blending must be enabled on the background!
imagealphablending($background, TRUE);
// Copy the image onto a white background to remove all transparency
if (imagecopy($background, $this->_image, 0, 0, 0, 0, $this->width, $this->height))
{
// Swap the new image for the old one
imagedestroy($this->_image);
$this->_image = $background;
}
}
/**
* Execute a save.
*
* @param string $file new image filename
* @param integer $quality quality
* @return boolean
*/
protected function _do_save($file, $quality)
{
// Loads image if not yet loaded
$this->_load_image();
// Get the extension of the file
$extension = pathinfo($file, PATHINFO_EXTENSION);
// Get the save function and IMAGETYPE
list($save, $type) = $this->_save_function($extension, $quality);
// Save the image to a file
$status = isset($quality) ? $save($this->_image, $file, $quality) : $save($this->_image, $file);
if ($status === TRUE AND $type !== $this->type)
{
// Reset the image type and mime type
$this->type = $type;
$this->mime = image_type_to_mime_type($type);
}
return TRUE;
}
/**
* Execute a render.
*
* @param string $type image type: png, jpg, gif, etc
* @param integer $quality quality
* @return string
*/
protected function _do_render($type, $quality)
{
// Loads image if not yet loaded
$this->_load_image();
// Get the save function and IMAGETYPE
list($save, $type) = $this->_save_function($type, $quality);
// Capture the output
ob_start();
// Render the image
$status = isset($quality) ? $save($this->_image, NULL, $quality) : $save($this->_image, NULL);
if ($status === TRUE AND $type !== $this->type)
{
// Reset the image type and mime type
$this->type = $type;
$this->mime = image_type_to_mime_type($type);
}
return ob_get_clean();
}
/**
* Get the GD saving function and image type for this extension.
* Also normalizes the quality setting
*
* @param string $extension image type: png, jpg, etc
* @param integer $quality image quality
* @return array save function, IMAGETYPE_* constant
* @throws ErrorException
*/
protected function _save_function($extension, & $quality)
{
if ( ! $extension)
{
// Use the current image type
$extension = image_type_to_extension($this->type, FALSE);
}
switch (strtolower($extension))
{
case 'jpg':
case 'jpeg':
// Save a JPG file
$save = 'imagejpeg';
$type = IMAGETYPE_JPEG;
break;
case 'gif':
// Save a GIF file
$save = 'imagegif';
$type = IMAGETYPE_GIF;
// GIFs do not a quality setting
$quality = NULL;
break;
case 'png':
// Save a PNG file
$save = 'imagepng';
$type = IMAGETYPE_PNG;
// Use a compression level of 9 (does not affect quality!)
$quality = 9;
break;
default:
throw new ErrorException(sprintf('Installed GD does not support %s images',$extension));
break;
}
return array($save, $type);
}
/**
* Create an empty image with the given width and height.
*
* @param integer $width image width
* @param integer $height image height
* @return resource
*/
protected function _create($width, $height)
{
// Create an empty image
$image = imagecreatetruecolor($width, $height);
// Do not apply alpha blending
imagealphablending($image, FALSE);
// Save alpha levels
imagesavealpha($image, TRUE);
return $image;
}
} // End Image_GD