| 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 | |
|---|
| 20 | $page['max_width'] = 800; |
|---|
| 21 | $page['max_height'] = 800; |
|---|
| 22 | |
|---|
| 23 | $page['thumb_width'] = 128; |
|---|
| 24 | $page['thumb_height'] = 96; |
|---|
| 25 | |
|---|
| 26 | $page['source_file'] = '/home/pierrick/public_html/test/resize/phpthumb/examples/statue_small.jpg'; |
|---|
| 27 | |
|---|
| 28 | function add_uploaded_file($source_filepath, $original_filename=null, $categories=null) |
|---|
| 29 | { |
|---|
| 30 | global $page; |
|---|
| 31 | |
|---|
| 32 | // current date |
|---|
| 33 | list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();')); |
|---|
| 34 | list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4); |
|---|
| 35 | |
|---|
| 36 | // upload directory hierarchy |
|---|
| 37 | $upload_dir = sprintf( |
|---|
| 38 | PHPWG_ROOT_PATH.'upload/%s/%s/%s', |
|---|
| 39 | $year, |
|---|
| 40 | $month, |
|---|
| 41 | $day |
|---|
| 42 | ); |
|---|
| 43 | |
|---|
| 44 | // compute file path |
|---|
| 45 | $md5sum = md5_file($source_filepath); |
|---|
| 46 | $date_string = preg_replace('/[^\d]/', '', $dbnow); |
|---|
| 47 | $random_string = substr($md5sum, 0, 8); |
|---|
| 48 | $filename_wo_ext = $date_string.'-'.$random_string; |
|---|
| 49 | $file_path = $upload_dir.'/'.$filename_wo_ext.'.jpg'; |
|---|
| 50 | |
|---|
| 51 | prepare_directory($upload_dir); |
|---|
| 52 | if (is_uploaded_file($source_filepath)) |
|---|
| 53 | { |
|---|
| 54 | move_uploaded_file($source_filepath, $file_path); |
|---|
| 55 | } |
|---|
| 56 | else |
|---|
| 57 | { |
|---|
| 58 | copy($source_filepath, $file_path); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | if (need_resize($file_path, $page['max_width'], $page['max_height'])) |
|---|
| 62 | { |
|---|
| 63 | $high_path = file_path_for_type($file_path, 'high'); |
|---|
| 64 | $high_dir = dirname($high_path); |
|---|
| 65 | prepare_directory($high_dir); |
|---|
| 66 | |
|---|
| 67 | rename($file_path, $high_path); |
|---|
| 68 | $high_infos = pwg_image_infos($high_path); |
|---|
| 69 | |
|---|
| 70 | pwg_image_resize($high_path, $file_path, $page['max_width'], $page['max_height']); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | $file_infos = pwg_image_infos($file_path); |
|---|
| 74 | |
|---|
| 75 | $thumb_path = file_path_for_type($file_path, 'thumb'); |
|---|
| 76 | $thumb_dir = dirname($thumb_path); |
|---|
| 77 | prepare_directory($thumb_dir); |
|---|
| 78 | pwg_image_resize($file_path, $thumb_path, $page['thumb_width'], $page['thumb_height']); |
|---|
| 79 | $thumb_infos = pwg_image_infos($thumb_path); |
|---|
| 80 | |
|---|
| 81 | // database registration |
|---|
| 82 | $insert = array( |
|---|
| 83 | 'file' => isset($original_filename) ? $original_filename : basename($file_path), |
|---|
| 84 | 'date_available' => $dbnow, |
|---|
| 85 | 'tn_ext' => 'jpg', |
|---|
| 86 | 'path' => $file_path, |
|---|
| 87 | 'filesize' => $file_infos['filesize'], |
|---|
| 88 | 'width' => $file_infos['width'], |
|---|
| 89 | 'height' => $file_infos['height'], |
|---|
| 90 | 'md5sum' => $md5sum, |
|---|
| 91 | ); |
|---|
| 92 | |
|---|
| 93 | if (isset($high_infos)) |
|---|
| 94 | { |
|---|
| 95 | $insert['has_high'] = 'true'; |
|---|
| 96 | $insert['high_filesize'] = $high_infos['filesize']; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | mass_inserts( |
|---|
| 100 | IMAGES_TABLE, |
|---|
| 101 | array_keys($insert), |
|---|
| 102 | array($insert) |
|---|
| 103 | ); |
|---|
| 104 | |
|---|
| 105 | $image_id = mysql_insert_id(); |
|---|
| 106 | |
|---|
| 107 | if (isset($categories) and count($categories) > 0) |
|---|
| 108 | { |
|---|
| 109 | associate_images_to_categories( |
|---|
| 110 | array($image_id), |
|---|
| 111 | $categories |
|---|
| 112 | ); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | // update metadata from the uploaded file (exif/iptc) |
|---|
| 116 | update_metadata(array($image_id=>$file_path)); |
|---|
| 117 | |
|---|
| 118 | invalidate_user_cache(); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | function prepare_directory($directory) |
|---|
| 122 | { |
|---|
| 123 | if (!is_dir($directory)) { |
|---|
| 124 | umask(0000); |
|---|
| 125 | $recursive = true; |
|---|
| 126 | if (!@mkdir($directory, 0777, $recursive)) |
|---|
| 127 | { |
|---|
| 128 | die('[prepare_directory] cannot create directory "'.$directory.'"'); |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | if (!is_writable($directory)) |
|---|
| 133 | { |
|---|
| 134 | // last chance to make the directory writable |
|---|
| 135 | @chmod($directory, 0777); |
|---|
| 136 | |
|---|
| 137 | if (!is_writable($directory)) |
|---|
| 138 | { |
|---|
| 139 | die('[prepare_directory] directory "'.$directory.'" has no write access'); |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | secure_directory($directory); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | function need_resize($image_filepath, $max_width, $max_height) |
|---|
| 147 | { |
|---|
| 148 | list($width, $height) = getimagesize($image_filepath); |
|---|
| 149 | |
|---|
| 150 | if ($width > $max_width or $height > $max_height) |
|---|
| 151 | { |
|---|
| 152 | return true; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | return false; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | function pwg_image_resize($source_filepath, $destination_filepath, $max_width, $max_height) |
|---|
| 159 | { |
|---|
| 160 | if (!function_exists('gd_info')) |
|---|
| 161 | { |
|---|
| 162 | return false; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | // extension of the picture filename |
|---|
| 166 | $extension = strtolower(get_extension($source_filepath)); |
|---|
| 167 | |
|---|
| 168 | $source_image = null; |
|---|
| 169 | if (in_array($extension, array('jpg', 'jpeg'))) |
|---|
| 170 | { |
|---|
| 171 | $source_image = @imagecreatefromjpeg($source_filepath); |
|---|
| 172 | } |
|---|
| 173 | else if ($extension == 'png') |
|---|
| 174 | { |
|---|
| 175 | $source_image = @imagecreatefrompng($source_filepath); |
|---|
| 176 | } |
|---|
| 177 | else |
|---|
| 178 | { |
|---|
| 179 | die('unsupported file extension'); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | // width/height |
|---|
| 183 | $source_width = imagesx($source_image); |
|---|
| 184 | $source_height = imagesy($source_image); |
|---|
| 185 | |
|---|
| 186 | $ratio_width = $source_width / $max_width; |
|---|
| 187 | $ratio_height = $source_height / $max_height; |
|---|
| 188 | |
|---|
| 189 | // maximal size exceeded ? |
|---|
| 190 | if ($ratio_width > 1 or $ratio_height > 1) |
|---|
| 191 | { |
|---|
| 192 | if ($ratio_width < $ratio_height) |
|---|
| 193 | { |
|---|
| 194 | $destination_width = ceil($source_width / $ratio_height); |
|---|
| 195 | $destination_height = $max_height; |
|---|
| 196 | } |
|---|
| 197 | else |
|---|
| 198 | { |
|---|
| 199 | $destination_width = $max_width; |
|---|
| 200 | $destination_height = ceil($source_height / $ratio_width); |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | else |
|---|
| 204 | { |
|---|
| 205 | // the image doesn't need any resize! We just copy it to the destination |
|---|
| 206 | copy($source_filepath, $destination_filepath); |
|---|
| 207 | return true; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | $destination_image = imagecreatetruecolor($destination_width, $destination_height); |
|---|
| 211 | |
|---|
| 212 | imagecopyresampled( |
|---|
| 213 | $destination_image, |
|---|
| 214 | $source_image, |
|---|
| 215 | 0, |
|---|
| 216 | 0, |
|---|
| 217 | 0, |
|---|
| 218 | 0, |
|---|
| 219 | $destination_width, |
|---|
| 220 | $destination_height, |
|---|
| 221 | $source_width, |
|---|
| 222 | $source_height |
|---|
| 223 | ); |
|---|
| 224 | |
|---|
| 225 | imagejpeg($destination_image, $destination_filepath, 95); |
|---|
| 226 | // freeing memory ressources |
|---|
| 227 | imagedestroy($source_image); |
|---|
| 228 | imagedestroy($destination_image); |
|---|
| 229 | |
|---|
| 230 | // everything should be OK if we are here! |
|---|
| 231 | return true; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | function pwg_image_infos($path) |
|---|
| 235 | { |
|---|
| 236 | list($width, $height) = getimagesize($path); |
|---|
| 237 | $filesize = floor(filesize($path)/1024); |
|---|
| 238 | |
|---|
| 239 | return array( |
|---|
| 240 | 'width' => $width, |
|---|
| 241 | 'height' => $height, |
|---|
| 242 | 'filesize' => $filesize, |
|---|
| 243 | ); |
|---|
| 244 | } |
|---|
| 245 | ?> |
|---|