@@ -14,6 +14,9 @@ class ImageResize
1414 const CROPLEFT = 4 ;
1515 const CROPRIGHT = 5 ;
1616 const CROPTOPCENTER = 6 ;
17+ const IMG_FLIP_HORIZONTAL = 0 ;
18+ const IMG_FLIP_VERTICAL = 1 ;
19+ const IMG_FLIP_BOTH = 2 ;
1720
1821 public $ quality_jpg = 85 ;
1922 public $ quality_webp = 85 ;
@@ -79,11 +82,12 @@ public function addFilter(callable $filter)
7982 * Apply filters.
8083 *
8184 * @param $image resource an image resource identifier
85+ * @param $filterType filter type and default value is IMG_FILTER_NEGATE
8286 */
83- protected function applyFilter ($ image )
87+ protected function applyFilter ($ image, $ filterType = IMG_FILTER_NEGATE )
8488 {
8589 foreach ($ this ->filters as $ function ) {
86- $ function ($ image );
90+ $ function ($ image, $ filterType );
8791 }
8892 }
8993
@@ -149,7 +153,6 @@ public function __construct($filename)
149153
150154 default :
151155 throw new ImageResizeException ('Unsupported image type ' );
152- break ;
153156 }
154157
155158 if (!$ this ->source_image ) {
@@ -571,7 +574,7 @@ public function crop($width, $height, $allow_enlarge = false, $position = self::
571574 */
572575 public function freecrop ($ width , $ height , $ x = false , $ y = false )
573576 {
574- if ($ x === false or $ y === false ) {
577+ if ($ x === false || $ y === false ) {
575578 return $ this ->crop ($ width , $ height );
576579 }
577580 $ this ->source_x = $ x ;
@@ -658,59 +661,51 @@ protected function getCropPosition($expectedSize, $position = self::CROPCENTER)
658661 }
659662 return $ size ;
660663 }
661- }
662-
663- // imageflip definition for PHP < 5.5
664- if (!function_exists ('imageflip ' )) {
665- define ('IMG_FLIP_HORIZONTAL ' , 0 );
666- define ('IMG_FLIP_VERTICAL ' , 1 );
667- define ('IMG_FLIP_BOTH ' , 2 );
668664
669- function imageflip ($ image , $ mode )
665+ /**
666+ * Flips an image using a given mode if PHP version is lower than 5.5
667+ *
668+ * @param resource $image
669+ * @param integer $mode
670+ * @return integer|null
671+ */
672+ public function imageFlip ($ image , $ mode )
670673 {
671- switch ($ mode ) {
672- case IMG_FLIP_HORIZONTAL : {
673- $ max_x = imagesx ($ image ) - 1 ;
674- $ half_x = $ max_x / 2 ;
675- $ sy = imagesy ($ image );
676- $ temp_image = imageistruecolor ($ image )? imagecreatetruecolor (1 , $ sy ): imagecreate (1 , $ sy );
677- for ($ x = 0 ; $ x < $ half_x ; ++$ x ) {
678- imagecopy ($ temp_image , $ image , 0 , 0 , $ x , 0 , 1 , $ sy );
679- imagecopy ($ image , $ image , $ x , 0 , $ max_x - $ x , 0 , 1 , $ sy );
680- imagecopy ($ image , $ temp_image , $ max_x - $ x , 0 , 0 , 0 , 1 , $ sy );
674+ switch ($ mode ) {
675+ case self ::IMG_FLIP_HORIZONTAL : {
676+ $ max_x = imagesx ($ image ) - 1 ;
677+ $ half_x = $ max_x / 2 ;
678+ $ sy = imagesy ($ image );
679+ $ temp_image = imageistruecolor ($ image )? imagecreatetruecolor (1 , $ sy ): imagecreate (1 , $ sy );
680+ for ($ x = 0 ; $ x < $ half_x ; ++$ x ) {
681+ imagecopy ($ temp_image , $ image , 0 , 0 , $ x , 0 , 1 , $ sy );
682+ imagecopy ($ image , $ image , $ x , 0 , $ max_x - $ x , 0 , 1 , $ sy );
683+ imagecopy ($ image , $ temp_image , $ max_x - $ x , 0 , 0 , 0 , 1 , $ sy );
684+ }
685+ break ;
681686 }
682- break ;
683- }
684- case IMG_FLIP_VERTICAL : {
685- $ sx = imagesx ($ image );
686- $ max_y = imagesy ($ image ) - 1 ;
687- $ half_y = $ max_y / 2 ;
688- $ temp_image = imageistruecolor ($ image )? imagecreatetruecolor ($ sx , 1 ): imagecreate ($ sx , 1 );
689- for ($ y = 0 ; $ y < $ half_y ; ++$ y ) {
690- imagecopy ($ temp_image , $ image , 0 , 0 , 0 , $ y , $ sx , 1 );
691- imagecopy ($ image , $ image , 0 , $ y , 0 , $ max_y - $ y , $ sx , 1 );
692- imagecopy ($ image , $ temp_image , 0 , $ max_y - $ y , 0 , 0 , $ sx , 1 );
693- }
694- break ;
695- }
696- case IMG_FLIP_BOTH : {
697- $ sx = imagesx ($ image );
698- $ sy = imagesy ($ image );
699- $ temp_image = imagerotate ($ image , 180 , 0 );
700- imagecopy ($ image , $ temp_image , 0 , 0 , 0 , 0 , $ sx , $ sy );
701- break ;
702- }
703- default : {
704- return ;
705- }
687+ case self ::IMG_FLIP_VERTICAL : {
688+ $ sx = imagesx ($ image );
689+ $ max_y = imagesy ($ image ) - 1 ;
690+ $ half_y = $ max_y / 2 ;
691+ $ temp_image = imageistruecolor ($ image )? imagecreatetruecolor ($ sx , 1 ): imagecreate ($ sx , 1 );
692+ for ($ y = 0 ; $ y < $ half_y ; ++$ y ) {
693+ imagecopy ($ temp_image , $ image , 0 , 0 , 0 , $ y , $ sx , 1 );
694+ imagecopy ($ image , $ image , 0 , $ y , 0 , $ max_y - $ y , $ sx , 1 );
695+ imagecopy ($ image , $ temp_image , 0 , $ max_y - $ y , 0 , 0 , $ sx , 1 );
696+ }
697+ break ;
698+ }
699+ case self ::IMG_FLIP_BOTH : {
700+ $ sx = imagesx ($ image );
701+ $ sy = imagesy ($ image );
702+ $ temp_image = imagerotate ($ image , 180 , 0 );
703+ imagecopy ($ image , $ temp_image , 0 , 0 , 0 , 0 , $ sx , $ sy );
704+ break ;
705+ }
706+ default :
707+ return null ;
706708 }
707709 imagedestroy ($ temp_image );
708710 }
709711}
710-
711- /**
712- * PHP Exception used in the ImageResize class
713- */
714- class ImageResizeException extends \Exception
715- {
716- }
0 commit comments