-
Notifications
You must be signed in to change notification settings - Fork 40
/
image_moo.php
1419 lines (1224 loc) · 45.3 KB
/
image_moo.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
/*
* Image_Moo library
*
* Written due to image_lib not being so nice when you have to do multiple things to a single image!
*
* @license MIT License -
* @author Matthew Augier, aka Mat-Moo
* @link http://www.dps.uk.com http://www.matmoo.com
* @docu http://todo :)
* @email [email protected]
*
* @file image_moo.php
* @version 1.1.7
* @date 2022 Jun 21
*
* Copyright (c) 2011-2014 Matthew (Mat-Moo.com) Augier
*
* Requires PHP 5+ and GD2!
*
* Example usage
* $this->image_moo->load("file")->resize(64,40)->save("thumb")->resize(640,480)->save("medium");
* if ($this->image_moo->errors) print $this->image_moo->display_errors();
*
* COLOURS!
* Any function that can take a colour as a parameter can take "#RGB", "#RRGGBB" or an array(R,G,B)
*
* image manipulation functions
* -----------------------------------------------------------------------------
* load($x) - Loads an image file specified by $x - JPG, PNG, GIF, WEBP supported
* load_temp() - Takes a cropped/altered image and makes it the main image to work with.
* save($x) - Saved the manipulated image (if applicable) to file $x - JPG, PNG, GIF, WEBP supported
* get_data_stream($filename="") - Return the image as a stream so that it can be sent as source data to the output
* save_pa($prepend="", $append="", $overwrite=FALSE) - Saves using the original image name but with prepend and append text, e.g. load('moo.jpg')->save_pa('pre_','_app') would save as filename pre_moo_app.jpg
* save_dynamic($filename="") - Saves as a stream output, use filename to return png/jpg/gif/webp etc., default is jpeg
* resize($x,$y=FALSE,$pad=FALSE) - Proportionally resize original image using the bounds $x and $y (if y is false x size is used), if padding is set return image is as defined centralised using BG colour
* resize_crop($x,$y) - Proportionally resize original image using the bounds $x and $y but cropped to fill dimensions
* stretch($x,$y) - Take the original image and stretch it to fill new dimensions $x $y
* crop($x1,$y1,$x2,$y2) - Crop the original image using Top left, $x1,$y1 to bottom right $x2,y2. New image size =$x2-x1 x $y2-y1
* rotate($angle) - Rotates the work image by X degrees, normally 90,180,270 can be any angle.Excess filled with background colour
* load_watermark($filename, $transparent_x=0, $transparent_y=0) - Loads the specified file as the watermark file, if using PNG32/24 use x,y to specify direct positions of colour to use as index
* make_watermark_text($text, $fontfile, $size=16, $colour="#ffffff", $angle=0) - Creates a text watermark
* watermark($position, $offset=8, $abs=FALSE) - Use the loaded watermark, or created text to place a watermark. $position works like NUM PAD key layout, e.g. 7=Top left, 3=Bottom right $offset is the padding/indentation, if $abs is true then use $positiona and $offset as direct values to watermark placement
* border($width,$colour="#000") - Draw a border around the output image X pixels wide in colour specified
* border_3d($width,$rot=0,$opacity=30) - Draw a 3d border (opaque) around the current image $width wise in 0-3 rot positions, $opacity allows you to change how much it effects the picture
* filter($function, $arg1=NULL, $arg2=NULL, $arg3=NULL, $arg4=NULL) -Runs the standard imagefilter GD2 command, see http://www.php.net/manual/en/function.imagefilter.php for details
* round($radius,$invert=FALSE,$corners(array[top left, top right, bottom right, bottom left of true or False)="") default is all on and normal rounding
* shadow($size=4, $direction=3, $colour="#444") - Size in pixels, note that the image will increase by this size, so resize(400,400)->shadoe(4) will give an image 404 pixels in size, Direction works on teh keypad basis like the watermark, so 3 is bottom right, $color if the colour of the shadow.
* -----------------------------------------------------------------------------
* image helper functions
* ignore_jpeg_warnings - Sets the gd.jpeg_ignore_warning to help load images that may otherwise not work
* allow_scale_up($onoff = FALSE) - When using resize, setting this to tru will allow small images to increase in size, otherwise they do not get resized
* real_filesize() - returns the filesize of the image in bytes etc.
* display_errors($open = '<p>', $close = '</p>') - Display errors as Ci standard style
* set_jpeg_quality($x) - quality to write jpeg/webp files in for save, default 75 (1-100)
* set_watermark_transparency($x) - the opacity of the watermark 1-100, 1-just about see, 100=solid
* check_gd() - Run to see if you server can use this library
* clear_temp() - Call to clear the temp changes using the master image again
* clear() - Clears all images in memory
* -----------------------------------------------------------------------------
*
* KNOWN BUGS
* make_watermark_text does not deal with rotation angle correctly, box is cropped
*
* TO DO
*
* THANKS
* Matjaž for poiting out the save_pa bug (should of tested it!)
* Cahva for posting yet another bug in the save_pa (Man I can be silly sometimes!)
* Cole spotting the resize flaw and providing a fix
* Nuno Mira for suggesting the new width/new size on teh ci forums
* HugoSolar for transparent rotate
* EbbenF/Locally.com for webp support
*
*/
class Image_moo
{
// image vars
private $main_image = "";
private $watermark_image;
private $temp_image;
private $jpeg_quality = 75;
private $background_colour = "#ffffff";
private $watermark_method;
private $jpeg_ignore_warnings = FALSE; // set to true or call ignore_jpeg_warnings()
private $can_stretch = FALSE; // when a resizing an image too small, allow it to be stretched larger
// other
private $filename = "";
// watermark stuff, opacity
private $watermark_transparency = 50;
// reported errors
public $errors = FALSE;
private $error_msg = array();
// image info
public $width = 0;
public $height = 0;
public $new_width = 0;
public $new_height = 0;
function __construct()
//----------------------------------------------------------------------------------------------------------
// create stuff here as needed
//----------------------------------------------------------------------------------------------------------
{
// log_message('debug', "Image Moo Class Initialized");
if ($this->jpeg_ignore_warnings) $this->ignore_jpeg_warnings();
if ($this->can_stretch) $this->can_stretch(TRUE);
}
public function ignore_jpeg_warnings($onoff = TRUE)
//----------------------------------------------------------------------------------------------------------
// having loaded lots of jpegs I quite often get corrupt ones, this setting relaxs GD a bit
// requires 5.1.3 php
//----------------------------------------------------------------------------------------------------------
{
ini_set('gd.jpeg_ignore_warning', $onoff == TRUE);
return $this;
}
public function allow_scale_up($onoff = FALSE)
//----------------------------------------------------------------------------------------------------------
// If you want to stretch or crop images that are smaller than the target size, call this with TRUE to scale
// up
//----------------------------------------------------------------------------------------------------------
{
$this->can_stretch = $onoff;
return $this;
}
private function _clear_errors()
//----------------------------------------------------------------------------------------------------------
// load a resource
//----------------------------------------------------------------------------------------------------------
{
$this->error_msg = array();
}
private function set_error($msg)
//----------------------------------------------------------------------------------------------------------
// Set an error message
//----------------------------------------------------------------------------------------------------------
{
$this->errors = TRUE;
$this->error_msg[] = $msg;
}
public function display_errors($open = '<p>', $close = '</p>')
//----------------------------------------------------------------------------------------------------------
// returns the errors formatted as needed, same as CI doed
//----------------------------------------------------------------------------------------------------------
{
$str = '';
foreach ($this->error_msg as $val)
{
$str .= $open.$val.$close;
}
return $str;
}
public function check_gd()
//----------------------------------------------------------------------------------------------------------
// verification util to see if you can use image_moo
//----------------------------------------------------------------------------------------------------------
{
// is gd loaded?
if ( ! extension_loaded('gd'))
{
if ( ! dl('gd.so'))
{
$this->set_error('GD library does not appear to be loaded');
return FALSE;
}
}
// check version
if (function_exists('gd_info'))
{
$gdarray = @gd_info();
$versiontxt = preg_replace('/[A-Z,\ ()\[\]]/i', '', $gdarray['GD Version']);
$versionparts=explode('.',$versiontxt);
// looking for a version 2
if ($versionparts[0]=="2")
{
return TRUE;
}
else
{
$this->set_error('Requires GD2, this reported as '.$versiontxt);
return FALSE;
}
}
else
{
// should this be a warning?
$this->set_error('Could not verify GD version');
return FALSE;
}
}
//----------------------------------------------------------------------------------------------------------
// checks that we have an GD image loaded
//---
private function is_gd_image( $image ) {
return ( (is_resource($image) && get_resource_type( $image ) === 'gd') || (is_object( $image ) && $image instanceof GdImage) );
}
private function _check_image()
//----------------------------------------------------------------------------------------------------------
// checks that we have an image loaded
//----------------------------------------------------------------------------------------------------------
{
// generic check
if (!$this->is_gd_image($this->main_image))
{
$this->set_error("No main image loaded!");
return FALSE;
}
else
{
return TRUE;
}
}
function get_data_stream($filename="")
//----------------------------------------------------------------------------------------------------------
// Saves image as a datastream for inline inclustion
// writes as a temp image
// you can not chain this one!
//----------------------------------------------------------------------------------------------------------
{
// validate we loaded a main image
if (!$this->_check_image()) return $this;
// if no operations, copy it for temp save
$this->_copy_to_temp_if_needed();
// ok, lets go!
if ($filename=="") $filename=rand(1000,999999).".jpg"; // send as jpeg
$ext = strtoupper(pathinfo($filename, PATHINFO_EXTENSION));
// start new buffer
ob_start();
switch ($ext)
{
case "GIF" :
imagegif($this->temp_image);
break;
case "JPG" :
case "JPEG" :
imagejpeg($this->temp_image);
break;
case "PNG" :
imagepng($this->temp_image);
break;
case "WEBP":
imagewebp($this->temp_image);
break;
default:
$this->set_error('Extension not recognised! Must be jpg/png/gif');
return FALSE;
break;
}
// get the buffer
$contents = ob_get_contents();
// remove buffer
ob_end_clean();
// return teh buffer (allows user to encode it)
return $contents;
}
function save_dynamic($filename="")
//----------------------------------------------------------------------------------------------------------
// Saves the temp image as a dynamic image
// e.g. direct output to the browser
//----------------------------------------------------------------------------------------------------------
{
// validate we loaded a main image
if (!$this->_check_image()) return $this;
// if no operations, copy it for temp save
$this->_copy_to_temp_if_needed();
// ok, lets go!
if ($filename=="") $filename=rand(1000,999999).".jpg"; // send as jpeg
$ext = strtoupper(pathinfo($filename, PATHINFO_EXTENSION));
header("Content-disposition: filename=$filename;");
header('Content-transfer-Encoding: binary');
header('Last-modified: '.gmdate('D, d M Y H:i:s'));
switch ($ext)
{
case "GIF" :
header("Content-type: image/gif");
imagegif($this->temp_image);
return $this;
break;
case "JPG" :
case "JPEG" :
header("Content-type: image/jpeg");
imagejpeg($this->temp_image, NULL, $this->jpeg_quality);
return $this;
break;
case "PNG" :
header("Content-type: image/png");
imagepng($this->temp_image);
return $this;
break;
case "WEBP":
header("Content-type: image/webp");
imagewebp($this->temp_image, NULL, $this->jpeg_quality);
return $this;
break;
}
$this->set_error('Unable to save, extension not GIF/JPEG/JPG/PNG');
return $this;
}
function save_pa($prepend="", $append="", $overwrite=FALSE)
//----------------------------------------------------------------------------------------------------------
// Saves the temp image as the filename specified,
// overwrite = true of false
//----------------------------------------------------------------------------------------------------------
{
// validate we loaded a main image
if (!$this->_check_image()) return $this;
// get current file parts
$parts=pathinfo($this->filename);
// save
$this->save($parts["dirname"].'/'.$prepend.$parts['filename'].$append.'.'.$parts["extension"], $overwrite);
return $this;
}
function save($filename,$overwrite=FALSE)
//----------------------------------------------------------------------------------------------------------
// Saves the temp image as the filename specified,
// overwrite = true of false
//----------------------------------------------------------------------------------------------------------
{
// validate we loaded a main image
if (!$this->_check_image()) return $this;
// if no operations, copy it for temp save
$this->_copy_to_temp_if_needed();
// check if it already exists
if (!$overwrite)
{
// don't overwrite, so check for file
if (file_exists($filename))
{
$this->set_error('File exists, overwrite is FALSE, could not save over file '.$filename);
return $this;
}
}
// find out the type of file to save
$ext = strtoupper(pathinfo($filename, PATHINFO_EXTENSION));
switch ($ext)
{
case "GIF" :
imagegif($this->temp_image, $filename);
return $this;
break;
case "JPG" :
case "JPEG" :
imagejpeg($this->temp_image, $filename, $this->jpeg_quality);
return $this;
break;
case "PNG" :
imagepng($this->temp_image, $filename);
return $this;
break;
case "WEBP" :
imagewebp($this->temp_image, $filename, $this->jpeg_quality);
return $this;
break;
}
// invalid filetype?!
$this->set_error('Do no know what '.$ext.' filetype is in filename '.$filename);
return $this;
}
private function _load_image($filename)
//----------------------------------------------------------------------------------------------------------
// private function to load a resource
//----------------------------------------------------------------------------------------------------------
{
// check the request file can be located
if (!file_exists($filename))
{
$this->set_error('Could not locate file '.$filename);
return FALSE;
}
// get image info about this file
$image_info=getimagesize($filename);
// load file depending on mimetype
try
{
switch ($image_info["mime"])
{
case "image/gif" :
return @imagecreatefromgif($filename);
break;
case "image/jpeg" :
return @imagecreatefromjpeg($filename);
break;
case "image/png" :
return @imagecreatefrompng($filename);
break;
case "image/webp" :
return @imagecreatefromwebp($filename);
break;
}
}
catch (Exception $e)
{
$this->set_error('Exception loading '.$filename.' - '.$e->getMessage());
}
// invalid filetype?!
$this->set_error('Unable to load '.$filename.' filetype '.$image_info["mime"].'not recognised');
return FALSE;
}
public function load_temp()
//----------------------------------------------------------------------------------------------------------
// Take the temp image and make it the main image
//----------------------------------------------------------------------------------------------------------
{
// validate we loaded a main image
if (!$this->_check_image()) return $this;
if (!$this->is_gd_image($this->temp_image))
{
$this->set_error("No temp image created!");
return FALSE;
}
// make main the temp
$this->main_image = $this->temp_image;
// clear temp
$this->clear_temp();
// reset sizes
$this->_set_new_size();
// return the object
return $this;
}
public function load($filename)
//----------------------------------------------------------------------------------------------------------
// Load an image, public function
//----------------------------------------------------------------------------------------------------------
{
// new image, reset error messages
$this->_clear_errors();
// remove temporary image stored
$this->clear_temp();
// save filename
$this->filename=$filename;
// reset width and height
$this->width = 0;
$this->height = 0;
// load it
$this->main_image = $this->_load_image($filename);
// no error, then get the dminesions set
if ($this->main_image <> FALSE)
{
$this->new_width = $this->width = imageSX($this->main_image);
$this->new_height = $this->height = imageSY($this->main_image);
$this->_set_new_size();
}
// return the object
return $this;
}
public function load_watermark($filename, $transparent_x=NULL, $transparent_y=NULL)
//----------------------------------------------------------------------------------------------------------
// Load an image, public function
//----------------------------------------------------------------------------------------------------------
{
if($this->is_gd_image($this->watermark_image)) imagedestroy($this->watermark_image);
$this->watermark_image = $this->_load_image($filename);
if($this->is_gd_image($this->watermark_image))
{
$this->watermark_method = 1;
if(($transparent_x <> NULL) AND ($transparent_y <> NULL))
{
// get the top left corner colour allocation
$tpcolour = imagecolorat($this->watermark_image, $transparent_x, $transparent_y);
// set this as the transparent colour
imagecolortransparent($this->watermark_image, $tpcolour);
// $set diff method
$this->watermark_method = 2;
}
}
// return this object
return $this;
}
public function real_filesize()
//----------------------------------------------------------------------------------------------------------
// Returns the actual filesize of the original image
//----------------------------------------------------------------------------------------------------------
{
// filename?
if ($this->filename == "")
{
$this->set_error('Unable to get filesize, no filename!');
return "-";
}
if (!file_exists($this->filename))
{
$this->set_error('Unable to get filesize, file does not exist!');
return "-";
}
// set the units (found on filesize.php)
$size = filesize($this->filename);
// set the units
$units = array(' B', ' KB', ' MB', ' GB', ' TB');
for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
// return the closest
return round($size, 2).$units[$i];
}
public function set_watermark_transparency($transparency=50)
//----------------------------------------------------------------------------------------------------------
// Sets the quality that jpeg will be saved at
//----------------------------------------------------------------------------------------------------------
{
$this->watermark_transparency = $transparency;
return $this;
}
public function set_background_colour($colour="#ffffff")
//----------------------------------------------------------------------------------------------------------
// Sets teh background colour to use on rotation and padding for resize
//----------------------------------------------------------------------------------------------------------
{
$this->background_colour = $this->_html2rgb($colour);
return $this;
}
public function set_jpeg_quality($quality=75)
//----------------------------------------------------------------------------------------------------------
// Sets the quality that jpeg will be saved at
//----------------------------------------------------------------------------------------------------------
{
$this->jpeg_quality = $quality;
return $this;
}
private function _copy_to_temp_if_needed()
//----------------------------------------------------------------------------------------------------------
// If temp image is empty, e.g. not resized or done anything then just copy main image
//----------------------------------------------------------------------------------------------------------
{
if (!$this->is_gd_image($this->temp_image))
{
// create a temp based on new dimensions
$this->temp_image = imagecreatetruecolor($this->width, $this->height);
// check it
if(!$this->is_gd_image($this->temp_image))
{
$this->set_error('Unable to create temp image sized '.$this->width.' x '.$this->height);
return FALSE;
}
// copy image to temp workspace
imagecopy($this->temp_image, $this->main_image, 0, 0, 0, 0, $this->width, $this->height);
$this->_set_new_size();
}
}
public function clear()
//----------------------------------------------------------------------------------------------------------
// clear everything!
//----------------------------------------------------------------------------------------------------------
{
if($this->is_gd_image($this->main_image)) imagedestroy($this->main_image);
if($this->is_gd_image($this->watermark_image)) imagedestroy($this->watermark_image);
if($this->is_gd_image($this->temp_image)) imagedestroy($this->temp_image);
return $this;
}
public function clear_temp()
//----------------------------------------------------------------------------------------------------------
// you may want to revert back to teh original image to work on, e.g. watermark, this clears temp
//----------------------------------------------------------------------------------------------------------
{
if($this->is_gd_image($this->temp_image)) imagedestroy($this->temp_image);
return $this;
}
public function resize_crop($mw,$mh)
//----------------------------------------------------------------------------------------------------------
// take main image and resize to tempimage using EXACT boundaries mw,mh (max width and max height)
// this is proportional and crops the image centrally to fit
//----------------------------------------------------------------------------------------------------------
{
if (!$this->_check_image()) return $this;
// clear temp image
$this->clear_temp();
if( $this->width > $mw || $this->height > $mh || $this->can_stretch)
// create a temp based on new dimensions
$this->temp_image = imagecreatetruecolor($mw, $mh);
else
return $this;
// check it
if(!$this->is_gd_image($this->temp_image))
{
$this->set_error('Unable to create temp image sized '.$mw.' x '.$mh);
return $this;
}
// work out best positions for copy
$wx=$this->width / $mw;
$wy=$this->height / $mh;
if ($wx >= $wy)
{
// use full height
$sy = 0;
$sy2 = $this->height;
// calcs
$calc_width = $mw * $wy;
$sx = ($this->width - $calc_width) / 2;
$sx2 = $calc_width;
}
else
{
// use full width
$sx = 0;
$sx2 = $this->width;
// calcs
$calc_height = $mh * $wx;
$sy = ($this->height - $calc_height) / 2;
$sy2 = $calc_height;
}
//image transparency preserved
imagealphablending( $this->temp_image, false );
imagesavealpha( $this->temp_image, true );
// copy section
imagecopyresampled($this->temp_image, $this->main_image, 0, 0, $sx, $sy, $mw, $mh, $sx2, $sy2);
// set sizes
$this->_set_new_size();
// return self
return $this;
}
public function resize($mw, $mh=FALSE, $pad=FALSE)
//----------------------------------------------------------------------------------------------------------
// take main image and resize to tempimage using boundaries mw,mh (max width or max height)
// this is proportional, pad to true will set it in the middle of area size
//----------------------------------------------------------------------------------------------------------
{
// no image - fail!
if (!$this->_check_image()) return $this;
// set mh if not set
if ($mh == FALSE) $mh = $mw;
// calc new dimensions
if( $this->width > $mw || $this->height > $mh || $this->can_stretch)
{
if( ($this->width / $this->height) > ($mw / $mh) ) {
$tnw = $mw;
$tnh = $tnw * $this->height / $this->width;
} else {
$tnh = $mh;
$tnw = $tnh * $this->width / $this->height;
}
}
else
{
$tnw = $this->width;
$tnh = $this->height;
}
// clear temp image
$this->clear_temp();
// create a temp based on new dimensions
if ($pad)
{
$tx = $mw;
$ty = $mh;
$px = ($mw - $tnw) / 2;
$py = ($mh - $tnh) / 2;
}
else
{
$tx = $tnw;
$ty = $tnh;
$px = 0;
$py = 0;
}
$this->temp_image = imagecreatetruecolor($tx,$ty);
// check it
if(!$this->is_gd_image($this->temp_image))
{
$this->set_error('Unable to create temp image sized '.$tx.' x '.$ty);
return $this;
}
/* hmm what was I doing here?!
imagealphablending($this->main_image, true);
$a = imagecolortransparent($this->temp_image, imagecolorallocatealpha($this->temp_image, 0, 0, 0, 127));
imagefilledrectangle($this->temp_image, 0, 0, $tx, $ty, $a);
imagesavealpha($this->temp_image, true);
*/
$col = $this->_html2rgb($this->background_colour);
$bg = imagecolorallocate($this->temp_image, $col[0], $col[1], $col[2]);
imagefilledrectangle($this->temp_image, 0, 0, $tx, $ty, $bg);
// if padding, fill background
if ($pad)
{
$col = $this->_html2rgb($this->background_colour);
$bg = imagecolorallocate($this->temp_image, $col[0], $col[1], $col[2]);
imagefilledrectangle($this->temp_image, 0, 0, $tx, $ty, $bg);
/* TO DO
imagealphablending($this->temp_image, false);
imagesavealpha($this->temp_image, true);
$color = imagecolorallocatealpha($this->temp_image, 0, 0, 0, 127);
imagefilledrectangle($this->temp_image, 0, 0, $this->width, $this->height, $color);
*/
}
// copy resized
imagecopyresampled($this->temp_image, $this->main_image, $px, $py, 0, 0, $tnw, $tnh, $this->width, $this->height);
// set sizes
$this->_set_new_size();
// return self
return $this;
}
public function stretch($mw,$mh)
//----------------------------------------------------------------------------------------------------------
// take main image and resize to tempimage using boundaries mw,mh (max width or max height)
// does not retain proportions
//----------------------------------------------------------------------------------------------------------
{
if (!$this->_check_image()) return $this;
// clear temp image
$this->clear_temp();
// create a temp based on new dimensions
$this->temp_image = imagecreatetruecolor($mw, $mh);
// check it
if(!$this->is_gd_image($this->temp_image))
{
$this->set_error('Unable to create temp image sized '.$mh.' x '.$mw);
return $this;
}
// copy resized (stethced, proportions not kept);
imagecopyresampled($this->temp_image, $this->main_image, 0, 0, 0, 0, $mw, $mh, $this->width, $this->height);
// set sizes
$this->_set_new_size();
// return self
return $this;
}
public function crop($x1, $y1, $x2, $y2)
//----------------------------------------------------------------------------------------------------------
// crop the main image to temp image using coords
//----------------------------------------------------------------------------------------------------------
{
if (!$this->_check_image()) return $this;
// clear temp image
$this->clear_temp();
// check dimensions
if ($x1 < 0 || $y1 < 0 || $x2 - $x1 > $this->width || $y2 - $y1 > $this->height)
{
$this->set_error('Invalid crop dimensions, either - passed or width/heigh too large '.$x1.'/'.$y1.' x '.$x2.'/'.$y2);
return $this;
}
// create a temp based on new dimensions
$this->temp_image = imagecreatetruecolor($x2-$x1, $y2-$y1);
// check it
if(!$this->is_gd_image($this->temp_image))
{
$this->set_error('Unable to create temp image sized '.($x2-$x1).' x '.($y2-$y1));
return $this;
}
// copy cropped portion
imagecopy($this->temp_image, $this->main_image, 0, 0, $x1, $y1, $x2 - $x1, $y2 - $y1);
// set sizes
$this->_set_new_size();
// return self
return $this;
}
private function _html2rgb($colour)
//----------------------------------------------------------------------------------------------------------
// convert #aa0011 to a php colour array
//----------------------------------------------------------------------------------------------------------
{
if (is_array($colour))
{
if (count($colour)==3) return $colour; // rgb sent as an array so use it
$this->set_error('Colour error, array sent not 3 elements, expected array(r,g,b)');
return false;
}
if ($colour[0] == '#')
$colour = substr($colour, 1);
if (strlen($colour) == 6)
{
list($r, $g, $b) = array($colour[0].$colour[1],
$colour[2].$colour[3],
$colour[4].$colour[5]);
}
elseif (strlen($colour) == 3)
{
list($r, $g, $b) = array($colour[0].$colour[0], $colour[1].$colour[1], $colour[2].$colour[2]);
}
else
{
$this->set_error('Colour error, value sent not #RRGGBB or RRGGBB, and not array(r,g,b)');
return false;
}
$r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
return array($r, $g, $b);
}
public function rotate($angle)
//----------------------------------------------------------------------------------------------------------
// rotate an image bu 0 / 90 / 180 / 270 degrees
//----------------------------------------------------------------------------------------------------------
{
// validate we loaded a main image
if (!$this->_check_image()) return $this;
// if no operations, copy it for temp save
$this->_copy_to_temp_if_needed();
// set the colour
$col = $this->_html2rgb($this->background_colour);
$bg = imagecolorallocatealpha($this->temp_image, 0, 0, 0, 127);
// rotate as needed
$this->temp_image = imagerotate($this->temp_image, $angle, $bg);
imagealphablending($this->temp_image, false);
imagesavealpha($this->temp_image, true);
// set sizes
$this->_set_new_size();
// return self
return $this;
}
public function make_watermark_text($text, $fontfile, $size=16, $colour="#ffffff", $angle=0)
//----------------------------------------------------------------------------------------------------------
// create an image from text that can be applied as a watermark
// text is the text to write, $fontile is a ttf file that will be used $size=font size, $colour is the colour of text
//----------------------------------------------------------------------------------------------------------
{
// check font file can be found
if (!file_exists($fontfile))
{
$this->set_error('Could not locate font file "'.$fontfile.'"');
return $this;
}
// validate we loaded a main image
if (!$this->_check_image())
{
$remove = TRUE;
// no image loaded so make temp image to use
$this->main_image = imagecreatetruecolor(1000,1000);
}
else
{
$remove = FALSE;
}
// work out text dimensions
$bbox = imageftbbox($size, $angle, $fontfile, $text);
$bw = abs($bbox[4] - $bbox[0]) + 1;
$bh = abs($bbox[1] - $bbox[5]) + 1;
$bl = $bbox[1];
// use this to create watermark image
if($this->is_gd_image($this->watermark_image)) imagedestroy($this->watermark_image);
$this->watermark_image = imagecreatetruecolor($bw, $bh);
// set colours
$col = $this->_html2rgb($colour);
$font_col = imagecolorallocate($this->watermark_image, $col[0], $col[1], $col[2]);
$bg_col = imagecolorallocate($this->watermark_image, 127, 128, 126);
// set method to use
$this->watermark_method = 2;
// create bg
imagecolortransparent($this->watermark_image, $bg_col);
imagefilledrectangle($this->watermark_image, 0,0, $bw, $bh, $bg_col);
// write text to watermark
imagefttext($this->watermark_image, $size, $angle, 0, $bh-$bl, $font_col, $fontfile, $text);
if ($remove) imagedestroy($this->main_image);
return $this;
}
public function watermark($position, $offset=8, $abs=FALSE)
//----------------------------------------------------------------------------------------------------------
// add a watermark to the image
// position works like a keypad e.g.
// 7 8 9
// 4 5 6
// 1 2 3
// offset moves image inwards by x pixels
// if abs is set then $position, $offset = direct placement coords
//----------------------------------------------------------------------------------------------------------
{
// validate we loaded a main image
if (!$this->_check_image()) return $this;
// validate we have a watermark
if(!$this->is_gd_image($this->watermark_image))
{
$this->set_error("Can't watermark image, no watermark loaded/created");
return $this;
}
// if no operations, copy it for temp save
$this->_copy_to_temp_if_needed();
// get watermark width
$wm_w = imageSX($this->watermark_image);
$wm_h = imageSY($this->watermark_image);
// get temp widths
$temp_w = imageSX($this->temp_image);
$temp_h = imageSY($this->temp_image);
// check watermark will fit!