-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpi.image.php
1306 lines (1115 loc) · 33.7 KB
/
pi.image.php
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class Plugin_image extends Plugin {
var $meta = array(
'name' => 'Image',
'version' => '0.9.2',
'author' => 'Mubashar Iqbal',
'author_url' => 'http://mubashariqbal.com'
);
public function index() {
$src = $this->fetch_param('src', null, false, false, false);
$dim = $this->fetch_param('dim', null);
$quality = $this->fetch_param('quality', '100');
if ($src == null) {
return '';
} else {
$pos = stripos($src, "/");
if ($pos !== false) {
if ($pos == 0) {
$src = substr($src, 1);
}
}
}
if ($dim == null) {
return '';
}
$arr = $this->generate_style($src, $dim, $quality);
return $arr;
}
private function generate_style($file, $dim, $quality) {
$file_name = basename($file);
$dir = dirname($file);
$new_file = "_cache/".$dir."/".$this->url_safe($dim)."-".$file_name;
if (!file_exists($file)) {
return null;
}
if ( ! file_exists($new_file) || Statamic_helper::is_file_newer($file, $new_file)) {
if ( ! is_dir(dirname($new_file))) {
mkdir(dirname($new_file), 0777, true);
}
$image = new Image($file);
$resize = true;
list($width, $height, $fix, $crop, $vh) = $this->parse_dim($dim);
if ($fix) {
$dimension = Image::NONE;
} else {
$dimension = Image::WIDTH;
$wd = $image->width - $width;
$hd = $image->height - $height;
$pp = ($image->width / $image->height) * $hd;
if ($vh) {
if ($vh == 'v') {
$dimension = Image::HEIGHT;
if (abs($wd) < abs($hd)) {
$crop = true;
}
} elseif ($vh == 'h') {
$dimension = Image::WIDTH;
if ($wd < 0) {
//$resize = false;
} else {
}
}
} else {
if ($crop) {
if ((($wd >= $hd) && ($pp < $wd)) || (($wd < 1) && ($pp < $wd))) {
$dimension = Image::HEIGHT;
}
} else {
if ((($wd <= $hd) && ($pp > $wd)) || (($wd > 1) && ($pp > $wd))) {
$dimension = Image::HEIGHT;
}
}
}
}
if ($resize) {
$image->resize($width, $height, $dimension);
}
if ($crop) {
$image->crop($width, $height, null);
}
$image->save($new_file, $quality);
}
$info = getimagesize($new_file);
$ret = array();
$ret['width'] = $info[0];
$ret['height'] = $info[1];
$ret['url'] = "/".$new_file; #backwards compatibility
$ret['image_url'] = $ret['url'];
return $ret;
}
protected function url_safe($str)
{
$patterns = array();
$patterns[0] = '/#/';
$patterns[1] = '/!/';
$patterns[2] = '/\(/';
$patterns[3] = '/\)/';
$patterns[4] = '/</';
$patterns[5] = '/>/';
$replacements = array();
$replacements[0] = 'F';
$replacements[1] = 'E';
$replacements[2] = 'R1';
$replacements[3] = 'R2';
$replacements[4] = 'A1';
$replacements[5] = 'A2';
$clean = preg_replace($patterns, $replacements, $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '-', $clean);
$clean = strtolower(ltrim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", '-', $clean);
return $clean;
}
protected function parse_dim($dimensions) {
# why not make this easier to use? some more parameters?
$fix = false;
$crop = false;
$vh = false;
switch (substr($dimensions, -1)) {
case '#':
$crop = true;
$dimensions = substr($dimensions, 0, (strlen($dimensions)-1));
break;
case '!':
$fix = true;
$dimensions = substr($dimensions, 0, (strlen($dimensions)-1));
break;
case '<':
$vh = "h";
$dimensions = substr($dimensions, 0, (strlen($dimensions)-1));
break;
case '>':
$vh = "v";
$dimensions = substr($dimensions, 0, (strlen($dimensions)-1));
break;
case '(':
$crop = true;
$vh = "h";
$dimensions = substr($dimensions, 0, (strlen($dimensions)-1));
break;
case ')':
$crop = true;
$vh = "v";
$dimensions = substr($dimensions, 0, (strlen($dimensions)-1));
break;
}
$dim = explode("x", $dimensions);
$width = (isset($dim[0]) && $dim[0] != '') ? $dim[0] : 0;
$height = (isset($dim[1]) && $dim[1] != '') ? $dim[1] : 0;
return array($width, $height, $fix, $crop, $vh);
}
}
/**
* Image manipulation support. Allows images to be resized, cropped, etc.
* Based on Kohana Image Class
* @license http://kohanaphp.com/license
*/
class Image {
// Resizing contraints
const NONE = 0x01;
const WIDTH = 0x02;
const HEIGHT = 0x03;
const AUTO = 0x04;
const INVERSE = 0x05;
// Flipping directions
const HORIZONTAL = 0x11;
const VERTICAL = 0x12;
/**
* @var string image file path
*/
public $file;
/**
* @var integer image width
*/
public $width;
/**
* @var integer image height
*/
public $height;
/**
* @var integer one of the IMAGETYPE_* constants
*/
public $type;
/**
* Loads information about the image. Will throw an exception if the image
* does not exist or is not an image.
*
* @param string image file path
* @return void
* @throws Exception
*/
public function __construct($file)
{
try
{
// Get the real path to the file
$file = realpath($file);
// Get the image information
$info = getimagesize($file);
}
catch (Exception $e)
{
// Ignore all errors while reading the image
}
if (empty($file) OR empty($info))
{
// throw new Exception('Not an image or invalid image: :file',
// array(':file' => Kohana::debug_path($file)));
return '';
}
// Store the image information
$this->file = $file;
$this->width = $info[0];
$this->height = $info[1];
$this->type = $info[2];
$this->mime = image_type_to_mime_type($this->type);
// 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))
{
$format = 'Cannot handle images of type %s for file: %s';
throw new Exception(sprintf($format, image_type_to_extension($this->type), $file));
}
// Save function for future use
$this->_create_function = $create;
// Save filename for lazy loading
$this->_image = $this->file;
}
// Is GD bundled or separate?
protected static $_bundled;
/**
* Checks if GD is enabled and bundled. Bundled GD is required for some
* methods to work. 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 Exception('GD is either not installed or not enabled, check your configuration');
}
if (defined('GD_BUNDLED'))
{
// Get the version via a constant, available in PHP 5.
Image_GD::$_bundled = GD_BUNDLED;
}
else
{
// Get the version information
$info = gd_info();
// Extract the bundled status
Image_GD::$_bundled = (bool) preg_match('/\bbundled\b/i', $info['GD Version']);
}
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 Exception('Image_GD requires GD version :required or greater, you have :version',
// array('required' => '2.0.1', ':version' => $version));
}
return Image_GD::$_checked = TRUE;
}
// Temporary image resource
protected $_image;
// Function name to open Image
protected $_create_function;
/**
* Destroys the loaded image to free up resources.
*
* @return void
*/
public function __destruct()
{
if (is_resource($this->_image))
{
// Free all resources
imagedestroy($this->_image);
}
}
/**
* Resize the image to the given size. Either the width or the height can
* be omitted and the image will be resized proportionally.
*
* // Resize to 200 pixels on the shortest side
* $image->resize(200, 200);
*
* // Resize to 200x200 pixels, keeping aspect ratio
* $image->resize(200, 200, Image::INVERSE);
*
* // Resize to 500 pixel width, keeping aspect ratio
* $image->resize(500, NULL);
*
* // Resize to 500 pixel height, keeping aspect ratio
* $image->resize(NULL, 500);
*
* // Resize to 200x500 pixels, ignoring aspect ratio
* $image->resize(200, 500, Image::NONE);
*
* @param integer new width
* @param integer new height
* @param integer master dimension
* @return $this
* @uses Image::_do_resize
*/
public function resize($width = NULL, $height = NULL, $master = NULL)
{
if ($master === NULL)
{
// Choose the master dimension automatically
$master = Image::AUTO;
}
// Image::WIDTH and Image::HEIGHT depricated. You can use it in old projects,
// but in new you must pass empty value for non-master dimension
elseif ($master == Image::WIDTH AND ! empty($width))
{
$master = Image::AUTO;
// Set empty height for backvard compatibility
$height = NULL;
}
elseif ($master == Image::HEIGHT AND ! empty($height))
{
$master = Image::AUTO;
// Set empty width for backvard compatibility
$width = NULL;
}
if (empty($width))
{
if ($master === Image::NONE)
{
// Use the current width
$width = $this->width;
}
else
{
// If width not set, master will be height
$master = Image::HEIGHT;
}
}
if (empty($height))
{
if ($master === Image::NONE)
{
// Use the current height
$height = $this->height;
}
else
{
// If height not set, master will be width
$master = Image::WIDTH;
}
}
switch ($master)
{
case Image::AUTO:
// Choose direction with the greatest reduction ratio
$master = ($this->width / $width) > ($this->height / $height) ? Image::WIDTH : Image::HEIGHT;
break;
case Image::INVERSE:
// Choose direction with the minimum reduction ratio
$master = ($this->width / $width) > ($this->height / $height) ? Image::HEIGHT : Image::WIDTH;
break;
}
switch ($master)
{
case Image::WIDTH:
// Recalculate the height based on the width proportions
$height = $this->height * $width / $this->width;
break;
case Image::HEIGHT:
// Recalculate the width based on the height proportions
$width = $this->width * $height / $this->height;
break;
}
// Convert the width and height to integers
$width = round($width);
$height = round($height);
$this->_do_resize($width, $height);
return $this;
}
/**
* Crop an image to the given size. Either the width or the height can be
* omitted and the current width or height will be used.
*
* If no offset is specified, the center of the axis will be used.
* If an offset of TRUE is specified, the bottom of the axis will be used.
*
* // Crop the image to 200x200 pixels, from the center
* $image->crop(200, 200);
*
* @param integer new width
* @param integer new height
* @param mixed offset from the left
* @param mixed offset from the top
* @return $this
* @uses Image::_do_crop
*/
public function crop($width, $height, $offset_x = NULL, $offset_y = NULL)
{
if ($width > $this->width)
{
// Use the current width
$width = $this->width;
}
if ($height > $this->height)
{
// Use the current height
$height = $this->height;
}
if ($offset_x === NULL)
{
// Center the X offset
$offset_x = round(($this->width - $width) / 2);
}
elseif ($offset_x === TRUE)
{
// Bottom the X offset
$offset_x = $this->width - $width;
}
elseif ($offset_x < 0)
{
// Set the X offset from the right
$offset_x = $this->width - $width + $offset_x;
}
if ($offset_y === NULL)
{
// Center the Y offset
$offset_y = round(($this->height - $height) / 2);
}
elseif ($offset_y === TRUE)
{
// Bottom the Y offset
$offset_y = $this->height - $height;
}
elseif ($offset_y < 0)
{
// Set the Y offset from the bottom
$offset_y = $this->height - $height + $offset_y;
}
// Determine the maximum possible width and height
$max_width = $this->width - $offset_x;
$max_height = $this->height - $offset_y;
if ($width > $max_width)
{
// Use the maximum available width
$width = $max_width;
}
if ($height > $max_height)
{
// Use the maximum available height
$height = $max_height;
}
$this->_do_crop($width, $height, $offset_x, $offset_y);
return $this;
}
/**
* Rotate the image by a given amount.
*
* // Rotate 45 degrees clockwise
* $image->rotate(45);
*
* // Rotate 90% counter-clockwise
* $image->rotate(-90);
*
* @param integer degrees to rotate: -360-360
* @return $this
* @uses Image::_do_rotate
*/
public function rotate($degrees)
{
// Make the degrees an integer
$degrees = (int) $degrees;
if ($degrees > 180)
{
do
{
// Keep subtracting full circles until the degrees have normalized
$degrees -= 360;
}
while($degrees > 180);
}
if ($degrees < -180)
{
do
{
// Keep adding full circles until the degrees have normalized
$degrees += 360;
}
while($degrees < -180);
}
$this->_do_rotate($degrees);
return $this;
}
/**
* Flip the image along the horizontal or vertical axis.
*
* // Flip the image from top to bottom
* $image->flip(Image::HORIZONTAL);
*
* // Flip the image from left to right
* $image->flip(Image::VERTICAL);
*
* @param integer direction: Image::HORIZONTAL, Image::VERTICAL
* @return $this
* @uses Image::_do_flip
*/
public function flip($direction)
{
if ($direction !== Image::HORIZONTAL)
{
// Flip vertically
$direction = Image::VERTICAL;
}
$this->_do_flip($direction);
return $this;
}
/**
* Sharpen the image by a given amount.
*
* // Sharpen the image by 20%
* $image->sharpen(20);
*
* @param integer amount to sharpen: 1-100
* @return $this
* @uses Image::_do_sharpen
*/
public function sharpen($amount)
{
// The amount must be in the range of 1 to 100
$amount = min(max($amount, 1), 100);
$this->_do_sharpen($amount);
return $this;
}
/**
* Add a reflection to an image. The most opaque part of the reflection
* will be equal to the opacity setting and fade out to full transparent.
* Alpha transparency is preserved.
*
* // Create a 50 pixel reflection that fades from 0-100% opacity
* $image->reflection(50);
*
* // Create a 50 pixel reflection that fades from 100-0% opacity
* $image->reflection(50, 100, TRUE);
*
* // Create a 50 pixel reflection that fades from 0-60% opacity
* $image->reflection(50, 60, TRUE);
*
* [!!] By default, the reflection will be go from transparent at the top
* to opaque at the bottom.
*
* @param integer reflection height
* @param integer reflection opacity: 0-100
* @param boolean TRUE to fade in, FALSE to fade out
* @return $this
* @uses Image::_do_reflection
*/
public function reflection($height = NULL, $opacity = 100, $fade_in = FALSE)
{
if ($height === NULL OR $height > $this->height)
{
// Use the current height
$height = $this->height;
}
// The opacity must be in the range of 0 to 100
$opacity = min(max($opacity, 0), 100);
$this->_do_reflection($height, $opacity, $fade_in);
return $this;
}
/**
* Add a watermark to an image with a specified opacity. Alpha transparency
* will be preserved.
*
* If no offset is specified, the center of the axis will be used.
* If an offset of TRUE is specified, the bottom of the axis will be used.
*
* // Add a watermark to the bottom right of the image
* $mark = Image::factory('upload/watermark.png');
* $image->watermark($mark, TRUE, TRUE);
*
* @param object watermark Image instance
* @param integer offset from the left
* @param integer offset from the top
* @param integer opacity of watermark: 1-100
* @return $this
* @uses Image::_do_watermark
*/
public function watermark(Image $watermark, $offset_x = NULL, $offset_y = NULL, $opacity = 100)
{
if ($offset_x === NULL)
{
// Center the X offset
$offset_x = round(($this->width - $watermark->width) / 2);
}
elseif ($offset_x === TRUE)
{
// Bottom the X offset
$offset_x = $this->width - $watermark->width;
}
elseif ($offset_x < 0)
{
// Set the X offset from the right
$offset_x = $this->width - $watermark->width + $offset_x;
}
if ($offset_y === NULL)
{
// Center the Y offset
$offset_y = round(($this->height - $watermark->height) / 2);
}
elseif ($offset_y === TRUE)
{
// Bottom the Y offset
$offset_y = $this->height - $watermark->height;
}
elseif ($offset_y < 0)
{
// Set the Y offset from the bottom
$offset_y = $this->height - $watermark->height + $offset_y;
}
// The opacity must be in the range of 1 to 100
$opacity = min(max($opacity, 1), 100);
$this->_do_watermark($watermark, $offset_x, $offset_y, $opacity);
return $this;
}
/**
* Set the background color of an image. This is only useful for images
* with alpha transparency.
*
* // Make the image background black
* $image->background('#000');
*
* // Make the image background black with 50% opacity
* $image->background('#000', 50);
*
* @param string hexadecimal color value
* @param integer background opacity: 0-100
* @return $this
* @uses Image::_do_background
*/
public function background($color, $opacity = 100)
{
if ($color[0] === '#')
{
// Remove the pound
$color = substr($color, 1);
}
if (strlen($color) === 3)
{
// Convert shorthand into longhand hex notation
$color = preg_replace('/./', '$0$0', $color);
}
// Convert the hex into RGB values
list ($r, $g, $b) = array_map('hexdec', str_split($color, 2));
// The opacity must be in the range of 0 to 100
$opacity = min(max($opacity, 0), 100);
$this->_do_background($r, $g, $b, $opacity);
return $this;
}
/**
* Save the image. If the filename is omitted, the original image will
* be overwritten.
*
* // Save the image as a PNG
* $image->save('saved/cool.png');
*
* // Overwrite the original image
* $image->save();
*
* [!!] If the file exists, but is not writable, an exception will be thrown.
*
* [!!] If the file does not exist, and the directory is not writable, an
* exception will be thrown.
*
* @param string new image path
* @param integer quality of image: 1-100
* @return boolean
* @uses Image::_save
* @throws Exception
*/
public function save($file = NULL, $quality = 100)
{
if ($file === NULL)
{
// Overwrite the file
$file = $this->file;
}
if (is_file($file))
{
if ( ! is_writable($file))
{
throw new Exception('File must be writable');
}
}
else
{
// Get the directory of the file
$directory = realpath(pathinfo($file, PATHINFO_DIRNAME));
if ( ! is_dir($directory) OR ! is_writable($directory))
{
throw new Exception('Directory must be writable');
}
}
// The quality must be in the range of 1 to 100
$quality = min(max($quality, 1), 100);
return $this->_do_save($file, $quality);
}
/**
* Render the image and return the binary string.
*
* // Render the image at 50% quality
* $data = $image->render(NULL, 50);
*
* // Render the image as a PNG
* $data = $image->render('png');
*
* @param string image type to return: png, jpg, gif, etc
* @param integer quality of image: 1-100
* @return string
* @uses Image::_do_render
*/
public function render($type = NULL, $quality = 100)
{
if ($type === NULL)
{
// Use the current image type
$type = image_type_to_extension($this->type, FALSE);
}
return $this->_do_render($type, $quality);
}
/**
* 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);
}
}
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);
}
}
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);
}
}
protected function _do_rotate($degrees)
{
if ( ! Image_GD::$_bundled)
{
throw new Exception('This method requires :function, which is only available in the bundled version of GD',
array(':function' => 'imagerotate'));
}
// 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;
}
}
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