[5089] | 1 | <?php |
---|
| 2 | // TODO |
---|
| 3 | // * check md5sum (already exists?) |
---|
| 4 | |
---|
| 5 | include_once(PHPWG_ROOT_PATH.'include/common.inc.php'); |
---|
| 6 | include_once(PHPWG_ROOT_PATH.'include/ws_functions.inc.php'); |
---|
| 7 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 8 | |
---|
| 9 | // Here is the plan |
---|
| 10 | // |
---|
| 11 | // 1) move uploaded file to upload/2010/01/22/20100122003814-449ada00.jpg |
---|
| 12 | // |
---|
| 13 | // 2) if taller than max_height or wider than max_width, move to pwg_high |
---|
| 14 | // + web sized creation |
---|
| 15 | // |
---|
| 16 | // 3) thumbnail creation from web sized |
---|
| 17 | // |
---|
| 18 | // 4) register in database |
---|
| 19 | |
---|
[5957] | 20 | // add default event handler for image and thumbnail resize |
---|
| 21 | add_event_handler('upload_image_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 6); |
---|
| 22 | add_event_handler('upload_thumbnail_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 6); |
---|
| 23 | |
---|
[5089] | 24 | function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null) |
---|
| 25 | { |
---|
| 26 | global $conf; |
---|
| 27 | |
---|
| 28 | // current date |
---|
| 29 | list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();')); |
---|
| 30 | list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4); |
---|
| 31 | |
---|
| 32 | // upload directory hierarchy |
---|
| 33 | $upload_dir = sprintf( |
---|
[6052] | 34 | PHPWG_ROOT_PATH.$conf['upload_dir'].'/%s/%s/%s', |
---|
[5089] | 35 | $year, |
---|
| 36 | $month, |
---|
| 37 | $day |
---|
| 38 | ); |
---|
| 39 | |
---|
| 40 | // compute file path |
---|
| 41 | $md5sum = md5_file($source_filepath); |
---|
| 42 | $date_string = preg_replace('/[^\d]/', '', $dbnow); |
---|
| 43 | $random_string = substr($md5sum, 0, 8); |
---|
| 44 | $filename_wo_ext = $date_string.'-'.$random_string; |
---|
| 45 | $file_path = $upload_dir.'/'.$filename_wo_ext.'.jpg'; |
---|
| 46 | |
---|
| 47 | prepare_directory($upload_dir); |
---|
| 48 | if (is_uploaded_file($source_filepath)) |
---|
| 49 | { |
---|
| 50 | move_uploaded_file($source_filepath, $file_path); |
---|
| 51 | } |
---|
| 52 | else |
---|
| 53 | { |
---|
| 54 | copy($source_filepath, $file_path); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | if ($conf['upload_form_websize_resize'] |
---|
| 58 | and need_resize($file_path, $conf['upload_form_websize_maxwidth'], $conf['upload_form_websize_maxheight'])) |
---|
| 59 | { |
---|
| 60 | $high_path = file_path_for_type($file_path, 'high'); |
---|
| 61 | $high_dir = dirname($high_path); |
---|
| 62 | prepare_directory($high_dir); |
---|
| 63 | |
---|
| 64 | rename($file_path, $high_path); |
---|
| 65 | $high_infos = pwg_image_infos($high_path); |
---|
| 66 | |
---|
[5957] | 67 | trigger_event('upload_image_resize', |
---|
| 68 | false, |
---|
[5089] | 69 | $high_path, |
---|
| 70 | $file_path, |
---|
| 71 | $conf['upload_form_websize_maxwidth'], |
---|
| 72 | $conf['upload_form_websize_maxheight'], |
---|
| 73 | $conf['upload_form_websize_quality'] |
---|
| 74 | ); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | $file_infos = pwg_image_infos($file_path); |
---|
| 78 | |
---|
| 79 | $thumb_path = file_path_for_type($file_path, 'thumb'); |
---|
| 80 | $thumb_dir = dirname($thumb_path); |
---|
| 81 | prepare_directory($thumb_dir); |
---|
| 82 | |
---|
[5957] | 83 | trigger_event('upload_thumbnail_resize', |
---|
| 84 | false, |
---|
[5089] | 85 | $file_path, |
---|
| 86 | $thumb_path, |
---|
| 87 | $conf['upload_form_thumb_maxwidth'], |
---|
| 88 | $conf['upload_form_thumb_maxheight'], |
---|
| 89 | $conf['upload_form_thumb_quality'] |
---|
| 90 | ); |
---|
| 91 | |
---|
| 92 | $thumb_infos = pwg_image_infos($thumb_path); |
---|
| 93 | |
---|
| 94 | // database registration |
---|
| 95 | $insert = array( |
---|
| 96 | 'file' => isset($original_filename) ? $original_filename : basename($file_path), |
---|
| 97 | 'date_available' => $dbnow, |
---|
| 98 | 'tn_ext' => 'jpg', |
---|
[6052] | 99 | 'path' => preg_replace('#^'.preg_quote(PHPWG_ROOT_PATH).'#', '', $file_path), |
---|
[5089] | 100 | 'filesize' => $file_infos['filesize'], |
---|
| 101 | 'width' => $file_infos['width'], |
---|
| 102 | 'height' => $file_infos['height'], |
---|
| 103 | 'md5sum' => $md5sum, |
---|
| 104 | ); |
---|
| 105 | |
---|
| 106 | if (isset($high_infos)) |
---|
| 107 | { |
---|
| 108 | $insert['has_high'] = 'true'; |
---|
| 109 | $insert['high_filesize'] = $high_infos['filesize']; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | if (isset($level)) |
---|
| 113 | { |
---|
| 114 | $insert['level'] = $level; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | mass_inserts( |
---|
| 118 | IMAGES_TABLE, |
---|
| 119 | array_keys($insert), |
---|
| 120 | array($insert) |
---|
| 121 | ); |
---|
| 122 | |
---|
| 123 | $image_id = mysql_insert_id(); |
---|
| 124 | |
---|
| 125 | if (isset($categories) and count($categories) > 0) |
---|
| 126 | { |
---|
| 127 | associate_images_to_categories( |
---|
| 128 | array($image_id), |
---|
| 129 | $categories |
---|
| 130 | ); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | // update metadata from the uploaded file (exif/iptc) |
---|
| 134 | update_metadata(array($image_id=>$file_path)); |
---|
| 135 | |
---|
| 136 | invalidate_user_cache(); |
---|
| 137 | |
---|
| 138 | return $image_id; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | function prepare_directory($directory) |
---|
| 142 | { |
---|
| 143 | if (!is_dir($directory)) { |
---|
[6384] | 144 | if (substr(PHP_OS, 0, 3) == 'WIN') |
---|
| 145 | { |
---|
| 146 | $directory = str_replace('/', DIRECTORY_SEPARATOR, $directory); |
---|
| 147 | } |
---|
[5089] | 148 | umask(0000); |
---|
| 149 | $recursive = true; |
---|
| 150 | if (!@mkdir($directory, 0777, $recursive)) |
---|
| 151 | { |
---|
| 152 | die('[prepare_directory] cannot create directory "'.$directory.'"'); |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | if (!is_writable($directory)) |
---|
| 157 | { |
---|
| 158 | // last chance to make the directory writable |
---|
| 159 | @chmod($directory, 0777); |
---|
| 160 | |
---|
| 161 | if (!is_writable($directory)) |
---|
| 162 | { |
---|
| 163 | die('[prepare_directory] directory "'.$directory.'" has no write access'); |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | secure_directory($directory); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | function need_resize($image_filepath, $max_width, $max_height) |
---|
| 171 | { |
---|
| 172 | list($width, $height) = getimagesize($image_filepath); |
---|
| 173 | |
---|
| 174 | if ($width > $max_width or $height > $max_height) |
---|
| 175 | { |
---|
| 176 | return true; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | return false; |
---|
| 180 | } |
---|
| 181 | |
---|
[5957] | 182 | function pwg_image_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality) |
---|
[5089] | 183 | { |
---|
[5957] | 184 | if ($result !== false) |
---|
| 185 | { |
---|
| 186 | //someone hooked us - so we skip |
---|
| 187 | return $result; |
---|
| 188 | } |
---|
| 189 | |
---|
[5089] | 190 | if (!function_exists('gd_info')) |
---|
| 191 | { |
---|
| 192 | return false; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | // extension of the picture filename |
---|
| 196 | $extension = strtolower(get_extension($source_filepath)); |
---|
| 197 | |
---|
| 198 | $source_image = null; |
---|
| 199 | if (in_array($extension, array('jpg', 'jpeg'))) |
---|
| 200 | { |
---|
| 201 | $source_image = @imagecreatefromjpeg($source_filepath); |
---|
| 202 | } |
---|
| 203 | else if ($extension == 'png') |
---|
| 204 | { |
---|
| 205 | $source_image = @imagecreatefrompng($source_filepath); |
---|
| 206 | } |
---|
| 207 | else |
---|
| 208 | { |
---|
| 209 | die('unsupported file extension'); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | // width/height |
---|
| 213 | $source_width = imagesx($source_image); |
---|
| 214 | $source_height = imagesy($source_image); |
---|
| 215 | |
---|
| 216 | $ratio_width = $source_width / $max_width; |
---|
| 217 | $ratio_height = $source_height / $max_height; |
---|
| 218 | |
---|
| 219 | // maximal size exceeded ? |
---|
| 220 | if ($ratio_width > 1 or $ratio_height > 1) |
---|
| 221 | { |
---|
| 222 | if ($ratio_width < $ratio_height) |
---|
| 223 | { |
---|
| 224 | $destination_width = ceil($source_width / $ratio_height); |
---|
| 225 | $destination_height = $max_height; |
---|
| 226 | } |
---|
| 227 | else |
---|
| 228 | { |
---|
| 229 | $destination_width = $max_width; |
---|
| 230 | $destination_height = ceil($source_height / $ratio_width); |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | else |
---|
| 234 | { |
---|
| 235 | // the image doesn't need any resize! We just copy it to the destination |
---|
| 236 | copy($source_filepath, $destination_filepath); |
---|
| 237 | return true; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | $destination_image = imagecreatetruecolor($destination_width, $destination_height); |
---|
| 241 | |
---|
| 242 | imagecopyresampled( |
---|
| 243 | $destination_image, |
---|
| 244 | $source_image, |
---|
| 245 | 0, |
---|
| 246 | 0, |
---|
| 247 | 0, |
---|
| 248 | 0, |
---|
| 249 | $destination_width, |
---|
| 250 | $destination_height, |
---|
| 251 | $source_width, |
---|
| 252 | $source_height |
---|
| 253 | ); |
---|
| 254 | |
---|
| 255 | imagejpeg($destination_image, $destination_filepath, $quality); |
---|
| 256 | // freeing memory ressources |
---|
| 257 | imagedestroy($source_image); |
---|
| 258 | imagedestroy($destination_image); |
---|
| 259 | |
---|
| 260 | // everything should be OK if we are here! |
---|
| 261 | return true; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | function pwg_image_infos($path) |
---|
| 265 | { |
---|
| 266 | list($width, $height) = getimagesize($path); |
---|
| 267 | $filesize = floor(filesize($path)/1024); |
---|
| 268 | |
---|
| 269 | return array( |
---|
| 270 | 'width' => $width, |
---|
| 271 | 'height' => $height, |
---|
| 272 | 'filesize' => $filesize, |
---|
| 273 | ); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | function is_valid_image_extension($extension) |
---|
| 277 | { |
---|
| 278 | return in_array(strtolower($extension), array('jpg', 'jpeg', 'png')); |
---|
| 279 | } |
---|
| 280 | ?> |
---|