[2] | 1 | <?php |
---|
[362] | 2 | // +-----------------------------------------------------------------------+ |
---|
[593] | 3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
| 4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
[675] | 5 | // | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net | |
---|
[362] | 6 | // +-----------------------------------------------------------------------+ |
---|
[593] | 7 | // | branch : BSF (Best So Far) |
---|
[362] | 8 | // | file : $RCSfile$ |
---|
| 9 | // | last update : $Date: 2006-06-22 19:39:54 +0000 (Thu, 22 Jun 2006) $ |
---|
| 10 | // | last modifier : $Author: nikrou $ |
---|
| 11 | // | revision : $Revision: 1378 $ |
---|
| 12 | // +-----------------------------------------------------------------------+ |
---|
| 13 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 14 | // | it under the terms of the GNU General Public License as published by | |
---|
| 15 | // | the Free Software Foundation | |
---|
| 16 | // | | |
---|
| 17 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 18 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 19 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 20 | // | General Public License for more details. | |
---|
| 21 | // | | |
---|
| 22 | // | You should have received a copy of the GNU General Public License | |
---|
| 23 | // | along with this program; if not, write to the Free Software | |
---|
| 24 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 25 | // | USA. | |
---|
| 26 | // +-----------------------------------------------------------------------+ |
---|
[1072] | 27 | |
---|
| 28 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 29 | |
---|
| 30 | // +-----------------------------------------------------------------------+ |
---|
| 31 | // | Check Access and exit when user status is not ok | |
---|
| 32 | // +-----------------------------------------------------------------------+ |
---|
| 33 | check_status(ACCESS_ADMINISTRATOR); |
---|
| 34 | |
---|
[26] | 35 | //------------------------------------------------------------------- functions |
---|
[695] | 36 | // RatioResizeImg creates a new picture (a thumbnail since it is supposed to |
---|
| 37 | // be smaller than original picture !) in the sub directory named |
---|
| 38 | // "thumbnail". |
---|
| 39 | function RatioResizeImg($path, $newWidth, $newHeight, $tn_ext) |
---|
[2] | 40 | { |
---|
[792] | 41 | global $conf, $lang, $page; |
---|
[2] | 42 | |
---|
[1378] | 43 | if (!function_exists('gd_info')) |
---|
| 44 | { |
---|
| 45 | return; |
---|
| 46 | } |
---|
| 47 | |
---|
[695] | 48 | $filename = basename($path); |
---|
| 49 | $dirname = dirname($path); |
---|
| 50 | |
---|
| 51 | // extension of the picture filename |
---|
| 52 | $extension = get_extension($filename); |
---|
| 53 | |
---|
| 54 | if ($extension == 'jpg' or $extension == 'JPG') |
---|
[2] | 55 | { |
---|
[695] | 56 | $srcImage = @imagecreatefromjpeg($path); |
---|
[2] | 57 | } |
---|
[695] | 58 | else if ($extension == 'png' or $extension == 'PNG') |
---|
[2] | 59 | { |
---|
[695] | 60 | $srcImage = @imagecreatefrompng($path); |
---|
[2] | 61 | } |
---|
[695] | 62 | else |
---|
[2] | 63 | { |
---|
[695] | 64 | unset($extension); |
---|
[2] | 65 | } |
---|
| 66 | |
---|
[26] | 67 | if ( isset( $srcImage ) ) |
---|
[2] | 68 | { |
---|
[26] | 69 | // width/height |
---|
| 70 | $srcWidth = imagesx( $srcImage ); |
---|
| 71 | $srcHeight = imagesy( $srcImage ); |
---|
| 72 | $ratioWidth = $srcWidth/$newWidth; |
---|
[2] | 73 | $ratioHeight = $srcHeight/$newHeight; |
---|
[26] | 74 | |
---|
| 75 | // maximal size exceeded ? |
---|
| 76 | if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) |
---|
[2] | 77 | { |
---|
[26] | 78 | if ( $ratioWidth < $ratioHeight) |
---|
[2] | 79 | { |
---|
| 80 | $destWidth = $srcWidth/$ratioHeight; |
---|
| 81 | $destHeight = $newHeight; |
---|
| 82 | } |
---|
| 83 | else |
---|
| 84 | { |
---|
| 85 | $destWidth = $newWidth; |
---|
| 86 | $destHeight = $srcHeight/$ratioWidth; |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | else |
---|
| 90 | { |
---|
| 91 | $destWidth = $srcWidth; |
---|
| 92 | $destHeight = $srcHeight; |
---|
| 93 | } |
---|
[26] | 94 | // according to the GD version installed on the server |
---|
| 95 | if ( $_POST['gd'] == 2 ) |
---|
[2] | 96 | { |
---|
[26] | 97 | // GD 2.0 or more recent -> good results (but slower) |
---|
[2] | 98 | $destImage = imagecreatetruecolor( $destWidth, $destHeight); |
---|
[26] | 99 | imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0, |
---|
| 100 | $destWidth,$destHeight,$srcWidth,$srcHeight ); |
---|
[2] | 101 | } |
---|
| 102 | else |
---|
| 103 | { |
---|
[26] | 104 | // GD prior to version 2 -> pretty bad results :-/ (but fast) |
---|
[2] | 105 | $destImage = imagecreate( $destWidth, $destHeight); |
---|
[26] | 106 | imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0, |
---|
| 107 | $destWidth,$destHeight,$srcWidth,$srcHeight ); |
---|
[2] | 108 | } |
---|
[695] | 109 | |
---|
| 110 | $tndir = $dirname.'/thumbnail'; |
---|
| 111 | if (!is_dir($tndir)) |
---|
[2] | 112 | { |
---|
[695] | 113 | if (!is_writable($dirname)) |
---|
| 114 | { |
---|
[792] | 115 | array_push($page['errors'], |
---|
| 116 | '['.$dirname.'] : '.$lang['no_write_access']); |
---|
[695] | 117 | return false; |
---|
| 118 | } |
---|
| 119 | umask(0000); |
---|
| 120 | mkdir($tndir, 0777); |
---|
[2] | 121 | } |
---|
[695] | 122 | |
---|
| 123 | $dest_file = $tndir.'/'.$conf['prefix_thumbnail']; |
---|
| 124 | $dest_file.= get_filename_wo_extension($filename); |
---|
[26] | 125 | $dest_file.= '.'.$tn_ext; |
---|
[695] | 126 | |
---|
[26] | 127 | // creation and backup of final picture |
---|
[695] | 128 | if (!is_writable($tndir)) |
---|
| 129 | { |
---|
[792] | 130 | array_push($page['errors'], '['.$tndir.'] : '.$lang['no_write_access']); |
---|
[695] | 131 | return false; |
---|
| 132 | } |
---|
| 133 | imagejpeg($destImage, $dest_file); |
---|
[26] | 134 | // freeing memory ressources |
---|
[2] | 135 | imagedestroy( $srcImage ); |
---|
| 136 | imagedestroy( $destImage ); |
---|
[695] | 137 | |
---|
| 138 | list($tn_width, $tn_height) = getimagesize($dest_file); |
---|
| 139 | $tn_size = floor(filesize($dest_file) / 1024).' KB'; |
---|
| 140 | |
---|
| 141 | $info = array( 'path' => $path, |
---|
[26] | 142 | 'tn_file' => $dest_file, |
---|
| 143 | 'tn_width' => $tn_width, |
---|
| 144 | 'tn_height' => $tn_height, |
---|
| 145 | 'tn_size' => $tn_size ); |
---|
[2] | 146 | return $info; |
---|
| 147 | } |
---|
[26] | 148 | // error |
---|
[2] | 149 | else |
---|
| 150 | { |
---|
| 151 | echo $lang['tn_no_support']." "; |
---|
[26] | 152 | if ( isset( $extenstion ) ) |
---|
[2] | 153 | { |
---|
[26] | 154 | echo $lang['tn_format'].' '.$extension; |
---|
[2] | 155 | } |
---|
| 156 | else |
---|
| 157 | { |
---|
| 158 | echo $lang['tn_thisformat']; |
---|
| 159 | } |
---|
| 160 | exit(); |
---|
| 161 | } |
---|
| 162 | } |
---|
[26] | 163 | |
---|
[393] | 164 | $pictures = array(); |
---|
| 165 | $stats = array(); |
---|
[695] | 166 | // +-----------------------------------------------------------------------+ |
---|
| 167 | // | template initialization | |
---|
| 168 | // +-----------------------------------------------------------------------+ |
---|
[393] | 169 | $template->set_filenames( array('thumbnail'=>'admin/thumbnail.tpl') ); |
---|
| 170 | |
---|
| 171 | $template->assign_vars(array( |
---|
| 172 | 'L_THUMBNAIL_TITLE'=>$lang['tn_dirs_title'], |
---|
[695] | 173 | 'L_UNLINK'=>$lang['tn_no_missing'], |
---|
[665] | 174 | 'L_MISSING_THUMBNAILS'=>$lang['tn_dirs_alone'], |
---|
[393] | 175 | 'L_RESULTS'=>$lang['tn_results_title'], |
---|
[695] | 176 | 'L_PATH'=>$lang['path'], |
---|
[393] | 177 | 'L_FILESIZE'=>$lang['filesize'], |
---|
| 178 | 'L_GENERATED'=>$lang['tn_results_gen_time'], |
---|
| 179 | 'L_THUMBNAIL'=>$lang['thumbnail'], |
---|
| 180 | 'L_PARAMS'=>$lang['tn_params_title'], |
---|
| 181 | 'L_GD'=>$lang['tn_params_GD'], |
---|
| 182 | 'L_CREATE'=>$lang['tn_params_create'], |
---|
| 183 | 'L_SUBMIT'=>$lang['submit'], |
---|
| 184 | 'L_REMAINING'=>$lang['tn_alone_title'], |
---|
| 185 | 'L_TN_STATS'=>$lang['tn_stats'], |
---|
| 186 | 'L_TN_NB_STATS'=>$lang['tn_stats_nb'], |
---|
| 187 | 'L_TN_TOTAL'=>$lang['tn_stats_total'], |
---|
| 188 | 'L_TN_MAX'=>$lang['tn_stats_max'], |
---|
| 189 | 'L_TN_MIN'=>$lang['tn_stats_min'], |
---|
| 190 | 'L_TN_AVERAGE'=>$lang['tn_stats_mean'], |
---|
[716] | 191 | 'L_ALL'=>$lang['tn_all'], |
---|
[862] | 192 | |
---|
[1236] | 193 | 'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=thumbnail', |
---|
[393] | 194 | |
---|
| 195 | 'T_STYLE'=>$user['template'] |
---|
| 196 | )); |
---|
[695] | 197 | // +-----------------------------------------------------------------------+ |
---|
| 198 | // | search pictures without thumbnails | |
---|
| 199 | // +-----------------------------------------------------------------------+ |
---|
| 200 | $wo_thumbnails = array(); |
---|
| 201 | $thumbnalized = array(); |
---|
[393] | 202 | |
---|
[695] | 203 | // what is the directory to search in ? |
---|
| 204 | $query = ' |
---|
| 205 | SELECT galleries_url |
---|
| 206 | FROM '.SITES_TABLE.' |
---|
| 207 | WHERE id = 1 |
---|
| 208 | ;'; |
---|
| 209 | list($galleries_url) = mysql_fetch_array(pwg_query($query)); |
---|
| 210 | $basedir = preg_replace('#/*$#', '', $galleries_url); |
---|
| 211 | |
---|
| 212 | $fs = get_fs($basedir); |
---|
| 213 | // because isset is one hundred time faster than in_array |
---|
| 214 | $fs['thumbnails'] = array_flip($fs['thumbnails']); |
---|
| 215 | |
---|
| 216 | foreach ($fs['elements'] as $path) |
---|
[2] | 217 | { |
---|
[695] | 218 | // only pictures need thumbnails |
---|
| 219 | if (in_array(get_extension($path), $conf['picture_ext'])) |
---|
[2] | 220 | { |
---|
[695] | 221 | $dirname = dirname($path); |
---|
| 222 | $filename = basename($path); |
---|
[703] | 223 | |
---|
| 224 | // only files matching the authorized filename pattern can be considered |
---|
| 225 | // as "without thumbnail" |
---|
| 226 | if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename)) |
---|
| 227 | { |
---|
| 228 | continue; |
---|
| 229 | } |
---|
| 230 | |
---|
[695] | 231 | // searching the element |
---|
| 232 | $filename_wo_ext = get_filename_wo_extension($filename); |
---|
| 233 | $tn_ext = ''; |
---|
| 234 | $base_test = $dirname.'/thumbnail/'; |
---|
| 235 | $base_test.= $conf['prefix_thumbnail'].$filename_wo_ext.'.'; |
---|
| 236 | foreach ($conf['picture_ext'] as $ext) |
---|
| 237 | { |
---|
| 238 | if (isset($fs['thumbnails'][$base_test.$ext])) |
---|
| 239 | { |
---|
| 240 | $tn_ext = $ext; |
---|
| 241 | break; |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | if (empty($tn_ext)) |
---|
| 246 | { |
---|
| 247 | array_push($wo_thumbnails, $path); |
---|
| 248 | } |
---|
[2] | 249 | } |
---|
[393] | 250 | } |
---|
[695] | 251 | // +-----------------------------------------------------------------------+ |
---|
| 252 | // | thumbnails creation | |
---|
| 253 | // +-----------------------------------------------------------------------+ |
---|
| 254 | if (isset($_POST['submit'])) |
---|
[393] | 255 | { |
---|
| 256 | $times = array(); |
---|
[695] | 257 | $infos = array(); |
---|
| 258 | |
---|
| 259 | // checking criteria |
---|
| 260 | if (!ereg('^[0-9]{2,3}$', $_POST['width']) or $_POST['width'] < 10) |
---|
[665] | 261 | { |
---|
[792] | 262 | array_push($page['errors'], $lang['tn_err_width'].' 10'); |
---|
[393] | 263 | } |
---|
[695] | 264 | if (!ereg('^[0-9]{2,3}$', $_POST['height']) or $_POST['height'] < 10) |
---|
[2] | 265 | { |
---|
[792] | 266 | array_push($page['errors'], $lang['tn_err_height'].' 10'); |
---|
[393] | 267 | } |
---|
[665] | 268 | |
---|
[695] | 269 | // picture miniaturization |
---|
[792] | 270 | if (count($page['errors']) == 0) |
---|
[393] | 271 | { |
---|
[695] | 272 | $num = 1; |
---|
| 273 | foreach ($wo_thumbnails as $path) |
---|
[665] | 274 | { |
---|
[695] | 275 | if (is_numeric($_POST['n']) and $num > $_POST['n']) |
---|
| 276 | { |
---|
| 277 | break; |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | $starttime = get_moment(); |
---|
| 281 | if ($info = RatioResizeImg($path,$_POST['width'],$_POST['height'],'jpg')) |
---|
| 282 | { |
---|
| 283 | $endtime = get_moment(); |
---|
| 284 | $info['time'] = ($endtime - $starttime) * 1000; |
---|
| 285 | array_push($infos, $info); |
---|
| 286 | array_push($times, $info['time']); |
---|
| 287 | array_push($thumbnalized, $path); |
---|
| 288 | $num++; |
---|
| 289 | } |
---|
| 290 | else |
---|
| 291 | { |
---|
| 292 | break; |
---|
| 293 | } |
---|
[2] | 294 | } |
---|
[695] | 295 | |
---|
| 296 | if (count($infos) > 0) |
---|
[665] | 297 | { |
---|
[695] | 298 | $sum = array_sum($times); |
---|
| 299 | $average = $sum / count($times); |
---|
| 300 | sort($times, SORT_NUMERIC); |
---|
| 301 | $max = array_pop($times); |
---|
| 302 | if (count($thumbnalized) == 1) |
---|
| 303 | { |
---|
| 304 | $min = $max; |
---|
| 305 | } |
---|
| 306 | else |
---|
| 307 | { |
---|
| 308 | $min = array_shift($times); |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | $template->assign_block_vars( |
---|
| 312 | 'results', |
---|
| 313 | array( |
---|
| 314 | 'TN_NB'=>count($infos), |
---|
| 315 | 'TN_TOTAL'=>number_format($sum, 2, '.', ' ').' ms', |
---|
| 316 | 'TN_MAX'=>number_format($max, 2, '.', ' ').' ms', |
---|
| 317 | 'TN_MIN'=>number_format($min, 2, '.', ' ').' ms', |
---|
| 318 | 'TN_AVERAGE'=>number_format($average, 2, '.', ' ').' ms' |
---|
| 319 | )); |
---|
| 320 | |
---|
| 321 | foreach ($infos as $i => $info) |
---|
| 322 | { |
---|
| 323 | if ($info['time'] == $max) |
---|
| 324 | { |
---|
| 325 | $class = 'worst_gen_time'; |
---|
| 326 | } |
---|
| 327 | else if ($info['time'] == $min) |
---|
| 328 | { |
---|
| 329 | $class = 'best_gen_time'; |
---|
| 330 | } |
---|
| 331 | else |
---|
| 332 | { |
---|
| 333 | $class = ''; |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | $template->assign_block_vars( |
---|
| 337 | 'results.picture', |
---|
| 338 | array( |
---|
| 339 | 'PATH'=>$info['path'], |
---|
| 340 | 'TN_FILE_IMG'=>$info['tn_file'], |
---|
| 341 | 'TN_FILESIZE_IMG'=>$info['tn_size'], |
---|
| 342 | 'TN_WIDTH_IMG'=>$info['tn_width'], |
---|
| 343 | 'TN_HEIGHT_IMG'=>$info['tn_height'], |
---|
| 344 | 'GEN_TIME'=>number_format($info['time'], 2, '.', ' ').' ms', |
---|
| 345 | |
---|
| 346 | 'T_CLASS'=>$class |
---|
| 347 | )); |
---|
| 348 | } |
---|
[665] | 349 | } |
---|
[2] | 350 | } |
---|
[665] | 351 | } |
---|
[695] | 352 | // +-----------------------------------------------------------------------+ |
---|
| 353 | // | form & pictures without thumbnails display | |
---|
| 354 | // +-----------------------------------------------------------------------+ |
---|
| 355 | $remainings = array_diff($wo_thumbnails, $thumbnalized); |
---|
| 356 | |
---|
| 357 | if (count($remainings) > 0) |
---|
| 358 | { |
---|
| 359 | $form_url = PHPWG_ROOT_PATH.'admin.php?page=thumbnail'; |
---|
| 360 | $gd = !empty($_POST['gd']) ? $_POST['gd'] : 2; |
---|
| 361 | $width = !empty($_POST['width']) ? $_POST['width'] : $conf['tn_width']; |
---|
| 362 | $height = !empty($_POST['height']) ? $_POST['height'] : $conf['tn_height']; |
---|
| 363 | $n = !empty($_POST['n']) ? $_POST['n'] : 5; |
---|
| 364 | |
---|
[665] | 365 | $gdlabel = 'GD'.$gd.'_CHECKED'; |
---|
[695] | 366 | $nlabel = 'n_'.$n.'_CHECKED'; |
---|
[665] | 367 | |
---|
| 368 | $template->assign_block_vars( |
---|
| 369 | 'params', |
---|
| 370 | array( |
---|
[1004] | 371 | 'F_ACTION'=>$form_url, |
---|
[665] | 372 | $gdlabel=>'checked="checked"', |
---|
[695] | 373 | $nlabel=>'checked="checked"', |
---|
[665] | 374 | 'WIDTH_TN'=>$width, |
---|
| 375 | 'HEIGHT_TN'=>$height |
---|
| 376 | )); |
---|
[695] | 377 | |
---|
[665] | 378 | $template->assign_block_vars( |
---|
| 379 | 'remainings', |
---|
[695] | 380 | array('TOTAL_IMG'=>count($remainings))); |
---|
[393] | 381 | |
---|
[695] | 382 | $num = 1; |
---|
| 383 | foreach ($remainings as $path) |
---|
[2] | 384 | { |
---|
[695] | 385 | $class = ($num % 2) ? 'row1' : 'row2'; |
---|
| 386 | list($width, $height) = getimagesize($path); |
---|
| 387 | $size = floor(filesize($path) / 1024).' KB'; |
---|
| 388 | |
---|
[665] | 389 | $template->assign_block_vars( |
---|
| 390 | 'remainings.remaining', |
---|
| 391 | array( |
---|
[695] | 392 | 'NB_IMG'=>($num), |
---|
| 393 | 'PATH'=>$path, |
---|
| 394 | 'FILESIZE_IMG'=>$size, |
---|
| 395 | 'WIDTH_IMG'=>$width, |
---|
| 396 | 'HEIGHT_IMG'=>$height, |
---|
| 397 | |
---|
[665] | 398 | 'T_CLASS'=>$class |
---|
| 399 | )); |
---|
[695] | 400 | |
---|
| 401 | $num++; |
---|
[665] | 402 | } |
---|
[2] | 403 | } |
---|
| 404 | else |
---|
| 405 | { |
---|
[695] | 406 | $template->assign_block_vars('warning', array()); |
---|
[2] | 407 | } |
---|
[695] | 408 | // +-----------------------------------------------------------------------+ |
---|
| 409 | // | return to admin | |
---|
| 410 | // +-----------------------------------------------------------------------+ |
---|
[393] | 411 | $template->assign_var_from_handle('ADMIN_CONTENT', 'thumbnail'); |
---|
[362] | 412 | ?> |
---|