[2] | 1 | <?php |
---|
[362] | 2 | // +-----------------------------------------------------------------------+ |
---|
[2297] | 3 | // | Piwigo - a PHP based picture gallery | |
---|
| 4 | // +-----------------------------------------------------------------------+ |
---|
[5196] | 5 | // | Copyright(C) 2008-2010 Piwigo Team http://piwigo.org | |
---|
[2297] | 6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
| 7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
| 8 | // +-----------------------------------------------------------------------+ |
---|
| 9 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 10 | // | it under the terms of the GNU General Public License as published by | |
---|
| 11 | // | the Free Software Foundation | |
---|
| 12 | // | | |
---|
| 13 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 16 | // | General Public License for more details. | |
---|
| 17 | // | | |
---|
| 18 | // | You should have received a copy of the GNU General Public License | |
---|
| 19 | // | along with this program; if not, write to the Free Software | |
---|
| 20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 21 | // | USA. | |
---|
| 22 | // +-----------------------------------------------------------------------+ |
---|
[1072] | 23 | |
---|
| 24 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 25 | |
---|
| 26 | // +-----------------------------------------------------------------------+ |
---|
| 27 | // | Check Access and exit when user status is not ok | |
---|
| 28 | // +-----------------------------------------------------------------------+ |
---|
| 29 | check_status(ACCESS_ADMINISTRATOR); |
---|
| 30 | |
---|
[26] | 31 | //------------------------------------------------------------------- functions |
---|
[695] | 32 | // RatioResizeImg creates a new picture (a thumbnail since it is supposed to |
---|
| 33 | // be smaller than original picture !) in the sub directory named |
---|
| 34 | // "thumbnail". |
---|
[5957] | 35 | function RatioResizeImg($info, $path, $newWidth, $newHeight, $tn_ext) |
---|
[2] | 36 | { |
---|
[792] | 37 | global $conf, $lang, $page; |
---|
[2] | 38 | |
---|
[5957] | 39 | if ($info !== false) |
---|
| 40 | { |
---|
| 41 | //someone hooked us - so we skip |
---|
| 42 | return $info; |
---|
| 43 | } |
---|
| 44 | |
---|
[1379] | 45 | if (!function_exists('gd_info')) |
---|
| 46 | { |
---|
| 47 | return; |
---|
| 48 | } |
---|
| 49 | |
---|
[695] | 50 | $filename = basename($path); |
---|
| 51 | $dirname = dirname($path); |
---|
| 52 | |
---|
| 53 | // extension of the picture filename |
---|
| 54 | $extension = get_extension($filename); |
---|
| 55 | |
---|
[1635] | 56 | if (in_array($extension, array('jpg', 'JPG', 'jpeg', 'JPEG'))) |
---|
[2] | 57 | { |
---|
[695] | 58 | $srcImage = @imagecreatefromjpeg($path); |
---|
[2] | 59 | } |
---|
[695] | 60 | else if ($extension == 'png' or $extension == 'PNG') |
---|
[2] | 61 | { |
---|
[695] | 62 | $srcImage = @imagecreatefrompng($path); |
---|
[2] | 63 | } |
---|
[695] | 64 | else |
---|
[2] | 65 | { |
---|
[695] | 66 | unset($extension); |
---|
[2] | 67 | } |
---|
[1631] | 68 | |
---|
[26] | 69 | if ( isset( $srcImage ) ) |
---|
[2] | 70 | { |
---|
[26] | 71 | // width/height |
---|
| 72 | $srcWidth = imagesx( $srcImage ); |
---|
| 73 | $srcHeight = imagesy( $srcImage ); |
---|
| 74 | $ratioWidth = $srcWidth/$newWidth; |
---|
[2] | 75 | $ratioHeight = $srcHeight/$newHeight; |
---|
[26] | 76 | |
---|
| 77 | // maximal size exceeded ? |
---|
| 78 | if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) |
---|
[2] | 79 | { |
---|
[26] | 80 | if ( $ratioWidth < $ratioHeight) |
---|
[2] | 81 | { |
---|
| 82 | $destWidth = $srcWidth/$ratioHeight; |
---|
| 83 | $destHeight = $newHeight; |
---|
| 84 | } |
---|
| 85 | else |
---|
| 86 | { |
---|
| 87 | $destWidth = $newWidth; |
---|
| 88 | $destHeight = $srcHeight/$ratioWidth; |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | else |
---|
| 92 | { |
---|
| 93 | $destWidth = $srcWidth; |
---|
| 94 | $destHeight = $srcHeight; |
---|
| 95 | } |
---|
[26] | 96 | // according to the GD version installed on the server |
---|
| 97 | if ( $_POST['gd'] == 2 ) |
---|
[2] | 98 | { |
---|
[26] | 99 | // GD 2.0 or more recent -> good results (but slower) |
---|
[2] | 100 | $destImage = imagecreatetruecolor( $destWidth, $destHeight); |
---|
[26] | 101 | imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0, |
---|
| 102 | $destWidth,$destHeight,$srcWidth,$srcHeight ); |
---|
[2] | 103 | } |
---|
| 104 | else |
---|
| 105 | { |
---|
[26] | 106 | // GD prior to version 2 -> pretty bad results :-/ (but fast) |
---|
[2] | 107 | $destImage = imagecreate( $destWidth, $destHeight); |
---|
[26] | 108 | imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0, |
---|
| 109 | $destWidth,$destHeight,$srcWidth,$srcHeight ); |
---|
[2] | 110 | } |
---|
[1631] | 111 | |
---|
| 112 | if (($tndir = mkget_thumbnail_dir($dirname, $page['errors'])) == false) |
---|
[2] | 113 | { |
---|
[1631] | 114 | return false; |
---|
[2] | 115 | } |
---|
[1631] | 116 | |
---|
[695] | 117 | $dest_file = $tndir.'/'.$conf['prefix_thumbnail']; |
---|
| 118 | $dest_file.= get_filename_wo_extension($filename); |
---|
[26] | 119 | $dest_file.= '.'.$tn_ext; |
---|
[695] | 120 | |
---|
[26] | 121 | // creation and backup of final picture |
---|
[695] | 122 | if (!is_writable($tndir)) |
---|
| 123 | { |
---|
[5021] | 124 | array_push($page['errors'], '['.$tndir.'] : '.l10n('no write access')); |
---|
[695] | 125 | return false; |
---|
| 126 | } |
---|
[3271] | 127 | imagejpeg($destImage, $dest_file, $conf['tn_compression_level']); |
---|
[26] | 128 | // freeing memory ressources |
---|
[2] | 129 | imagedestroy( $srcImage ); |
---|
| 130 | imagedestroy( $destImage ); |
---|
[695] | 131 | |
---|
| 132 | list($tn_width, $tn_height) = getimagesize($dest_file); |
---|
| 133 | $tn_size = floor(filesize($dest_file) / 1024).' KB'; |
---|
| 134 | |
---|
| 135 | $info = array( 'path' => $path, |
---|
[26] | 136 | 'tn_file' => $dest_file, |
---|
| 137 | 'tn_width' => $tn_width, |
---|
| 138 | 'tn_height' => $tn_height, |
---|
| 139 | 'tn_size' => $tn_size ); |
---|
[2] | 140 | return $info; |
---|
| 141 | } |
---|
[26] | 142 | // error |
---|
[2] | 143 | else |
---|
| 144 | { |
---|
[8727] | 145 | echo l10n('Photo unreachable or no support')." "; |
---|
| 146 | if ( isset( $extension ) ) |
---|
[2] | 147 | { |
---|
[5021] | 148 | echo l10n('for the file format').' '.$extension; |
---|
[2] | 149 | } |
---|
| 150 | else |
---|
| 151 | { |
---|
[5021] | 152 | echo l10n('for this file format'); |
---|
[2] | 153 | } |
---|
| 154 | exit(); |
---|
| 155 | } |
---|
| 156 | } |
---|
[26] | 157 | |
---|
[393] | 158 | $pictures = array(); |
---|
| 159 | $stats = array(); |
---|
[2428] | 160 | |
---|
| 161 | if (!function_exists('gd_info')) |
---|
| 162 | { |
---|
| 163 | array_push($page['errors'], l10n('GD library is missing')); |
---|
| 164 | } |
---|
| 165 | |
---|
[5957] | 166 | // add default event handler for thumbnail resize |
---|
| 167 | add_event_handler('thumbnail_resize', 'RatioResizeImg', EVENT_HANDLER_PRIORITY_NEUTRAL, 5); |
---|
| 168 | |
---|
[695] | 169 | // +-----------------------------------------------------------------------+ |
---|
| 170 | // | template initialization | |
---|
| 171 | // +-----------------------------------------------------------------------+ |
---|
[2530] | 172 | $template->set_filenames( array('thumbnail'=>'thumbnail.tpl') ); |
---|
[393] | 173 | |
---|
[5920] | 174 | $template->assign( |
---|
| 175 | array('U_HELP' => get_root_url().'admin/popuphelp.php?page=thumbnail') |
---|
| 176 | ); |
---|
[695] | 177 | // +-----------------------------------------------------------------------+ |
---|
| 178 | // | search pictures without thumbnails | |
---|
| 179 | // +-----------------------------------------------------------------------+ |
---|
| 180 | $wo_thumbnails = array(); |
---|
| 181 | $thumbnalized = array(); |
---|
[393] | 182 | |
---|
[695] | 183 | // what is the directory to search in ? |
---|
| 184 | $query = ' |
---|
[1814] | 185 | SELECT galleries_url FROM '.SITES_TABLE.' |
---|
[6550] | 186 | WHERE galleries_url NOT LIKE \'http://%\' |
---|
[695] | 187 | ;'; |
---|
[1814] | 188 | $result = pwg_query($query); |
---|
[4325] | 189 | while ( $row=pwg_db_fetch_assoc($result) ) |
---|
[1814] | 190 | { |
---|
| 191 | $basedir = preg_replace('#/*$#', '', $row['galleries_url']); |
---|
| 192 | $fs = get_fs($basedir); |
---|
[695] | 193 | |
---|
[1814] | 194 | // because isset is one hundred time faster than in_array |
---|
| 195 | $fs['thumbnails'] = array_flip($fs['thumbnails']); |
---|
[695] | 196 | |
---|
[1814] | 197 | foreach ($fs['elements'] as $path) |
---|
[2] | 198 | { |
---|
[1814] | 199 | // only pictures need thumbnails |
---|
| 200 | if (in_array(get_extension($path), $conf['picture_ext'])) |
---|
[703] | 201 | { |
---|
[1814] | 202 | $dirname = dirname($path); |
---|
| 203 | $filename = basename($path); |
---|
| 204 | |
---|
| 205 | // only files matching the authorized filename pattern can be considered |
---|
| 206 | // as "without thumbnail" |
---|
| 207 | if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename)) |
---|
[695] | 208 | { |
---|
[1814] | 209 | continue; |
---|
[695] | 210 | } |
---|
[1814] | 211 | |
---|
| 212 | // searching the element |
---|
| 213 | $filename_wo_ext = get_filename_wo_extension($filename); |
---|
| 214 | $tn_ext = ''; |
---|
[3720] | 215 | $base_test = $dirname.'/'.$conf['dir_thumbnail'].'/'; |
---|
[1814] | 216 | $base_test.= $conf['prefix_thumbnail'].$filename_wo_ext.'.'; |
---|
| 217 | foreach ($conf['picture_ext'] as $ext) |
---|
| 218 | { |
---|
| 219 | if (isset($fs['thumbnails'][$base_test.$ext])) |
---|
| 220 | { |
---|
| 221 | $tn_ext = $ext; |
---|
| 222 | break; |
---|
| 223 | } |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | if (empty($tn_ext)) |
---|
| 227 | { |
---|
| 228 | array_push($wo_thumbnails, $path); |
---|
| 229 | } |
---|
[695] | 230 | } |
---|
[1814] | 231 | } // next element |
---|
| 232 | } // next site id |
---|
[695] | 233 | // +-----------------------------------------------------------------------+ |
---|
| 234 | // | thumbnails creation | |
---|
| 235 | // +-----------------------------------------------------------------------+ |
---|
| 236 | if (isset($_POST['submit'])) |
---|
[393] | 237 | { |
---|
| 238 | $times = array(); |
---|
[695] | 239 | $infos = array(); |
---|
| 240 | |
---|
| 241 | // checking criteria |
---|
[3747] | 242 | if (!preg_match('/^[0-9]{2,3}$/', $_POST['width']) or $_POST['width'] < 10) |
---|
[665] | 243 | { |
---|
[5021] | 244 | array_push($page['errors'], l10n('width must be a number superior to').' 10'); |
---|
[393] | 245 | } |
---|
[3747] | 246 | if (!preg_match('/^[0-9]{2,3}$/', $_POST['height']) or $_POST['height'] < 10) |
---|
[2] | 247 | { |
---|
[5021] | 248 | array_push($page['errors'], l10n('height must be a number superior to').' 10'); |
---|
[393] | 249 | } |
---|
[665] | 250 | |
---|
[695] | 251 | // picture miniaturization |
---|
[792] | 252 | if (count($page['errors']) == 0) |
---|
[393] | 253 | { |
---|
[695] | 254 | $num = 1; |
---|
| 255 | foreach ($wo_thumbnails as $path) |
---|
[665] | 256 | { |
---|
[695] | 257 | if (is_numeric($_POST['n']) and $num > $_POST['n']) |
---|
| 258 | { |
---|
| 259 | break; |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | $starttime = get_moment(); |
---|
[5957] | 263 | if ($info = trigger_event('thumbnail_resize', |
---|
| 264 | false, |
---|
| 265 | $path, |
---|
| 266 | $_POST['width'], |
---|
| 267 | $_POST['height'], |
---|
| 268 | 'jpg' |
---|
| 269 | ) |
---|
| 270 | ) |
---|
[695] | 271 | { |
---|
| 272 | $endtime = get_moment(); |
---|
| 273 | $info['time'] = ($endtime - $starttime) * 1000; |
---|
| 274 | array_push($infos, $info); |
---|
| 275 | array_push($times, $info['time']); |
---|
| 276 | array_push($thumbnalized, $path); |
---|
| 277 | $num++; |
---|
| 278 | } |
---|
| 279 | else |
---|
| 280 | { |
---|
| 281 | break; |
---|
| 282 | } |
---|
[2] | 283 | } |
---|
[695] | 284 | |
---|
| 285 | if (count($infos) > 0) |
---|
[665] | 286 | { |
---|
[695] | 287 | $sum = array_sum($times); |
---|
| 288 | $average = $sum / count($times); |
---|
| 289 | sort($times, SORT_NUMERIC); |
---|
| 290 | $max = array_pop($times); |
---|
| 291 | if (count($thumbnalized) == 1) |
---|
| 292 | { |
---|
| 293 | $min = $max; |
---|
| 294 | } |
---|
| 295 | else |
---|
| 296 | { |
---|
| 297 | $min = array_shift($times); |
---|
| 298 | } |
---|
| 299 | |
---|
[2250] | 300 | $tpl_var = |
---|
[695] | 301 | array( |
---|
| 302 | 'TN_NB'=>count($infos), |
---|
| 303 | 'TN_TOTAL'=>number_format($sum, 2, '.', ' ').' ms', |
---|
| 304 | 'TN_MAX'=>number_format($max, 2, '.', ' ').' ms', |
---|
| 305 | 'TN_MIN'=>number_format($min, 2, '.', ' ').' ms', |
---|
[2250] | 306 | 'TN_AVERAGE'=>number_format($average, 2, '.', ' ').' ms', |
---|
| 307 | 'elements' => array() |
---|
| 308 | ); |
---|
[695] | 309 | |
---|
| 310 | foreach ($infos as $i => $info) |
---|
| 311 | { |
---|
[2250] | 312 | $tpl_var['elements'][] = |
---|
[695] | 313 | array( |
---|
| 314 | 'PATH'=>$info['path'], |
---|
| 315 | 'TN_FILE_IMG'=>$info['tn_file'], |
---|
| 316 | 'TN_FILESIZE_IMG'=>$info['tn_size'], |
---|
| 317 | 'TN_WIDTH_IMG'=>$info['tn_width'], |
---|
| 318 | 'TN_HEIGHT_IMG'=>$info['tn_height'], |
---|
| 319 | 'GEN_TIME'=>number_format($info['time'], 2, '.', ' ').' ms', |
---|
[2250] | 320 | ); |
---|
[695] | 321 | } |
---|
[2250] | 322 | $template->assign('results', $tpl_var); |
---|
[665] | 323 | } |
---|
[2] | 324 | } |
---|
[665] | 325 | } |
---|
[695] | 326 | // +-----------------------------------------------------------------------+ |
---|
| 327 | // | form & pictures without thumbnails display | |
---|
| 328 | // +-----------------------------------------------------------------------+ |
---|
| 329 | $remainings = array_diff($wo_thumbnails, $thumbnalized); |
---|
| 330 | |
---|
| 331 | if (count($remainings) > 0) |
---|
| 332 | { |
---|
[2250] | 333 | $form_url = get_root_url().'admin.php?page=thumbnail'; |
---|
[695] | 334 | $gd = !empty($_POST['gd']) ? $_POST['gd'] : 2; |
---|
| 335 | $width = !empty($_POST['width']) ? $_POST['width'] : $conf['tn_width']; |
---|
| 336 | $height = !empty($_POST['height']) ? $_POST['height'] : $conf['tn_height']; |
---|
| 337 | $n = !empty($_POST['n']) ? $_POST['n'] : 5; |
---|
| 338 | |
---|
[2250] | 339 | $template->assign( |
---|
[665] | 340 | 'params', |
---|
| 341 | array( |
---|
[2250] | 342 | 'F_ACTION'=> $form_url, |
---|
| 343 | 'GD_SELECTED' => $gd, |
---|
| 344 | 'N_SELECTED' => $n, |
---|
[665] | 345 | 'WIDTH_TN'=>$width, |
---|
| 346 | 'HEIGHT_TN'=>$height |
---|
| 347 | )); |
---|
[695] | 348 | |
---|
[2250] | 349 | $template->assign( |
---|
| 350 | 'TOTAL_NB_REMAINING', |
---|
| 351 | count($remainings)); |
---|
[393] | 352 | |
---|
[695] | 353 | foreach ($remainings as $path) |
---|
[2] | 354 | { |
---|
[695] | 355 | list($width, $height) = getimagesize($path); |
---|
| 356 | $size = floor(filesize($path) / 1024).' KB'; |
---|
| 357 | |
---|
[2250] | 358 | $template->append( |
---|
| 359 | 'remainings', |
---|
[665] | 360 | array( |
---|
[695] | 361 | 'PATH'=>$path, |
---|
| 362 | 'FILESIZE_IMG'=>$size, |
---|
| 363 | 'WIDTH_IMG'=>$width, |
---|
| 364 | 'HEIGHT_IMG'=>$height, |
---|
[665] | 365 | )); |
---|
| 366 | } |
---|
[2] | 367 | } |
---|
[2250] | 368 | |
---|
[695] | 369 | // +-----------------------------------------------------------------------+ |
---|
| 370 | // | return to admin | |
---|
| 371 | // +-----------------------------------------------------------------------+ |
---|
[393] | 372 | $template->assign_var_from_handle('ADMIN_CONTENT', 'thumbnail'); |
---|
[362] | 373 | ?> |
---|