forked from Oberto/php-image-magician
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_image_magician.php
executable file
·3294 lines (2683 loc) · 102 KB
/
php_image_magician.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
# ========================================================================#
#
# This work is licensed under the Creative Commons Attribution 3.0 Unported
# License. To view a copy of this license,
# visit http://creativecommons.org/licenses/by/3.0/ or send a letter to
# Creative Commons, 444 Castro Street, Suite 900, Mountain View, California,
# 94041, USA.
#
# All rights reserved.
#
# Author: Jarrod Oberto
# Version: 1.5.1
# Date: 10-05-11
# Purpose: Provide tools for image manipulation using GD
# Param In: See functions.
# Param Out: Produces a resized image
# Requires : Requires PHP GD library.
# Usage Example:
# include("lib/php_image_magician.php");
# $magicianObj = new resize('images/car.jpg');
# $magicianObj -> resizeImage(150, 100, 0);
# $magicianObj -> saveImage('images/car_small.jpg', 100);
#
# - See end of doc for more examples -
#
# Supported file types include: jpg, png, gif, bmp, psd (read)
#
#
#
# The following functions are taken from phpThumb() [available from
# http://phpthumb.sourceforge.net], and are used with written permission
# from James Heinrich.
# - GD2BMPstring
# - GetPixelColor
# - LittleEndian2String
#
# The following functions are from Marc Hibbins and are used with written
# permission (are also under the Attribution-ShareAlike
# [http://creativecommons.org/licenses/by-sa/3.0/] license.
# -
#
# PhpPsdReader is used with written permission from Tim de Koning.
# [http://www.kingsquare.nl/phppsdreader]
#
#
#
# Modificatoin history
# Date Initials Ver Description
# 10-05-11 J.C.O 0.0 Initial build
# 01-06-11 J.C.O 0.1.1 * Added reflections
# * Added Rounded corners
# * You can now use PNG interlacing
# * Added shadow
# * Added caption box
# * Added vintage filter
# * Added dynamic image resizing (resize on the fly)
# * minor bug fixes
# 05-06-11 J.C.O 0.1.1.1 * Fixed undefined variables
# 17-06-11 J.C.O 0.1.2 * Added image_batch_class.php class
# * Minor bug fixes
# 26-07-11 J.C.O 0.1.4 * Added support for external images
# * Can now set the crop poisition
# 03-08-11 J.C.O 0.1.5 * Added reset() method to reset resource to
# original input file.
# * Added method addTextToCaptionBox() to
# simplify adding text to a caption box.
# * Added experimental writeIPTC. (not finished)
# * Added experimental readIPTC. (not finished)
# 11-08-11 J.C.O * Added initial border presets.
# 30-08-11 J.C.O * Added 'auto' crop option to crop portrait
# images near the top.
# 08-09-11 J.C.O * Added cropImage() method to allow standalone
# cropping.
# 17-09-11 J.C.O * Added setCropFromTop() set method - set the
# percentage to crop from the top when using
# crop 'auto' option.
# * Added setTransparency() set method - allows you
# to turn transparency off (like when saving
# as a jpg).
# * Added setFillColor() set method - set the
# background color to use instead of transparency.
# 05-11-11 J.C.O 0.1.5.1 * Fixed interlacing option
# 0-07-12 J.C.O 1.0
#
# Known issues & Limitations:
# -------------------------------
# Not so much an issue, the image is destroyed on the deconstruct rather than
# when we have finished with it. The reason for this is that we don't know
# when we're finished with it as you can both save the image and display
# it directly to the screen (imagedestroy($this->imageResized))
#
# Opening BMP files is slow. A test with 884 bmp files processed in a loop
# takes forever - over 5 min. This test inlcuded opening the file, then
# getting and displaying its width and height.
#
# $forceStretch:
# -------------------------------
# On by default.
# $forceStretch can be disabled by calling method setForceStretch with false
# parameter. If disabled, if an images original size is smaller than the size
# specified by the user, the original size will be used. This is useful when
# dealing with small images.
#
# If enabled, images smaller than the size specified will be stretched to
# that size.
#
# Tips:
# -------------------------------
# * If you're resizing a transparent png and saving it as a jpg, set
# $keepTransparency to false with: $magicianObj->setTransparency(false);
#
# FEATURES:
# * EASY TO USE
# * BMP SUPPORT (read & write)
# * PSD (photoshop) support (read)
# * RESIZE IMAGES
# - Preserve transparency (png, gif)
# - Apply sharpening (jpg) (requires PHP >= 5.1.0)
# - Set image quality (jpg, png)
# - Resize modes:
# - exact size
# - resize by width (auto height)
# - resize by height (auto width)
# - auto (automatically determine the best of the above modes to use)
# - crop - resize as best as it can then crop the rest
# - Force stretching of smaller images (upscale)
# * APPLY FILTERS
# - Convert to grey scale
# - Convert to black and white
# - Convert to sepia
# - Convert to negative
# * ROTATE IMAGES
# - Rotate using predefined "left", "right", or "180"; or any custom degree amount
# * EXTRACT EXIF DATA (requires exif module)
# - make
# - model
# - date
# - exposure
# - aperture
# - f-stop
# - iso
# - focal length
# - exposure program
# - metering mode
# - flash status
# - creator
# - copyright
# * ADD WATERMARK
# - Specify exact x, y placement
# - Or, specify using one of the 9 pre-defined placements such as "tl"
# (for top left), "m" (for middle), "br" (for bottom right)
# - also specify padding from edge amount (optional).
# - Set opacity of watermark (png).
# * ADD BORDER
# * USE HEX WHEN SPECIFYING COLORS (eg: #ffffff)
# * SAVE IMAGE OR OUTPUT TO SCREEN
#
#
# ========================================================================#
class imageLib
{
private $fileName;
private $image;
protected $imageResized;
private $widthOriginal; # Always be the original width
private $heightOriginal;
private $width; # Current width (width after resize)
private $height;
private $imageSize;
private $fileExtension;
private $debug = true;
private $errorArray = array();
private $forceStretch = true;
private $aggresiveSharpening = false;
private $transparentArray = array('.png', '.gif');
private $keepTransparency = true;
private $fillColorArray = array('r'=>255, 'g'=>255, 'b'=>255);
private $sharpenArray = array('jpg');
private $psdReaderPath;
private $filterOverlayPath;
private $isInterlace;
private $captionBoxPositionArray = array();
private $fontDir = 'fonts';
private $cropFromTopPercent = 10;
## --------------------------------------------------------
function __construct($fileName)
# Author: Jarrod Oberto
# Date: 27-02-08
# Purpose: Constructor
# Param in: $fileName: File name and path.
# Param out: n/a
# Reference:
# Notes:
#
{
if (!$this->testGDInstalled()) { if ($this->debug) { die('The GD Library is not installed.'); }else{ die(); }};
$this->initialise();
// *** Save the image file name. Only store this incase you want to display it
$this->fileName = $fileName;
$this->fileExtension = strtolower(strrchr($fileName, '.'));
// *** Open up the file
$this->image = $this->openImage($fileName);
// *** Assign here so we don't modify the original
$this->imageResized = $this->image;
// *** If file is an image
if ($this->testIsImage($this->image))
{
// *** Get width and height
$this->width = imagesx($this->image);
$this->widthOriginal = imagesx($this->image);
$this->height = imagesy($this->image);
$this->heightOriginal = imagesy($this->image);
/* Added 15-09-08
* Get the filesize using this build in method.
* Stores an array of size
*
* $this->imageSize[1] = width
* $this->imageSize[2] = height
* $this->imageSize[3] = width x height
*
*/
$this->imageSize = getimagesize($this->fileName);
} else {
$this->errorArray[] = 'File is not an image';
}
}
## --------------------------------------------------------
private function initialise () {
$this->psdReaderPath = dirname(__FILE__) . '/classPhpPsdReader.php';
$this->filterOverlayPath = dirname(__FILE__) . '/filters';
// *** Set if image should be interlaced or not.
$this->isInterlace = false;
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*-
Resize
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/
public function resizeImage($newWidth, $newHeight, $option = 0, $sharpen = false)
# Author: Jarrod Oberto
# Date: 27-02-08
# Purpose: Resizes the image
# Param in: $newWidth:
# $newHeight:
# $option: 0 / exact = defined size;
# 1 / portrait = keep aspect set height;
# 2 / landscape = keep aspect set width;
# 3 / auto = auto;
# 4 / crop= resize and crop;
#
# $option can also be an array containing options for
# cropping. E.G., array('crop', 'r')
#
# This array only applies to 'crop' and the 'r' refers to
# "crop right". Other value include; tl, t, tr, l, m (default),
# r, bl, b, br, or you can specify your own co-ords (which
# isn't recommended.
#
# $sharpen: true: sharpen (jpg only);
# false: don't sharpen
# Param out: n/a
# Reference:
# Notes: To clarify the $option input:
# 0 = The exact height and width dimensions you set.
# 1 = Whatever height is passed in will be the height that
# is set. The width will be calculated and set automatically
# to a the value that keeps the original aspect ratio.
# 2 = The same but based on the width. We try make the image the
# biggest size we can while stil fitting inside the box size
# 3 = Depending whether the image is landscape or portrait, this
# will automatically determine whether to resize via
# dimension 1,2 or 0
# 4 = Will resize and then crop the image for best fit
#
# forceStretch can be applied to options 1,2,3 and 4
#
{
// *** We can pass in an array of options to change the crop position
$cropPos = 'm';
if (is_array($option) && strtolower($option[0]) == 'crop') {
$cropPos = $option[1]; # get the crop option
} else if (strpos($option, '-') !== false) {
// *** Or pass in a hyphen seperated option
$optionPiecesArray = explode('-', $option);
$cropPos = end($optionPiecesArray);
}
// *** Check the option is valid
$option = $this->prepOption($option);
// *** Make sure the file passed in is valid
if (!$this->image) { if ($this->debug) { die('file ' . $this->getFileName() .' is missing or invalid'); }else{ die(); }};
// *** Get optimal width and height - based on $option
$dimensionsArray = $this->getDimensions($newWidth, $newHeight, $option);
$optimalWidth = $dimensionsArray['optimalWidth'];
$optimalHeight = $dimensionsArray['optimalHeight'];
// *** Resample - create image canvas of x, y size
$this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
$this->keepTransparancy($optimalWidth, $optimalHeight, $this->imageResized);
imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);
// *** If '4', then crop too
if ($option == 4 || $option == 'crop') {
if (($optimalWidth >= $newWidth && $optimalHeight >= $newHeight)) {
$this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight, $cropPos);
}
}
// *** Sharpen image (if jpg and the user wishes to do so)
if ($sharpen && in_array($this->fileExtension, $this->sharpenArray)) {
// *** Sharpen
$this->sharpen();
}
}
## --------------------------------------------------------
public function cropImage($newWidth, $newHeight, $cropPos = 'm')
# Author: Jarrod Oberto
# Date: 08-09-11
# Purpose: Crops the image
# Param in: $newWidth: crop with
# $newHeight: crop height
# $cropPos: Can be any of the following:
# tl, t, tr, l, m, r, bl, b, br, auto
# Or:
# a custom position such as '30x50'
# Param out: n/a
# Reference:
# Notes:
#
{
// *** Make sure the file passed in is valid
if (!$this->image) { if ($this->debug) { die('file ' . $this->getFileName() .' is missing or invalid'); }else{ die(); }};
$this->imageResized = $this->image;
$this->crop($this->width, $this->height, $newWidth, $newHeight, $cropPos);
}
## --------------------------------------------------------
private function keepTransparancy($width, $height, $im)
# Author: Jarrod Oberto
# Date: 08-04-11
# Purpose: Keep transparency for png and gif image
# Param in:
# Param out: n/a
# Reference:
# Notes:
#
{
// *** If PNG, perform some transparency retention actions (gif untested)
if (in_array($this->fileExtension, $this->transparentArray) && $this->keepTransparency) {
imagealphablending($im, false);
imagesavealpha($im, true);
$transparent = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefilledrectangle($im, 0, 0, $width, $height, $transparent);
} else {
$color = imagecolorallocate($im, $this->fillColorArray['r'], $this->fillColorArray['g'], $this->fillColorArray['b']);
imagefilledrectangle($im, 0, 0, $width, $height, $color);
}
}
## --------------------------------------------------------
private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight, $cropPos)
# Author: Jarrod Oberto
# Date: 15-09-08
# Purpose: Crops the image
# Param in: $newWidth:
# $newHeight:
# Param out: n/a
# Reference:
# Notes:
#
{
// *** Get cropping co-ordinates
$cropArray = $this->getCropPlacing($optimalWidth, $optimalHeight, $newWidth, $newHeight, $cropPos);
$cropStartX = $cropArray['x'];
$cropStartY = $cropArray['y'];
// *** Crop this bad boy
$crop = imagecreatetruecolor($newWidth , $newHeight);
$this->keepTransparancy($optimalWidth, $optimalHeight, $crop);
imagecopyresampled($crop, $this->imageResized, 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight);
$this->imageResized = $crop;
// *** Set new width and height to our variables
$this->width = $newWidth;
$this->height = $newHeight;
}
## --------------------------------------------------------
private function getCropPlacing($optimalWidth, $optimalHeight, $newWidth, $newHeight, $pos='m')
#
# Author: Jarrod Oberto
# Date: July 11
# Purpose: Set the cropping area.
# Params in:
# Params out: (array) the crop x and y co-ordinates.
# Notes: When specifying the exact pixel crop position (eg 10x15), be
# very careful as it's easy to crop out of the image leaving
# black borders.
#
{
$pos = strtolower($pos);
// *** If co-ords have been entered
if (strstr($pos, 'x')) {
$pos = str_replace(' ', '', $pos);
$xyArray = explode('x', $pos);
list($cropStartX, $cropStartY) = $xyArray;
} else {
switch ($pos) {
case 'tl':
$cropStartX = 0;
$cropStartY = 0;
break;
case 't':
$cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
$cropStartY = 0;
break;
case 'tr':
$cropStartX = $optimalWidth - $newWidth;
$cropStartY = 0;
break;
case 'l':
$cropStartX = 0;
$cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );
break;
case 'm':
$cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
$cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );
break;
case 'r':
$cropStartX = $optimalWidth - $newWidth;
$cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );
break;
case 'bl':
$cropStartX = 0;
$cropStartY = $optimalHeight - $newHeight;
break;
case 'b':
$cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
$cropStartY = $optimalHeight - $newHeight;
break;
case 'br':
$cropStartX = $optimalWidth - $newWidth;
$cropStartY = $optimalHeight - $newHeight;
break;
case 'auto':
// *** If image is a portrait crop from top, not center. v1.5
if ($optimalHeight > $optimalWidth) {
$cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
$cropStartY = ($this->cropFromTopPercent /100) * $optimalHeight;
} else {
// *** Else crop from the center
$cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
$cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );
}
break;
default:
// *** Default to center
$cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
$cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );
break;
}
}
return array('x' => $cropStartX, 'y' => $cropStartY);
}
## --------------------------------------------------------
private function getDimensions($newWidth, $newHeight, $option)
# Author: Jarrod Oberto
# Date: 17-11-09
# Purpose: Get new image dimensions based on user specificaions
# Param in: $newWidth:
# $newHeight:
# Param out: Array of new width and height values
# Reference:
# Notes: If $option = 3 then this function is call recursivly
#
# To clarify the $option input:
# 0 = The exact height and width dimensions you set.
# 1 = Whatever height is passed in will be the height that
# is set. The width will be calculated and set automatically
# to a the value that keeps the original aspect ratio.
# 2 = The same but based on the width.
# 3 = Depending whether the image is landscape or portrait, this
# will automatically determine whether to resize via
# dimension 1,2 or 0.
# 4 = Resize the image as much as possible, then crop the
# remainder.
{
switch (strval($option))
{
case '0':
case 'exact':
$optimalWidth = $newWidth;
$optimalHeight= $newHeight;
break;
case '1':
case 'portrait':
$dimensionsArray = $this->getSizeByFixedHeight($newWidth, $newHeight);
$optimalWidth = $dimensionsArray['optimalWidth'];
$optimalHeight = $dimensionsArray['optimalHeight'];
break;
case '2':
case 'landscape':
$dimensionsArray = $this->getSizeByFixedWidth($newWidth, $newHeight);
$optimalWidth = $dimensionsArray['optimalWidth'];
$optimalHeight = $dimensionsArray['optimalHeight'];
break;
case '3':
case 'auto':
$dimensionsArray = $this->getSizeByAuto($newWidth, $newHeight);
$optimalWidth = $dimensionsArray['optimalWidth'];
$optimalHeight = $dimensionsArray['optimalHeight'];
break;
case '4':
case 'crop':
$dimensionsArray = $this->getOptimalCrop($newWidth, $newHeight);
$optimalWidth = $dimensionsArray['optimalWidth'];
$optimalHeight = $dimensionsArray['optimalHeight'];
break;
}
return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}
## --------------------------------------------------------
private function getSizeByFixedHeight($newWidth, $newHeight)
{
// *** If forcing is off...
if (!$this->forceStretch) {
// *** ...check if actual height is less than target height
if ($this->height < $newHeight) {
return array('optimalWidth' => $this->width, 'optimalHeight' => $this->height);
}
}
$ratio = $this->width / $this->height;
$newWidth = $newHeight * $ratio;
//return $newWidth;
return array('optimalWidth' => $newWidth, 'optimalHeight' => $newHeight);
}
## --------------------------------------------------------
private function getSizeByFixedWidth($newWidth, $newHeight)
{
// *** If forcing is off...
if (!$this->forceStretch) {
// *** ...check if actual width is less than target width
if ($this->width < $newWidth) {
return array('optimalWidth' => $this->width, 'optimalHeight' => $this->height);
}
}
$ratio = $this->height / $this->width;
$newHeight = $newWidth * $ratio;
//return $newHeight;
return array('optimalWidth' => $newWidth, 'optimalHeight' => $newHeight);
}
## --------------------------------------------------------
private function getSizeByAuto($newWidth, $newHeight)
# Author: Jarrod Oberto
# Date: 19-08-08
# Purpose: Depending on the height, choose to resize by 0, 1, or 2
# Param in: The new height and new width
# Notes:
#
{
// *** If forcing is off...
if (!$this->forceStretch) {
// *** ...check if actual size is less than target size
if ($this->width < $newWidth && $this->height < $newHeight) {
return array('optimalWidth' => $this->width, 'optimalHeight' => $this->height);
}
}
if ($this->height < $this->width)
// *** Image to be resized is wider (landscape)
{
//$optimalWidth = $newWidth;
//$optimalHeight= $this->getSizeByFixedWidth($newWidth);
$dimensionsArray = $this->getSizeByFixedWidth($newWidth, $newHeight);
$optimalWidth = $dimensionsArray['optimalWidth'];
$optimalHeight = $dimensionsArray['optimalHeight'];
}
elseif ($this->height > $this->width)
// *** Image to be resized is taller (portrait)
{
//$optimalWidth = $this->getSizeByFixedHeight($newHeight);
//$optimalHeight= $newHeight;
$dimensionsArray = $this->getSizeByFixedHeight($newWidth, $newHeight);
$optimalWidth = $dimensionsArray['optimalWidth'];
$optimalHeight = $dimensionsArray['optimalHeight'];
}
else
// *** Image to be resizerd is a square
{
if ($newHeight < $newWidth) {
//$optimalWidth = $newWidth;
//$optimalHeight= $this->getSizeByFixedWidth($newWidth);
$dimensionsArray = $this->getSizeByFixedWidth($newWidth, $newHeight);
$optimalWidth = $dimensionsArray['optimalWidth'];
$optimalHeight = $dimensionsArray['optimalHeight'];
} else if ($newHeight > $newWidth) {
//$optimalWidth = $this->getSizeByFixedHeight($newHeight);
//$optimalHeight= $newHeight;
$dimensionsArray = $this->getSizeByFixedHeight($newWidth, $newHeight);
$optimalWidth = $dimensionsArray['optimalWidth'];
$optimalHeight = $dimensionsArray['optimalHeight'];
} else {
// *** Sqaure being resized to a square
$optimalWidth = $newWidth;
$optimalHeight= $newHeight;
}
}
return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}
## --------------------------------------------------------
private function getOptimalCrop($newWidth, $newHeight)
# Author: Jarrod Oberto
# Date: 17-11-09
# Purpose: Get optimal crop dimensions
# Param in: width and height as requested by user (fig 3)
# Param out: Array of optimal width and height (fig 2)
# Reference:
# Notes: The optimal width and height return are not the same as the
# same as the width and height passed in. For example:
#
#
# |-----------------| |------------| |-------|
# | | => |**| |**| => | |
# | | |**| |**| | |
# | | |------------| |-------|
# |-----------------|
# original optimal crop
# size size size
# Fig 1 2 3
#
# 300 x 250 150 x 125 150 x 100
#
# The optimal size is the smallest size (that is closest to the crop size)
# while retaining proportion/ratio.
#
# The crop size is the optimal size that has been cropped on one axis to
# make the image the exact size specified by the user.
#
# * represent cropped area
#
{
// *** If forcing is off...
if (!$this->forceStretch) {
// *** ...check if actual size is less than target size
if ($this->width < $newWidth && $this->height < $newHeight) {
return array('optimalWidth' => $this->width, 'optimalHeight' => $this->height);
}
}
$heightRatio = $this->height / $newHeight;
$widthRatio = $this->width / $newWidth;
if ($heightRatio < $widthRatio) {
$optimalRatio = $heightRatio;
} else {
$optimalRatio = $widthRatio;
}
$optimalHeight = round( $this->height / $optimalRatio );
$optimalWidth = round( $this->width / $optimalRatio );
return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}
## --------------------------------------------------------
private function sharpen()
# Author: Jarrod Oberto
# Date: 08 04 2011
# Purpose: Sharpen image
# Param in: n/a
# Param out: n/a
# Reference:
# Notes:
# Credit: Incorporates Joe Lencioni (August 6, 2008) code
{
if (version_compare(PHP_VERSION, '5.1.0') >= 0) {
// ***
if ($this->aggresiveSharpening) { # A more aggressive sharpening solution
$sharpenMatrix = array( array( -1, -1, -1 ),
array( -1, 16, -1 ),
array( -1, -1, -1 ) );
$divisor = 8;
$offset = 0;
imageconvolution($this->imageResized, $sharpenMatrix, $divisor, $offset);
}
else # More subtle and personally more desirable
{
$sharpness = $this->findSharp($this->widthOriginal, $this->width);
$sharpenMatrix = array(
array(-1, -2, -1),
array(-2, $sharpness + 12, -2), //Lessen the effect of a filter by increasing the value in the center cell
array(-1, -2, -1)
);
$divisor = $sharpness; // adjusts brightness
$offset = 0;
imageconvolution($this->imageResized, $sharpenMatrix, $divisor, $offset);
}
}
else
{
if ($this->debug) { die('Sharpening required PHP 5.1.0 or greater.'); }
}
}
## --------------------------------------------------------
private function sharpen2($level)
{
$sharpenMatrix = array(
array($level, $level, $level),
array($level, (8*$level)+1, $level), //Lessen the effect of a filter by increasing the value in the center cell
array($level, $level, $level)
);
}
## --------------------------------------------------------
private function findSharp($orig, $final)
# Author: Ryan Rud (http://adryrun.com)
# Purpose: Find optimal sharpness
# Param in: n/a
# Param out: n/a
# Reference:
# Notes:
#
{
$final = $final * (750.0 / $orig);
$a = 52;
$b = -0.27810650887573124;
$c = .00047337278106508946;
$result = $a + $b * $final + $c * $final * $final;
return max(round($result), 0);
}
## --------------------------------------------------------
private function prepOption($option)
# Author: Jarrod Oberto
# Purpose: Prep option like change the passed in option to lowercase
# Param in: (str/int) $option: eg. 'exact', 'crop'. 0, 4
# Param out: lowercase string
# Reference:
# Notes:
#
{
if (is_array($option)) {
if (strtolower($option[0]) == 'crop' && count($option) == 2) {
return 'crop';
} else {
die('Crop resize option array is badly formatted.');
}
} else if (strpos($option, 'crop') !== false) {
return 'crop';
}
if (is_string($option)) {
return strtolower($option);
}
return $option;
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*-
Presets
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/
#
# Preset are pre-defined templates you can apply to your image.
#
# These are inteded to be applied to thumbnail images.
#
public function borderPreset($preset)
{
switch ($preset)
{
case 'simple':
$this->addBorder(7, '#fff');
$this->addBorder(6, '#f2f1f0');
$this->addBorder(2, '#fff');
$this->addBorder(1, '#ccc');
break;
default:
break;
}
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*-
Draw border
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/
public function addBorder($thickness = 1, $rgbArray = array(255, 255, 255))
# Author: Jarrod Oberto
# Date: 05-05-11
# Purpose: Add a border to the image
# Param in:
# Param out:
# Reference:
# Notes: This border is added to the INSIDE of the image
#
{
if ($this->imageResized) {
$rgbArray = $this->formatColor($rgbArray);
$r = $rgbArray['r'];
$g = $rgbArray['g'];
$b = $rgbArray['b'];
$x1 = 0;
$y1 = 0;
$x2 = ImageSX($this->imageResized) - 1;
$y2 = ImageSY($this->imageResized) - 1;
$rgbArray = ImageColorAllocate($this->imageResized, $r, $g, $b);
for($i = 0; $i < $thickness; $i++) {
ImageRectangle($this->imageResized, $x1++, $y1++, $x2--, $y2--, $rgbArray);
}
}
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*-
Gray Scale
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/
public function greyScale()
# Author: Jarrod Oberto
# Date: 07-05-2011
# Purpose: Make image greyscale
# Param in: n/a
# Param out:
# Reference:
# Notes:
#
{
if ($this->imageResized) {
imagefilter($this->imageResized, IMG_FILTER_GRAYSCALE);
}
}
## --------------------------------------------------------
public function greyScaleEnhanced()
# Author: Jarrod Oberto
# Date: 07-05-2011
# Purpose: Make image greyscale
# Param in: n/a
# Param out:
# Reference:
# Notes:
#
{
if ($this->imageResized) {
imagefilter($this->imageResized, IMG_FILTER_GRAYSCALE);
imagefilter($this->imageResized, IMG_FILTER_CONTRAST, -15);
imagefilter($this->imageResized, IMG_FILTER_BRIGHTNESS, 2);
$this->sharpen($this->width);
}
}
## --------------------------------------------------------
public function greyScaleDramatic()
# Alias of gd_filter_monopin
{
$this->gd_filter_monopin();
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*-
Black 'n White
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*/
public function blackAndWhite()
# Author: Jarrod Oberto
# Date: 07-05-2011
# Purpose: Make image black and white
# Param in: n/a
# Param out:
# Reference:
# Notes:
#
{
if ($this->imageResized) {
imagefilter($this->imageResized, IMG_FILTER_GRAYSCALE);
imagefilter($this->imageResized, IMG_FILTER_CONTRAST, -1000);
}