1 | <?php |
---|
2 | |
---|
3 | if (!defined('PHPWG_ROOT_PATH')){ |
---|
4 | die ("Hacking attempt!"); |
---|
5 | } |
---|
6 | |
---|
7 | |
---|
8 | class AjaxImgupload{ |
---|
9 | var $options = array(); |
---|
10 | /* constructor */ |
---|
11 | function __construct($options = array()){ |
---|
12 | |
---|
13 | /* default configuration */ |
---|
14 | |
---|
15 | $this->options = array( 'rootImg' => "/images/", |
---|
16 | 'destDir'=>'/images/', |
---|
17 | 'upload_url'=>'/images/', |
---|
18 | 'file_support'=>array('jpeg', 'jpg', 'png', 'gif','bmp' ), |
---|
19 | 'max_size'=>array( |
---|
20 | 'jpeg'=>'20111110', |
---|
21 | 'jpg'=>'2011110', |
---|
22 | 'gif'=>'2011110', |
---|
23 | 'png'=>'2011110', |
---|
24 | 'bmp'=>'2011110', |
---|
25 | 'txt'=>'500' |
---|
26 | ), |
---|
27 | 'thumbnail_dir'=>'/images/', |
---|
28 | 'thumbnail_url'=>'/images/', |
---|
29 | 'thumbnail_dimension'=>array("max_width"=>200,"max_height"=>200) |
---|
30 | ); |
---|
31 | |
---|
32 | /* replace with customi condiguration */ |
---|
33 | $this->options = array_replace_recursive($this->options, $options); |
---|
34 | } |
---|
35 | /****** add file to repository *********/ |
---|
36 | function addFile() { |
---|
37 | /* validate uploaded file */ |
---|
38 | $this->validate(); |
---|
39 | } |
---|
40 | /****************************** |
---|
41 | * validate file |
---|
42 | * - file size |
---|
43 | * - supported file type |
---|
44 | * - any kind of error in file upload |
---|
45 | *******************************/ |
---|
46 | function validateFiles() { |
---|
47 | $errorslist = array(); |
---|
48 | $details = array(); |
---|
49 | foreach($_FILES as $key=>$value){ |
---|
50 | //print $key; |
---|
51 | $name = $value['name']; |
---|
52 | $type = $value['type']; |
---|
53 | $size = $value['size']; |
---|
54 | |
---|
55 | $extension = strtolower(substr(strstr($name,"."),1)); |
---|
56 | |
---|
57 | /* extension check */ |
---|
58 | if(!in_array($extension,$this->options['file_support'])) { |
---|
59 | $errorslist[$key][$name][] = "File is not of correct type";continue; |
---|
60 | } |
---|
61 | |
---|
62 | /* Size of file check*/ |
---|
63 | if ( isset($this->options['max_size'][$extension]) ){ |
---|
64 | if(1024 * $this->options['max_size'][$extension] < $size) { |
---|
65 | $errorslist[$key][$name][] = "File is too large".$size; |
---|
66 | continue; |
---|
67 | } |
---|
68 | }else{ |
---|
69 | $errorslist['error'] = $extension . " no max-size " . print_r($this->options['max_size'],true) ; |
---|
70 | |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | /* mime type check */ |
---|
75 | if(!in_array($extension,$this->options['file_support'])) { |
---|
76 | $errorslist[$key][$name][] = "File is not of correct type";continue; |
---|
77 | } |
---|
78 | /* check for any kind of error in file upload */ |
---|
79 | if($value['error']!=0) { |
---|
80 | $errorslist[$key][$name][] = "There was some error while uploading file. Please try again.";continue; |
---|
81 | } |
---|
82 | /* generate unique name for file */ |
---|
83 | $filename = rand(10,10000)."_".date("Ymdh").".".$extension; |
---|
84 | /* move file from temporary location to some location */ |
---|
85 | |
---|
86 | move_uploaded_file($value['tmp_name'], $this->options['destDir']."$filename"); |
---|
87 | move_uploaded_file($value['tmp_name'], $this->options['destDir']."new.png"); |
---|
88 | |
---|
89 | if(isset($this->options['thumbnail_dir'])) { |
---|
90 | $this->createThumbnail($filename); |
---|
91 | } |
---|
92 | |
---|
93 | $details['files'][] = array( |
---|
94 | "name"=>$filename, |
---|
95 | "upload_name"=>$name, |
---|
96 | 'thumbimgurl'=>$this->options['destDir'].$filename, |
---|
97 | 'imgUrl'=>$this->options['destDir'].$filename, |
---|
98 | 'action'=>'added', |
---|
99 | 'errors'=> $errorslist |
---|
100 | |
---|
101 | ); |
---|
102 | |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | if(isset($this->options['callback'])) |
---|
107 | { |
---|
108 | call_user_func($this->options['callback'],$details['files']); |
---|
109 | } |
---|
110 | return $details; |
---|
111 | |
---|
112 | } |
---|
113 | |
---|
114 | /****************************************************** |
---|
115 | *this function generate thumbnail for uploaded images |
---|
116 | *******************************************************/ |
---|
117 | function createThumbnail($file_name) { |
---|
118 | $dimension = $this->options['thumbnail_dimension']; |
---|
119 | $file_path = $this->options['destDir'].$file_name; |
---|
120 | $new_file_path = $this->options['thumbnail_dir'].$file_name; |
---|
121 | list($img_width, $img_height) = @getimagesize($file_path); |
---|
122 | |
---|
123 | if (!$img_width || !$img_height) { |
---|
124 | return false; |
---|
125 | } |
---|
126 | $scale = min( |
---|
127 | $dimension['max_width'] / $img_width, |
---|
128 | $dimension['max_height'] / $img_height |
---|
129 | ); |
---|
130 | |
---|
131 | if ($scale >= 1) { |
---|
132 | if ($file_path !== $new_file_path) { |
---|
133 | return copy($file_path, $new_file_path); |
---|
134 | } |
---|
135 | return true; |
---|
136 | } |
---|
137 | |
---|
138 | $new_width = $img_width * $scale; |
---|
139 | $new_height = $img_height * $scale; |
---|
140 | $new_img = @imagecreatetruecolor($new_width, $new_height); |
---|
141 | |
---|
142 | switch (strtolower(substr(strrchr($file_name, '.'), 1))) { |
---|
143 | case 'jpg': |
---|
144 | case 'jpeg': |
---|
145 | |
---|
146 | $src_img = @imagecreatefromjpeg($file_path); |
---|
147 | $write_image = 'imagejpeg'; |
---|
148 | $image_quality = 75; |
---|
149 | break; |
---|
150 | case 'gif': |
---|
151 | @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0)); |
---|
152 | $src_img = @imagecreatefromgif($file_path); |
---|
153 | $write_image = 'imagegif'; |
---|
154 | $image_quality = null; |
---|
155 | break; |
---|
156 | case 'png': |
---|
157 | @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0)); |
---|
158 | @imagealphablending($new_img, false); |
---|
159 | @imagesavealpha($new_img, true); |
---|
160 | $src_img = @imagecreatefrompng($file_path); |
---|
161 | $write_image = 'imagepng'; |
---|
162 | $image_quality = 9; |
---|
163 | break; |
---|
164 | default: |
---|
165 | $src_img = null; |
---|
166 | } |
---|
167 | $success = $src_img && @imagecopyresampled( |
---|
168 | $new_img, |
---|
169 | $src_img, |
---|
170 | 0, 0, 0, 0, |
---|
171 | $new_width, |
---|
172 | $new_height, |
---|
173 | $img_width, |
---|
174 | $img_height |
---|
175 | ) && $write_image($new_img, $new_file_path, $image_quality); |
---|
176 | |
---|
177 | // Free up memory (imagedestroy does not delete files): |
---|
178 | @imagedestroy($src_img); |
---|
179 | @imagedestroy($new_img); |
---|
180 | return $success; |
---|
181 | } |
---|
182 | |
---|
183 | /* Remove file from disk and call user function to clean related to file from database */ |
---|
184 | function removeFile($names=array()) { |
---|
185 | $details = array(); |
---|
186 | foreach($names as $name){ |
---|
187 | $status = array("name"=>$name,'action'=>'deleted'); |
---|
188 | $status['status'] = $this->unlinkImages($name); |
---|
189 | $details['files'][] = $status; |
---|
190 | } |
---|
191 | if(isset($this->options['callback'])) { |
---|
192 | call_user_func($this->options['callback'],$details['files']); |
---|
193 | } |
---|
194 | return true; |
---|
195 | } |
---|
196 | |
---|
197 | /* File delete */ |
---|
198 | function unlinkImages($name) { |
---|
199 | if(file_exists($this->options['destDir'].$name)) |
---|
200 | { |
---|
201 | @unlink($this->options['destDir'].$name); |
---|
202 | @unlink($this->options['thumbnail_dir'].$name); |
---|
203 | return true; |
---|
204 | } |
---|
205 | else |
---|
206 | return false; |
---|
207 | } |
---|
208 | |
---|
209 | /* Return all file */ |
---|
210 | function getFilesDetails() { |
---|
211 | $files = array(); |
---|
212 | if(isset($this->options['loadcallback'])) |
---|
213 | $files = call_user_func($this->options['loadcallback'],$this); |
---|
214 | return $files; |
---|
215 | } |
---|
216 | |
---|
217 | /* get thumbnail url */ |
---|
218 | function getFile($id,$thumb=true) { |
---|
219 | return $this->options['thumbnail_url'].$id; |
---|
220 | } |
---|
221 | |
---|
222 | /* |
---|
223 | interaction related to common functionality |
---|
224 | - addimg upload image to repository |
---|
225 | - getfilethumb get file thumbnail as when required |
---|
226 | - deleteimg delete image from server |
---|
227 | - retrive all image details located on server |
---|
228 | |
---|
229 | */ |
---|
230 | function responder($action=''){ |
---|
231 | switch($action) |
---|
232 | { |
---|
233 | case "addimg": |
---|
234 | $details = $this->validateFiles(); |
---|
235 | print "<html><body>"; |
---|
236 | print(json_encode($details)); |
---|
237 | print "</body></html>"; |
---|
238 | break; |
---|
239 | case "getfilethumb": |
---|
240 | print $this->getFile($_REQUEST['action'],$name); |
---|
241 | break; |
---|
242 | case "deleteimg": |
---|
243 | $filenames = explode(",",$_REQUEST['name']); |
---|
244 | print $this->removeFile($filenames); |
---|
245 | break; |
---|
246 | case "getimages": |
---|
247 | $details = $this->getFilesDetails(); |
---|
248 | print "<html><body>"; |
---|
249 | print(json_encode($details)); |
---|
250 | print "</body></html>"; |
---|
251 | break; |
---|
252 | } |
---|
253 | |
---|
254 | } |
---|
255 | |
---|
256 | } |
---|
257 | ?> |
---|