11<?php
2- class ImageUploadAndResize{
3-
2+ class ImageUploadAndResize
3+ {
4+
45 private $ newWidth ;
56 private $ folderName ;
67 public $ n = 0 ;
78 public $ s = 0 ;
89 public $ Sflag = 0 ;
910 public $ prepareNames ;
1011 protected $ isSubQuery = false ;
11-
12+
1213 /**
13- * Image compress and processing
14- */
15-
16- public function compressImage ($ sourceURL , $ destinationURL , $ minImgWidth , $ wmImageSource ="" , $ positionX ="" , $ positionY ="" , $ quality , $ newWidth ) {
14+ * Image compress and processing
15+ */
16+
17+ public function compressImage ($ sourceURL , $ destinationURL , $ minImgWidth , $ wmImageSource = "" , $ positionX = "" , $ positionY = "" , $ quality , $ newWidth )
18+ {
1719 $ infoImg = getimagesize ($ sourceURL );
1820 $ width = $ infoImg [0 ];
1921 $ height = $ infoImg [1 ];
20- if ($ width< $ minImgWidth ){
21- echo '<div class="alert alert-danger">Image <strong>WIDTH</strong> is less then ' . $ minImgWidth. 'px</div> ' ;
22+ if ($ width < $ minImgWidth ) {
23+ echo '<div class="alert alert-danger">Image <strong>WIDTH</strong> is less then ' . $ minImgWidth . 'px</div> ' ;
2224 exit ;
2325 }
24- if ($ newWidth!= "" ){
26+ if ($ newWidth != "" ) {
2527 $ diff = $ width / $ newWidth ;
2628 $ newHeight = $ height / $ diff ; // creating new width and height with aspect ratio
27- }else {
29+ } else {
2830 $ newWidth = $ width ;
2931 $ newHeight = $ height ;
3032 }
31-
33+
3234 $ watermark = imagecreatefrompng ($ wmImageSource );
33-
35+
3436 $ imgResource = imagecreatetruecolor ($ newWidth , $ newHeight );
35- if ($ infoImg ['mime ' ] == 'image/jpeg ' ){
37+ if ($ infoImg ['mime ' ] == 'image/jpeg ' ) {
3638 $ image = imagecreatefromjpeg ($ sourceURL );
3739 // Set the margins for the watermark and get the height/width of the watermark image
3840 $ positionRight = $ positionX ;
@@ -41,9 +43,9 @@ public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImag
4143 $ sy = imagesy ($ watermark );
4244 // width to calculate positioning of the watermark.
4345 imagecopy ($ image , $ watermark , imagesx ($ image ) - $ sx - $ positionRight , imagesy ($ image ) - $ sy - $ positionBottom , 0 , 0 , imagesx ($ watermark ), imagesy ($ watermark ));
44-
46+
4547 imagecopyresampled ($ imgResource , $ image , 0 , 0 , 0 , 0 , $ newWidth , $ newHeight , $ width , $ height );
46- }elseif ($ infoImg ['mime ' ] == 'image/jpg ' ){
48+ } elseif ($ infoImg ['mime ' ] == 'image/jpg ' ) {
4749 $ image = imagecreatefromjpeg ($ sourceURL );
4850 // Set the margins for the watermark and get the height/width of the watermark image
4951 $ positionRight = $ positionX ;
@@ -52,9 +54,9 @@ public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImag
5254 $ sy = imagesy ($ watermark );
5355 // width to calculate positioning of the watermark.
5456 imagecopy ($ image , $ watermark , imagesx ($ image ) - $ sx - $ positionRight , imagesy ($ image ) - $ sy - $ positionBottom , 0 , 0 , imagesx ($ watermark ), imagesy ($ watermark ));
55-
57+
5658 imagecopyresampled ($ imgResource , $ image , 0 , 0 , 0 , 0 , $ newWidth , $ newHeight , $ width , $ height );
57- } elseif ($ infoImg ['mime ' ] == 'image/png ' ){
59+ } elseif ($ infoImg ['mime ' ] == 'image/png ' ) {
5860 $ image = imagecreatefrompng ($ sourceURL );
5961 // Set the margins for the watermark and get the height/width of the watermark image
6062 $ positionRight = $ positionX ;
@@ -63,12 +65,12 @@ public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImag
6365 $ sy = imagesy ($ watermark );
6466 // width to calculate positioning of the watermark.
6567 imagecopy ($ image , $ watermark , imagesx ($ image ) - $ sx - $ positionRight , imagesy ($ image ) - $ sy - $ positionBottom , 0 , 0 , imagesx ($ watermark ), imagesy ($ watermark ));
66-
68+
6769 imagealphablending ($ image , false );
6870 imagesavealpha ($ image , true );
69-
71+
7072 imagecopyresampled ($ imgResource , $ image , 0 , 0 , 0 , 0 , $ newWidth , $ newHeight , $ width , $ height );
71- } elseif ($ infoImg ['mime ' ] == 'image/gif ' ){
73+ } elseif ($ infoImg ['mime ' ] == 'image/gif ' ) {
7274 $ image = imagecreatefromgif ($ sourceURL );
7375 // Set the margins for the watermark and get the height/width of the watermark image
7476 $ positionRight = $ positionX ;
@@ -77,77 +79,74 @@ public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImag
7779 $ sy = imagesy ($ watermark );
7880 // width to calculate positioning of the watermark.
7981 imagecopy ($ image , $ watermark , imagesx ($ image ) - $ sx - $ positionRight , imagesy ($ image ) - $ sy - $ positionBottom , 0 , 0 , imagesx ($ watermark ), imagesy ($ watermark ));
80-
82+
8183 imagealphablending ($ image , false );
8284 imagesavealpha ($ image , true );
83-
85+
8486 imagecopyresampled ($ imgResource , $ image , 0 , 0 , 0 , 0 , $ newWidth , $ newHeight , $ width , $ height );
8587 }
86-
88+
8789 $ RET = imagejpeg ($ imgResource , $ destinationURL , $ quality );
8890 imagedestroy ($ image );
8991 return $ RET ;
9092 }
91-
92- public function createDir ($ folderName , $ permission ){
93- if (!file_exists ($ folderName )) {
93+
94+ public function createDir ($ folderName , $ permission )
95+ {
96+ if (!file_exists ($ folderName )) {
9497 mkdir ($ folderName , $ permission , true );
9598 $ fName = $ folderName ;
96- }else {
99+ } else {
97100 $ fName = $ folderName ;
98101 }
99102 return $ fName ;
100103 }
101-
102-
103- public function uploadFiles ($ yourFileName , $ yourDestination , $ createThumb =false , $ minImgWidth =400 , $ waterMarkImgSrc ="" , $ xPosition ="" , $ yPosition ="" , $ reName ="" , $ permission =0655 , $ quality =100 , $ newWidth ="" , $ thumbWidth ="" ){
104- if (!empty ($ _FILES [$ yourFileName ])){
105- if ($ createThumb !="" and $ createThumb ===true ){
106- $ srcThumbPath = self ::createDir ($ yourDestination .'/thumb ' , $ permission ).'/ ' ;
104+
105+
106+ public function uploadFiles ($ yourFileName , $ yourDestination , $ createThumb = false , $ minImgWidth = 400 , $ waterMarkImgSrc = "" , $ xPosition = "" , $ yPosition = "" , $ reName = "" , $ permission = 0655 , $ quality = 100 , $ newWidth = "" , $ thumbWidth = "" )
107+ {
108+ if (!empty ($ _FILES [$ yourFileName ])) {
109+ if ($ createThumb != "" and $ createThumb === true ) {
110+ $ srcThumbPath = $ this ->createDir ($ yourDestination . '/thumb ' , $ permission ) . '/ ' ;
107111 }
108- foreach ($ _FILES [$ yourFileName ]['name ' ] as $ val )
109- {
112+ foreach ($ _FILES [$ yourFileName ]['name ' ] as $ val ) {
110113 $ infoExt = getimagesize ($ _FILES [$ yourFileName ]['tmp_name ' ][$ this ->n ]);
111114 $ this ->s ++;
112- $ filesName = str_replace (" " ,"" ,trim ($ _FILES [$ yourFileName ]['name ' ][$ this ->n ]));
113- $ files = explode (". " ,$ filesName );
114- $ File_Ext = substr ($ _FILES [$ yourFileName ]['name ' ][$ this ->n ], strrpos ($ _FILES [$ yourFileName ]['name ' ][$ this ->n ],'. ' ));
115-
116- if ($ infoExt ['mime ' ] == 'image/gif ' || $ infoExt ['mime ' ] == 'image/jpeg ' || $ infoExt ['mime ' ] == 'image/png ' )
117- {
118- $ srcPath = self ::createDir ($ yourDestination , $ permission ).'/ ' ;
119- if ($ reName !="" ){
120- $ fileName = $ this ->s .$ reName .$ File_Ext ;
121- }else {
122- $ fileName = $ files [0 ].$ File_Ext ;
115+ $ filesName = str_replace (" " , "" , trim ($ _FILES [$ yourFileName ]['name ' ][$ this ->n ]));
116+ $ files = explode (". " , $ filesName );
117+ $ File_Ext = substr ($ _FILES [$ yourFileName ]['name ' ][$ this ->n ], strrpos ($ _FILES [$ yourFileName ]['name ' ][$ this ->n ], '. ' ));
118+
119+ if ($ infoExt ['mime ' ] == 'image/gif ' || $ infoExt ['mime ' ] == 'image/jpeg ' || $ infoExt ['mime ' ] == 'image/png ' ) {
120+ $ srcPath = $ this ->createDir ($ yourDestination , $ permission ) . '/ ' ;
121+ if ($ reName != "" ) {
122+ $ fileName = $ this ->s . $ reName . $ File_Ext ;
123+ } else {
124+ $ fileName = $ files [0 ] . $ File_Ext ;
123125 }
124- $ path = trim ($ srcPath. $ fileName );
125- $ thumbPath = trim ($ srcThumbPath. $ fileName );
126- if ( self :: compressImage ($ _FILES [$ yourFileName ]['tmp_name ' ][$ this ->n ], $ path , $ minImgWidth , $ waterMarkImgSrc , $ xPosition , $ yPosition , $ quality , $ newWidth )){
127-
128- self :: compressImage ($ _FILES [$ yourFileName ]['tmp_name ' ][$ this ->n ], $ thumbPath , $ minImgWidth , $ waterMarkImgSrc , $ xPosition , $ yPosition , $ quality , $ thumbWidth );
129-
126+ $ path = trim ($ srcPath . $ fileName );
127+ $ thumbPath = trim ($ srcThumbPath . $ fileName );
128+ if ( $ this -> compressImage ($ _FILES [$ yourFileName ]['tmp_name ' ][$ this ->n ], $ path , $ minImgWidth , $ waterMarkImgSrc , $ xPosition , $ yPosition , $ quality , $ newWidth )) {
129+
130+ $ this -> compressImage ($ _FILES [$ yourFileName ]['tmp_name ' ][$ this ->n ], $ thumbPath , $ minImgWidth , $ waterMarkImgSrc , $ xPosition , $ yPosition , $ quality , $ thumbWidth );
131+
130132 $ this ->prepareNames [] = $ fileName ; //need to be fixed.
131133 $ this ->Sflag = 1 ; // success
132- }else {
134+ } else {
133135 $ this ->Sflag = 2 ; // file not move to the destination
134136 }
135- }
136- else
137- {
137+ } else {
138138 $ this ->Sflag = 3 ; //extention not valid
139139 }
140140 $ this ->n ++;
141141 }
142- if ($ this ->Sflag == 1 ) {
142+ if ($ this ->Sflag == 1 ) {
143143 return $ this ->prepareNames ;
144- echo '<div class="alert alert-success">Images uploaded successfully!</div> ' ;
145- }else if ($ this ->Sflag == 2 ) {
144+ echo '<div class="alert alert-success">Images uploaded successfully!</div> ' ;
145+ } else if ($ this ->Sflag == 2 ) {
146146 echo '<div class="alert alert-danger">File not move to the destination.</div> ' ;
147- }else if ($ this ->Sflag == 3 ) {
147+ } else if ($ this ->Sflag == 3 ) {
148148 echo '<div class="alert alert-danger">File extention not good. Try with <em>.PNG, .JPEG, .GIF, .JPG</em></div> ' ;
149149 }
150150 }
151151 }
152152}
153- ?>
0 commit comments