1
+ <?php
2
+
3
+ namespace anyFileUpload ;
4
+
5
+ use Exception ;
6
+
7
+ class FilesUploadAndImageResize
8
+ {
9
+ protected $ allowExtension = [];
10
+ protected $ fileDestination = '' ;
11
+ protected $ filePermission = '0655 ' ;
12
+ protected $ n = 0 ;
13
+ protected $ s = 0 ;
14
+ protected $ format = 'array ' ;
15
+ protected $ param = [];
16
+ public $ uploadedData = '' ;
17
+
18
+ /**
19
+ * Must Initialize main param
20
+ * @param
21
+ * 1) Format set the return result set array or json
22
+ * 2) Pass file extentions in array
23
+ * 3) Dir path where you want to upload the files *thumb folder will be craeted inside
24
+ *
25
+ */
26
+
27
+ public function __construct (string $ format , array $ allowExtension , string $ fileDestination , int $ filePermission )
28
+ {
29
+ $ this ->format = strtolower ($ format );
30
+ $ this ->allowExtension = $ allowExtension ;
31
+ $ this ->fileDestination = $ fileDestination ;
32
+ $ this ->filePermission = $ filePermission ;
33
+ }
34
+
35
+ /**
36
+ * Image compress and processing
37
+ * Main function that we used to compress images
38
+ * Consider only jpeg,jpg,png,gif images [If you want to use other extensions then use Imagick]
39
+ *
40
+ */
41
+
42
+ public function compressImage (string $ sourceURL , string $ destinationURL , int $ minImgWidth , array $ waterMark = [], int $ quality , string $ newWidth ): bool |string
43
+ {
44
+ try {
45
+ if (!empty ($ waterMark )) {
46
+ $ waterMark ['font-size ' ] = (empty ($ waterMark ['font-size ' ])) ? 25 : $ waterMark ['font-size ' ];
47
+ $ waterMark ['font-family ' ] = (empty ($ waterMark ['font-family ' ])) ? __DIR__ . "/fonts/Myriad-Pro-Regular.ttf " : $ waterMark ['font-family ' ];
48
+ $ waterMark ['font-color ' ] = (empty ($ waterMark ['font-color ' ])) ? '#000000 ' : $ waterMark ['font-color ' ];
49
+ $ positionX = $ waterMark ['position-x ' ] ?? '' ;
50
+ $ positionY = $ waterMark ['position-y ' ] ?? '' ;
51
+ }
52
+
53
+ $ infoImg = getimagesize ($ sourceURL );
54
+ $ width = $ infoImg [0 ];
55
+ $ height = $ infoImg [1 ];
56
+ if ($ width < $ minImgWidth ) {
57
+ echo '<div class="alert alert-danger">Image <strong>WIDTH</strong> is less then ' . $ minImgWidth . 'px</div> ' ;
58
+ exit ;
59
+ }
60
+
61
+ $ image = '' ;
62
+ if ($ infoImg ['mime ' ] == 'image/jpeg ' ) {
63
+ $ image = imagecreatefromjpeg ($ sourceURL );
64
+ } elseif ($ infoImg ['mime ' ] == 'image/jpg ' ) {
65
+ $ image = imagecreatefromjpeg ($ sourceURL );
66
+ } elseif ($ infoImg ['mime ' ] == 'image/png ' ) {
67
+ $ image = imagecreatefrompng ($ sourceURL );
68
+ } elseif ($ infoImg ['mime ' ] == 'image/gif ' ) {
69
+ $ image = imagecreatefromgif ($ sourceURL );
70
+ }
71
+
72
+ //Adding watermark
73
+ if (!empty ($ waterMark )) {
74
+ if (!empty ($ waterMark ['value ' ]) && is_file ($ waterMark ['value ' ])) {
75
+ $ watermark = imagecreatefrompng ($ waterMark ['value ' ]);
76
+ imagecopy ($ image , $ watermark , 0 , 0 , 0 , 0 , imagesx ($ watermark ), imagesy ($ watermark ));
77
+ } else {
78
+ $ positionRight = $ positionX ;
79
+ $ positionBottom = $ positionY ;
80
+ $ sx = imagesx ($ image );
81
+ $ sy = imagesy ($ image );
82
+ $ watermarktext = ($ waterMark ['value ' ] != "" ) ? $ waterMark ['value ' ] : '' ;
83
+ $ font = ($ waterMark ['font-family ' ] != "" ) ? $ waterMark ['font-family ' ] : '' ;
84
+ $ fontsize = ($ waterMark ['font-size ' ] != "" ) ? $ waterMark ['font-size ' ] : '' ;
85
+ list ($ r , $ g , $ b ) = sscanf ($ waterMark ['font-color ' ], "#%02x%02x%02x " );
86
+ $ color = imagecolorallocate ($ image , $ r , $ g , $ b );
87
+ imagettftext ($ image , $ fontsize , 0 , $ sx - $ positionRight , $ sy - $ positionBottom , $ color , $ font , $ watermarktext );
88
+ }
89
+ }
90
+
91
+ // Creating new width and height with aspect ratio
92
+ if ($ newWidth != "" ) {
93
+ $ diff = $ width / $ newWidth ;
94
+ $ newHeight = $ height / $ diff ;
95
+ } else {
96
+ $ newWidth = $ width ;
97
+ $ newHeight = $ height ;
98
+ }
99
+
100
+ $ imgResource = imagecreatetruecolor ($ newWidth , $ newHeight );
101
+
102
+ imagealphablending ($ imgResource , false );
103
+ imagesavealpha ($ imgResource , true );
104
+
105
+ imagecopyresampled ($ imgResource , $ image , 0 , 0 , 0 , 0 , $ newWidth , $ newHeight , $ width , $ height );
106
+ if ($ infoImg ['mime ' ] == 'image/png ' || $ infoImg ['mime ' ] == 'image/gif ' ) {
107
+ $ newQuality = ($ quality / 10 ) - 1 ;
108
+ imagealphablending ($ imgResource , false );
109
+ imagesavealpha ($ imgResource , true );
110
+ $ responseImage = imagepng ($ imgResource , $ destinationURL , $ newQuality ); //For png quality range is 0-9
111
+ } else {
112
+ $ responseImage = imagejpeg ($ imgResource , $ destinationURL , $ quality );
113
+ }
114
+
115
+ imagedestroy ($ image );
116
+ return $ responseImage ;
117
+ } catch (Exception $ e ) {
118
+ return $ e ->getMessage ();
119
+ }
120
+ }
121
+
122
+ /**
123
+ * Create folder with permission
124
+ * If exist no need to create
125
+ * @param folder path and permission [default = 0655]
126
+ *
127
+ */
128
+
129
+ public function createDir (string $ fileDestination , int $ filePermission ): string
130
+ {
131
+ try {
132
+ if (!file_exists ($ fileDestination )) {
133
+ mkdir ($ fileDestination , $ filePermission , true );
134
+ $ fName = $ fileDestination ;
135
+ } else {
136
+ $ fName = $ fileDestination ;
137
+ }
138
+ return $ fName ;
139
+ } catch (Exception $ e ) {
140
+ return $ e ->getMessage ();
141
+ }
142
+ }
143
+
144
+ /**
145
+ * Main function to upload files
146
+ * This function return Array with status & names
147
+ * Array index tells the status of files
148
+ * ['bad-extension-files'] return all files with bad extension which is set by user
149
+ * ['bad-extensions'] return only bad extensions which is set by user
150
+ * ['uploaded-files'] return all uploaded files
151
+ * ['real-uploaded-files'] return all uploaded real files name.
152
+ * ['not-uploaded-files'] return all not move files into the destination folder [ Note: Folder (Dir) Permission issue ]
153
+ *
154
+ */
155
+
156
+ public function uploadFiles (string $ fileParamName , int $ minImgWidth = 400 , array $ waterMark , string $ reName = "" , int $ quality = 100 , string $ newWidth = "" , array $ thumbWidth = []): array |string
157
+ {
158
+ try {
159
+ if (!empty ($ _FILES [$ fileParamName ])) {
160
+
161
+ $ srcPath = $ this ->createDir ($ this ->fileDestination , $ this ->filePermission ) . '/ ' ;
162
+ if (isset ($ thumbWidth ) && !empty ($ thumbWidth )) {
163
+ $ srcThumbPath = $ this ->createDir ($ this ->fileDestination . '/thumb ' , $ this ->filePermission ) . '/ ' ;
164
+ }
165
+ foreach ($ _FILES [$ fileParamName ]['name ' ] as $ val ) {
166
+ $ this ->s ++;
167
+
168
+ $ fileInfo = pathinfo (basename ($ _FILES [$ fileParamName ]['name ' ][$ this ->n ]), PATHINFO_EXTENSION );
169
+ $ fileName = 'file- ' . $ this ->s . '- ' . rand (0 , 999 ) . time () . '. ' . $ fileInfo ;
170
+ if ($ reName != "" ) {
171
+ $ fileName = $ this ->s . $ reName . '. ' . $ fileInfo ;
172
+ }
173
+ $ filePath = trim ($ srcPath . $ fileName );
174
+ if (in_array (strtolower ($ fileInfo ), array_map ('strtolower ' , $ this ->allowExtension )) || empty ($ this ->allowExtension )) {
175
+ // Upload and compress only images
176
+ if (strtolower ($ fileInfo ) == 'gif ' || strtolower ($ fileInfo ) == 'jpeg ' || strtolower ($ fileInfo ) == 'jpg ' || strtolower ($ fileInfo ) == 'png ' ) {
177
+ if ($ this ->compressImage ($ _FILES [$ fileParamName ]['tmp_name ' ][$ this ->n ], $ filePath , $ minImgWidth , $ waterMark , $ quality , $ newWidth )) {
178
+ if (isset ($ thumbWidth ) && !empty ($ thumbWidth )) {
179
+ foreach ($ thumbWidth as $ tw ) {
180
+ $ thumbPath = trim ($ srcThumbPath . $ tw . '- ' . $ fileName );
181
+ $ this ->compressImage ($ _FILES [$ fileParamName ]['tmp_name ' ][$ this ->n ], $ thumbPath , $ minImgWidth , $ waterMark , $ quality , $ tw );
182
+ $ this ->param ['uploaded-thumb-files ' ][$ tw ][] = $ tw . '- ' . $ fileName ; //All uploaded thumbnail files name are move in this array
183
+ $ this ->param ['path-uploaded-thumb-files ' ][] = trim ($ srcThumbPath ); //All uploaded thumbnail files with complete path
184
+ }
185
+ }
186
+
187
+ $ this ->param ['real-uploaded-files ' ][] = $ val ; //All uploaded files with real name
188
+ $ this ->param ['uploaded-files ' ][] = $ fileName ; //All uploaded files name are move in this array
189
+ $ this ->param ['path-uploaded-files ' ][] = $ srcPath ; //All uploaded files name are move in this array
190
+ } else {
191
+ $ this ->param ['not-uploaded-files ' ][] = $ fileName ; //All not move files name into the destination folder [ Note: Check Folder Permission ]
192
+ }
193
+ } else {
194
+ // Upload all other files
195
+ if (move_uploaded_file ($ _FILES [$ fileParamName ]['tmp_name ' ][$ this ->n ], $ filePath )) {
196
+ $ this ->param ['real-uploaded-files ' ][] = $ val ; //All uploaded files with real name
197
+ $ this ->param ['uploaded-files ' ][] = $ fileName ; //All uploaded files name are move in this array
198
+ $ this ->param ['path-uploaded-files ' ][] = $ srcPath ; //All uploaded files name are move in this array
199
+ } else {
200
+ $ this ->param ['not-uploaded-files ' ][] = $ fileName ; //All not move files name into the destination folder [ Note: Check Folder Permission ]
201
+ }
202
+ }
203
+ } else {
204
+ $ this ->param ['bad-extension-files ' ][] = $ fileName ; //Bad extension files name are move in this array
205
+ $ this ->param ['bad-extensions ' ][] = strtolower ($ fileInfo ); //Bad extensions move in this array
206
+ }
207
+
208
+ $ this ->n ++;
209
+ }
210
+ if ($ this ->format == "array " ) {
211
+ $ this ->uploadedData = $ this ->param ;
212
+ } else if ($ this ->format == "json " ) {
213
+ $ this ->uploadedData = json_encode ($ this ->param );
214
+ }
215
+ return $ this ->uploadedData ;
216
+ }
217
+ } catch (Exception $ e ) {
218
+ return $ e ->getMessage ();
219
+ }
220
+ }
221
+ }
0 commit comments