1
1
<?php
2
- class ImageUploadAndResize{
3
-
2
+ class ImageUploadAndResize
3
+ {
4
+
4
5
private $ newWidth ;
5
6
private $ folderName ;
6
7
public $ n = 0 ;
7
8
public $ s = 0 ;
8
9
public $ Sflag = 0 ;
9
10
public $ prepareNames ;
10
11
protected $ isSubQuery = false ;
11
-
12
+
12
13
/**
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
+ {
17
19
$ infoImg = getimagesize ($ sourceURL );
18
20
$ width = $ infoImg [0 ];
19
21
$ 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> ' ;
22
24
exit ;
23
25
}
24
- if ($ newWidth!= "" ){
26
+ if ($ newWidth != "" ) {
25
27
$ diff = $ width / $ newWidth ;
26
28
$ newHeight = $ height / $ diff ; // creating new width and height with aspect ratio
27
- }else {
29
+ } else {
28
30
$ newWidth = $ width ;
29
31
$ newHeight = $ height ;
30
32
}
31
-
33
+
32
34
$ watermark = imagecreatefrompng ($ wmImageSource );
33
-
35
+
34
36
$ imgResource = imagecreatetruecolor ($ newWidth , $ newHeight );
35
- if ($ infoImg ['mime ' ] == 'image/jpeg ' ){
37
+ if ($ infoImg ['mime ' ] == 'image/jpeg ' ) {
36
38
$ image = imagecreatefromjpeg ($ sourceURL );
37
39
// Set the margins for the watermark and get the height/width of the watermark image
38
40
$ positionRight = $ positionX ;
@@ -41,9 +43,9 @@ public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImag
41
43
$ sy = imagesy ($ watermark );
42
44
// width to calculate positioning of the watermark.
43
45
imagecopy ($ image , $ watermark , imagesx ($ image ) - $ sx - $ positionRight , imagesy ($ image ) - $ sy - $ positionBottom , 0 , 0 , imagesx ($ watermark ), imagesy ($ watermark ));
44
-
46
+
45
47
imagecopyresampled ($ imgResource , $ image , 0 , 0 , 0 , 0 , $ newWidth , $ newHeight , $ width , $ height );
46
- }elseif ($ infoImg ['mime ' ] == 'image/jpg ' ){
48
+ } elseif ($ infoImg ['mime ' ] == 'image/jpg ' ) {
47
49
$ image = imagecreatefromjpeg ($ sourceURL );
48
50
// Set the margins for the watermark and get the height/width of the watermark image
49
51
$ positionRight = $ positionX ;
@@ -52,9 +54,9 @@ public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImag
52
54
$ sy = imagesy ($ watermark );
53
55
// width to calculate positioning of the watermark.
54
56
imagecopy ($ image , $ watermark , imagesx ($ image ) - $ sx - $ positionRight , imagesy ($ image ) - $ sy - $ positionBottom , 0 , 0 , imagesx ($ watermark ), imagesy ($ watermark ));
55
-
57
+
56
58
imagecopyresampled ($ imgResource , $ image , 0 , 0 , 0 , 0 , $ newWidth , $ newHeight , $ width , $ height );
57
- } elseif ($ infoImg ['mime ' ] == 'image/png ' ){
59
+ } elseif ($ infoImg ['mime ' ] == 'image/png ' ) {
58
60
$ image = imagecreatefrompng ($ sourceURL );
59
61
// Set the margins for the watermark and get the height/width of the watermark image
60
62
$ positionRight = $ positionX ;
@@ -63,12 +65,12 @@ public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImag
63
65
$ sy = imagesy ($ watermark );
64
66
// width to calculate positioning of the watermark.
65
67
imagecopy ($ image , $ watermark , imagesx ($ image ) - $ sx - $ positionRight , imagesy ($ image ) - $ sy - $ positionBottom , 0 , 0 , imagesx ($ watermark ), imagesy ($ watermark ));
66
-
68
+
67
69
imagealphablending ($ image , false );
68
70
imagesavealpha ($ image , true );
69
-
71
+
70
72
imagecopyresampled ($ imgResource , $ image , 0 , 0 , 0 , 0 , $ newWidth , $ newHeight , $ width , $ height );
71
- } elseif ($ infoImg ['mime ' ] == 'image/gif ' ){
73
+ } elseif ($ infoImg ['mime ' ] == 'image/gif ' ) {
72
74
$ image = imagecreatefromgif ($ sourceURL );
73
75
// Set the margins for the watermark and get the height/width of the watermark image
74
76
$ positionRight = $ positionX ;
@@ -77,77 +79,74 @@ public function compressImage($sourceURL, $destinationURL, $minImgWidth, $wmImag
77
79
$ sy = imagesy ($ watermark );
78
80
// width to calculate positioning of the watermark.
79
81
imagecopy ($ image , $ watermark , imagesx ($ image ) - $ sx - $ positionRight , imagesy ($ image ) - $ sy - $ positionBottom , 0 , 0 , imagesx ($ watermark ), imagesy ($ watermark ));
80
-
82
+
81
83
imagealphablending ($ image , false );
82
84
imagesavealpha ($ image , true );
83
-
85
+
84
86
imagecopyresampled ($ imgResource , $ image , 0 , 0 , 0 , 0 , $ newWidth , $ newHeight , $ width , $ height );
85
87
}
86
-
88
+
87
89
$ RET = imagejpeg ($ imgResource , $ destinationURL , $ quality );
88
90
imagedestroy ($ image );
89
91
return $ RET ;
90
92
}
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 )) {
94
97
mkdir ($ folderName , $ permission , true );
95
98
$ fName = $ folderName ;
96
- }else {
99
+ } else {
97
100
$ fName = $ folderName ;
98
101
}
99
102
return $ fName ;
100
103
}
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 ) . '/ ' ;
107
111
}
108
- foreach ($ _FILES [$ yourFileName ]['name ' ] as $ val )
109
- {
112
+ foreach ($ _FILES [$ yourFileName ]['name ' ] as $ val ) {
110
113
$ infoExt = getimagesize ($ _FILES [$ yourFileName ]['tmp_name ' ][$ this ->n ]);
111
114
$ 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 ;
123
125
}
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
+
130
132
$ this ->prepareNames [] = $ fileName ; //need to be fixed.
131
133
$ this ->Sflag = 1 ; // success
132
- }else {
134
+ } else {
133
135
$ this ->Sflag = 2 ; // file not move to the destination
134
136
}
135
- }
136
- else
137
- {
137
+ } else {
138
138
$ this ->Sflag = 3 ; //extention not valid
139
139
}
140
140
$ this ->n ++;
141
141
}
142
- if ($ this ->Sflag == 1 ) {
142
+ if ($ this ->Sflag == 1 ) {
143
143
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 ) {
146
146
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 ) {
148
148
echo '<div class="alert alert-danger">File extention not good. Try with <em>.PNG, .JPEG, .GIF, .JPG</em></div> ' ;
149
149
}
150
150
}
151
151
}
152
152
}
153
- ?>
0 commit comments