[12796] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
| 3 | // | Piwigo - a PHP based photo gallery | |
---|
| 4 | // +-----------------------------------------------------------------------+ |
---|
| 5 | // | Copyright(C) 2008-2012 Piwigo Team http://piwigo.org | |
---|
| 6 | // +-----------------------------------------------------------------------+ |
---|
| 7 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 8 | // | it under the terms of the GNU General Public License as published by | |
---|
| 9 | // | the Free Software Foundation | |
---|
| 10 | // | | |
---|
| 11 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 12 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 13 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 14 | // | General Public License for more details. | |
---|
| 15 | // | | |
---|
| 16 | // | You should have received a copy of the GNU General Public License | |
---|
| 17 | // | along with this program; if not, write to the Free Software | |
---|
| 18 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 19 | // | USA. | |
---|
| 20 | // +-----------------------------------------------------------------------+ |
---|
| 21 | |
---|
[12798] | 22 | define('PHPWG_ROOT_PATH','./'); |
---|
[12796] | 23 | |
---|
| 24 | // fast bootstrap - no db connection |
---|
| 25 | include(PHPWG_ROOT_PATH . 'include/config_default.inc.php'); |
---|
| 26 | @include(PHPWG_ROOT_PATH. 'local/config/config.inc.php'); |
---|
| 27 | |
---|
| 28 | defined('PWG_LOCAL_DIR') or define('PWG_LOCAL_DIR', 'local/'); |
---|
[12802] | 29 | defined('PWG_DERIVATIVE_DIR') or define('PWG_DERIVATIVE_DIR', $conf['data_location'].'i/'); |
---|
[12796] | 30 | |
---|
[13736] | 31 | @include(PHPWG_ROOT_PATH.PWG_LOCAL_DIR .'config/database.inc.php'); |
---|
| 32 | |
---|
| 33 | |
---|
[12796] | 34 | function trigger_action() {} |
---|
| 35 | function get_extension( $filename ) |
---|
| 36 | { |
---|
| 37 | return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) ); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | function mkgetdir($dir) |
---|
| 41 | { |
---|
| 42 | if ( !is_dir($dir) ) |
---|
| 43 | { |
---|
| 44 | global $conf; |
---|
| 45 | if (substr(PHP_OS, 0, 3) == 'WIN') |
---|
| 46 | { |
---|
| 47 | $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir); |
---|
| 48 | } |
---|
| 49 | $umask = umask(0); |
---|
| 50 | $mkd = @mkdir($dir, $conf['chmod_value'], true); |
---|
| 51 | umask($umask); |
---|
| 52 | if ($mkd==false) |
---|
| 53 | { |
---|
| 54 | return false; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | $file = $dir.'/index.htm'; |
---|
| 58 | file_exists($file) or @file_put_contents( $file, 'Not allowed!' ); |
---|
| 59 | } |
---|
| 60 | if ( !is_writable($dir) ) |
---|
| 61 | { |
---|
| 62 | return false; |
---|
| 63 | } |
---|
| 64 | return true; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | // end fast bootstrap |
---|
| 68 | |
---|
[12908] | 69 | function ilog() |
---|
| 70 | { |
---|
[13021] | 71 | global $conf; |
---|
[12908] | 72 | if (!$conf['enable_i_log']) return; |
---|
[13021] | 73 | |
---|
[12958] | 74 | $line = date("c"); |
---|
[12908] | 75 | foreach( func_get_args() as $arg) |
---|
| 76 | { |
---|
[12958] | 77 | $line .= ' '; |
---|
[12908] | 78 | if (is_array($arg)) |
---|
| 79 | { |
---|
[12958] | 80 | $line .= implode(' ', $arg); |
---|
[12908] | 81 | } |
---|
| 82 | else |
---|
| 83 | { |
---|
[12958] | 84 | $line .= $arg; |
---|
[12908] | 85 | } |
---|
| 86 | } |
---|
[13021] | 87 | $file=PHPWG_ROOT_PATH.$conf['data_location'].'tmp/i.log'; |
---|
| 88 | if (false == file_put_contents($file, $line."\n", FILE_APPEND)) |
---|
| 89 | { |
---|
| 90 | mkgetdir(dirname($file)); |
---|
| 91 | } |
---|
[12908] | 92 | } |
---|
[12796] | 93 | |
---|
| 94 | function ierror($msg, $code) |
---|
| 95 | { |
---|
| 96 | if ($code==301 || $code==302) |
---|
| 97 | { |
---|
| 98 | if (ob_get_length () !== FALSE) |
---|
| 99 | { |
---|
| 100 | ob_clean(); |
---|
| 101 | } |
---|
| 102 | // default url is on html format |
---|
| 103 | $url = html_entity_decode($msg); |
---|
| 104 | header('Request-URI: '.$url); |
---|
| 105 | header('Content-Location: '.$url); |
---|
| 106 | header('Location: '.$url); |
---|
[16990] | 107 | ilog('WARN', $code, $url, $_SERVER['REQUEST_URI']); |
---|
[12796] | 108 | exit; |
---|
| 109 | } |
---|
| 110 | if ($code>=400) |
---|
| 111 | { |
---|
| 112 | $protocol = $_SERVER["SERVER_PROTOCOL"]; |
---|
| 113 | if ( ('HTTP/1.1' != $protocol) && ('HTTP/1.0' != $protocol) ) |
---|
| 114 | $protocol = 'HTTP/1.0'; |
---|
| 115 | |
---|
| 116 | header( "$protocol $code $msg", true, $code ); |
---|
| 117 | } |
---|
| 118 | //todo improve |
---|
| 119 | echo $msg; |
---|
[15551] | 120 | ilog('ERROR', $code, $msg, $_SERVER['REQUEST_URI']); |
---|
[12796] | 121 | exit; |
---|
| 122 | } |
---|
| 123 | |
---|
[12908] | 124 | function time_step( &$step ) |
---|
| 125 | { |
---|
| 126 | $tmp = $step; |
---|
[12920] | 127 | $step = microtime(true); |
---|
[12908] | 128 | return intval(1000*($step - $tmp)); |
---|
| 129 | } |
---|
[12796] | 130 | |
---|
[13021] | 131 | function url_to_size($s) |
---|
| 132 | { |
---|
| 133 | $pos = strpos($s, 'x'); |
---|
| 134 | if ($pos===false) |
---|
| 135 | { |
---|
| 136 | return array((int)$s, (int)$s); |
---|
| 137 | } |
---|
| 138 | return array((int)substr($s,0,$pos), (int)substr($s,$pos+1)); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | function parse_custom_params($tokens) |
---|
| 142 | { |
---|
| 143 | if (count($tokens)<1) |
---|
| 144 | ierror('Empty array while parsing Sizing', 400); |
---|
| 145 | |
---|
| 146 | $crop = 0; |
---|
| 147 | $min_size = null; |
---|
| 148 | |
---|
| 149 | $token = array_shift($tokens); |
---|
| 150 | if ($token[0]=='s') |
---|
| 151 | { |
---|
| 152 | $size = url_to_size( substr($token,1) ); |
---|
| 153 | } |
---|
| 154 | elseif ($token[0]=='e') |
---|
| 155 | { |
---|
| 156 | $crop = 1; |
---|
| 157 | $size = $min_size = url_to_size( substr($token,1) ); |
---|
| 158 | } |
---|
| 159 | else |
---|
| 160 | { |
---|
| 161 | $size = url_to_size( $token ); |
---|
| 162 | if (count($tokens)<2) |
---|
| 163 | ierror('Sizing arr', 400); |
---|
| 164 | |
---|
| 165 | $token = array_shift($tokens); |
---|
| 166 | $crop = char_to_fraction($token); |
---|
| 167 | |
---|
| 168 | $token = array_shift($tokens); |
---|
| 169 | $min_size = url_to_size( $token ); |
---|
| 170 | } |
---|
| 171 | return new DerivativeParams( new SizingParams($size, $crop, $min_size) ); |
---|
| 172 | } |
---|
| 173 | |
---|
[12796] | 174 | function parse_request() |
---|
| 175 | { |
---|
| 176 | global $conf, $page; |
---|
| 177 | |
---|
| 178 | if ( $conf['question_mark_in_urls']==false and |
---|
| 179 | isset($_SERVER["PATH_INFO"]) and !empty($_SERVER["PATH_INFO"]) ) |
---|
| 180 | { |
---|
| 181 | $req = $_SERVER["PATH_INFO"]; |
---|
| 182 | $req = str_replace('//', '/', $req); |
---|
| 183 | $path_count = count( explode('/', $req) ); |
---|
| 184 | $page['root_path'] = PHPWG_ROOT_PATH.str_repeat('../', $path_count-1); |
---|
| 185 | } |
---|
| 186 | else |
---|
| 187 | { |
---|
| 188 | $req = $_SERVER["QUERY_STRING"]; |
---|
[12865] | 189 | if ($pos=strpos($req, '&')) |
---|
| 190 | { |
---|
| 191 | $req = substr($req, 0, $pos); |
---|
| 192 | } |
---|
[12796] | 193 | /*foreach (array_keys($_GET) as $keynum => $key) |
---|
| 194 | { |
---|
| 195 | $req = $key; |
---|
| 196 | break; |
---|
| 197 | }*/ |
---|
| 198 | $page['root_path'] = PHPWG_ROOT_PATH; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | $req = ltrim($req, '/'); |
---|
| 202 | |
---|
[13527] | 203 | foreach (preg_split('#/+#', $req) as $token) |
---|
| 204 | { |
---|
| 205 | preg_match($conf['sync_chars_regex'], $token) or ierror('Invalid chars in request', 400); |
---|
| 206 | } |
---|
[16990] | 207 | |
---|
[12796] | 208 | $page['derivative_path'] = PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$req; |
---|
| 209 | |
---|
| 210 | $pos = strrpos($req, '.'); |
---|
| 211 | $pos!== false || ierror('Missing .', 400); |
---|
| 212 | $ext = substr($req, $pos); |
---|
| 213 | $page['derivative_ext'] = $ext; |
---|
| 214 | $req = substr($req, 0, $pos); |
---|
| 215 | |
---|
| 216 | $pos = strrpos($req, '-'); |
---|
| 217 | $pos!== false || ierror('Missing -', 400); |
---|
| 218 | $deriv = substr($req, $pos+1); |
---|
| 219 | $req = substr($req, 0, $pos); |
---|
| 220 | |
---|
| 221 | $deriv = explode('_', $deriv); |
---|
| 222 | foreach (ImageStdParams::get_defined_type_map() as $type => $params) |
---|
| 223 | { |
---|
[12797] | 224 | if ( derivative_to_url($type) == $deriv[0]) |
---|
[12796] | 225 | { |
---|
| 226 | $page['derivative_type'] = $type; |
---|
| 227 | $page['derivative_params'] = $params; |
---|
| 228 | break; |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | if (!isset($page['derivative_type'])) |
---|
| 233 | { |
---|
[12797] | 234 | if (derivative_to_url(IMG_CUSTOM) == $deriv[0]) |
---|
[12796] | 235 | { |
---|
| 236 | $page['derivative_type'] = IMG_CUSTOM; |
---|
| 237 | } |
---|
| 238 | else |
---|
| 239 | { |
---|
| 240 | ierror('Unknown parsing type', 400); |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | array_shift($deriv); |
---|
| 244 | |
---|
| 245 | if ($page['derivative_type'] == IMG_CUSTOM) |
---|
| 246 | { |
---|
[13021] | 247 | $params = $page['derivative_params'] = parse_custom_params($deriv); |
---|
[13651] | 248 | ImageStdParams::apply_global($params); |
---|
[13021] | 249 | |
---|
[12865] | 250 | if ($params->sizing->ideal_size[0] < 20 or $params->sizing->ideal_size[1] < 20) |
---|
| 251 | { |
---|
| 252 | ierror('Invalid size', 400); |
---|
| 253 | } |
---|
| 254 | if ($params->sizing->max_crop < 0 or $params->sizing->max_crop > 1) |
---|
| 255 | { |
---|
| 256 | ierror('Invalid crop', 400); |
---|
| 257 | } |
---|
[13021] | 258 | $greatest = ImageStdParams::get_by_type(IMG_XXLARGE); |
---|
[13038] | 259 | |
---|
[13021] | 260 | $key = array(); |
---|
| 261 | $params->add_url_tokens($key); |
---|
| 262 | $key = implode('_', $key); |
---|
| 263 | if (!isset(ImageStdParams::$custom[$key])) |
---|
| 264 | { |
---|
| 265 | ierror('Size not allowed', 403); |
---|
| 266 | } |
---|
[12796] | 267 | } |
---|
| 268 | |
---|
[13038] | 269 | if (is_file(PHPWG_ROOT_PATH.$req.$ext)) |
---|
| 270 | { |
---|
| 271 | $req = './'.$req; // will be used to match #iamges.path |
---|
| 272 | } |
---|
| 273 | elseif (is_file(PHPWG_ROOT_PATH.'../'.$req.$ext)) |
---|
| 274 | { |
---|
[12796] | 275 | $req = '../'.$req; |
---|
[13038] | 276 | } |
---|
[12796] | 277 | |
---|
| 278 | $page['src_location'] = $req.$ext; |
---|
| 279 | $page['src_path'] = PHPWG_ROOT_PATH.$page['src_location']; |
---|
| 280 | $page['src_url'] = $page['root_path'].$page['src_location']; |
---|
| 281 | } |
---|
| 282 | |
---|
[13038] | 283 | function try_switch_source(DerivativeParams $params, $original_mtime) |
---|
| 284 | { |
---|
| 285 | global $page; |
---|
| 286 | $candidates = array(); |
---|
| 287 | foreach(ImageStdParams::get_defined_type_map() as $candidate) |
---|
| 288 | { |
---|
| 289 | if ($candidate->type == $params->type) |
---|
| 290 | continue; |
---|
| 291 | if ($candidate->use_watermark != $params->use_watermark) |
---|
| 292 | continue; |
---|
| 293 | if ($candidate->max_width() < $params->max_width() || $candidate->max_height() < $params->max_height()) |
---|
| 294 | continue; |
---|
| 295 | if ($params->sizing->max_crop==0) |
---|
| 296 | { |
---|
| 297 | if ($candidate->sizing->max_crop!=0) |
---|
| 298 | continue; |
---|
| 299 | } |
---|
| 300 | else |
---|
| 301 | { |
---|
| 302 | if ($candidate->sizing->max_crop!=0) |
---|
| 303 | continue; // this could be optimized |
---|
| 304 | if (!isset($page['original_size'])) |
---|
| 305 | continue; |
---|
| 306 | $candidate_size = $candidate->compute_final_size($page['original_size']); |
---|
| 307 | if ($candidate_size[0] < $params->sizing->min_size[0] || $candidate_size[1] < $params->sizing->min_size[1] ) |
---|
| 308 | continue; |
---|
| 309 | } |
---|
| 310 | $candidates[] = $candidate; |
---|
| 311 | } |
---|
[12796] | 312 | |
---|
[13038] | 313 | foreach( array_reverse($candidates) as $candidate) |
---|
| 314 | { |
---|
| 315 | $candidate_path = $page['derivative_path']; |
---|
| 316 | $candidate_path = str_replace( '-'.derivative_to_url($params->type), '-'.derivative_to_url($candidate->type), $candidate_path); |
---|
| 317 | $candidate_mtime = @filemtime($candidate_path); |
---|
| 318 | if ($candidate_mtime === false |
---|
| 319 | || $candidate_mtime < $original_mtime |
---|
| 320 | || $candidate_mtime < $candidate->last_mod_time) |
---|
| 321 | continue; |
---|
| 322 | $params->use_watermark = false; |
---|
| 323 | $params->sharpen = min(1, $params->sharpen); |
---|
| 324 | $page['src_path'] = $candidate_path; |
---|
| 325 | $page['src_url'] = $page['root_path'] . substr($candidate_path, strlen(PHPWG_ROOT_PATH)); |
---|
[13848] | 326 | $page['rotation_angle'] = 0; |
---|
[13038] | 327 | } |
---|
| 328 | } |
---|
| 329 | |
---|
[12820] | 330 | function send_derivative($expires) |
---|
| 331 | { |
---|
| 332 | global $page; |
---|
[13444] | 333 | |
---|
| 334 | if (isset($_GET['ajaxload']) and $_GET['ajaxload'] == 'true') |
---|
| 335 | { |
---|
| 336 | include_once(PHPWG_ROOT_PATH.'include/functions_cookie.inc.php'); |
---|
| 337 | include_once(PHPWG_ROOT_PATH.'include/functions_url.inc.php'); |
---|
| 338 | |
---|
| 339 | $response = new json_response(); |
---|
| 340 | $response->url = embellish_url(get_absolute_root_url().$page['derivative_path']); |
---|
| 341 | echo json_encode($response); |
---|
| 342 | return; |
---|
| 343 | } |
---|
[12820] | 344 | $fp = fopen($page['derivative_path'], 'rb'); |
---|
[12796] | 345 | |
---|
[12820] | 346 | $fstat = fstat($fp); |
---|
| 347 | header('Last-Modified: '.gmdate('D, d M Y H:i:s', $fstat['mtime']).' GMT'); |
---|
| 348 | if ($expires!==false) |
---|
| 349 | { |
---|
| 350 | header('Expires: '.gmdate('D, d M Y H:i:s', $expires).' GMT'); |
---|
| 351 | } |
---|
| 352 | header('Content-length: '.$fstat['size']); |
---|
| 353 | header('Connection: close'); |
---|
| 354 | |
---|
| 355 | $ctype="application/octet-stream"; |
---|
| 356 | switch (strtolower($page['derivative_ext'])) |
---|
| 357 | { |
---|
| 358 | case ".jpe": case ".jpeg": case ".jpg": $ctype="image/jpeg"; break; |
---|
| 359 | case ".png": $ctype="image/png"; break; |
---|
| 360 | case ".gif": $ctype="image/gif"; break; |
---|
| 361 | } |
---|
| 362 | header("Content-Type: $ctype"); |
---|
| 363 | |
---|
| 364 | fpassthru($fp); |
---|
| 365 | fclose($fp); |
---|
| 366 | } |
---|
| 367 | |
---|
[13444] | 368 | class json_response |
---|
| 369 | { |
---|
| 370 | var $url; |
---|
| 371 | } |
---|
[12820] | 372 | |
---|
[12796] | 373 | $page=array(); |
---|
[12920] | 374 | $begin = $step = microtime(true); |
---|
[12908] | 375 | $timing=array(); |
---|
| 376 | foreach( explode(',','load,rotate,crop,scale,sharpen,watermark,save,send') as $k ) |
---|
| 377 | { |
---|
| 378 | $timing[$k] = ''; |
---|
| 379 | } |
---|
[12796] | 380 | |
---|
[13736] | 381 | include_once(PHPWG_ROOT_PATH .'include/dblayer/functions_'.$conf['dblayer'].'.inc.php'); |
---|
[12796] | 382 | include_once( PHPWG_ROOT_PATH .'/include/derivative_params.inc.php'); |
---|
| 383 | include_once( PHPWG_ROOT_PATH .'/include/derivative_std_params.inc.php'); |
---|
| 384 | |
---|
[13736] | 385 | try |
---|
| 386 | { |
---|
| 387 | $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'], |
---|
| 388 | $conf['db_password'], $conf['db_base']); |
---|
| 389 | } |
---|
| 390 | catch (Exception $e) |
---|
| 391 | { |
---|
| 392 | ilog("db error", $e->getMessage()); |
---|
| 393 | } |
---|
| 394 | list($conf['derivatives']) = pwg_db_fetch_row(pwg_query('SELECT value FROM '.$prefixeTable.'config WHERE param=\'derivatives\'')); |
---|
| 395 | ImageStdParams::load_from_db(); |
---|
[12796] | 396 | |
---|
| 397 | |
---|
| 398 | parse_request(); |
---|
| 399 | //var_export($page); |
---|
| 400 | |
---|
| 401 | $params = $page['derivative_params']; |
---|
| 402 | |
---|
| 403 | $src_mtime = @filemtime($page['src_path']); |
---|
| 404 | if ($src_mtime === false) |
---|
| 405 | { |
---|
| 406 | ierror('Source not found', 404); |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | $need_generate = false; |
---|
| 410 | $derivative_mtime = @filemtime($page['derivative_path']); |
---|
| 411 | if ($derivative_mtime === false or |
---|
| 412 | $derivative_mtime < $src_mtime or |
---|
| 413 | $derivative_mtime < $params->last_mod_time) |
---|
| 414 | { |
---|
| 415 | $need_generate = true; |
---|
| 416 | } |
---|
| 417 | |
---|
[12820] | 418 | $expires=false; |
---|
| 419 | $now = time(); |
---|
[12865] | 420 | if ( isset($_GET['b']) ) |
---|
| 421 | { |
---|
| 422 | $expires = $now + 100; |
---|
| 423 | header("Cache-control: no-store, max-age=100"); |
---|
| 424 | } |
---|
| 425 | elseif ( $now > (max($src_mtime, $params->last_mod_time) + 24*3600) ) |
---|
[12820] | 426 | {// somehow arbitrary - if derivative params or src didn't change for the last 24 hours, we send an expire header for several days |
---|
| 427 | $expires = $now + 10*24*3600; |
---|
| 428 | } |
---|
| 429 | |
---|
[12796] | 430 | if (!$need_generate) |
---|
| 431 | { |
---|
| 432 | if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) |
---|
| 433 | and strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $derivative_mtime) |
---|
| 434 | {// send the last mod time of the file back |
---|
| 435 | header('Last-Modified: '.gmdate('D, d M Y H:i:s', $derivative_mtime).' GMT', true, 304); |
---|
| 436 | header('Expires: '.gmdate('D, d M Y H:i:s', time()+10*24*3600).' GMT', true, 304); |
---|
| 437 | exit; |
---|
| 438 | } |
---|
[12820] | 439 | send_derivative($expires); |
---|
[13444] | 440 | exit; |
---|
[12796] | 441 | } |
---|
| 442 | |
---|
[13864] | 443 | include_once(PHPWG_ROOT_PATH . 'admin/include/image.class.php'); |
---|
[13038] | 444 | $page['coi'] = null; |
---|
| 445 | if (strpos($page['src_location'], '/pwg_representative/')===false |
---|
| 446 | && strpos($page['src_location'], 'themes/')===false |
---|
| 447 | && strpos($page['src_location'], 'plugins/')===false) |
---|
| 448 | { |
---|
| 449 | try |
---|
| 450 | { |
---|
[13843] | 451 | $query = ' |
---|
[13864] | 452 | SELECT * |
---|
[13843] | 453 | FROM '.$prefixeTable.'images |
---|
| 454 | WHERE path=\''.$page['src_location'].'\' |
---|
| 455 | ;'; |
---|
[16990] | 456 | |
---|
[13038] | 457 | if ( ($row=pwg_db_fetch_assoc(pwg_query($query))) ) |
---|
| 458 | { |
---|
| 459 | if (isset($row['width'])) |
---|
| 460 | { |
---|
| 461 | $page['original_size'] = array($row['width'],$row['height']); |
---|
| 462 | } |
---|
| 463 | $page['coi'] = $row['coi']; |
---|
[13843] | 464 | |
---|
[13864] | 465 | if (!isset($row['rotation'])) |
---|
[13843] | 466 | { |
---|
| 467 | $page['rotation_angle'] = pwg_image::get_rotation_angle($page['src_path']); |
---|
[16990] | 468 | |
---|
[13843] | 469 | single_update( |
---|
| 470 | $prefixeTable.'images', |
---|
| 471 | array('rotation' => pwg_image::get_rotation_code_from_angle($page['rotation_angle'])), |
---|
| 472 | array('id' => $row['id']) |
---|
| 473 | ); |
---|
| 474 | } |
---|
| 475 | else |
---|
| 476 | { |
---|
| 477 | $page['rotation_angle'] = pwg_image::get_rotation_angle_from_code($row['rotation']); |
---|
| 478 | } |
---|
| 479 | |
---|
[13038] | 480 | } |
---|
| 481 | if (!$row) |
---|
| 482 | { |
---|
| 483 | ierror('Db file path not found', 404); |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | catch (Exception $e) |
---|
| 487 | { |
---|
| 488 | ilog("db error", $e->getMessage()); |
---|
| 489 | } |
---|
| 490 | } |
---|
[15551] | 491 | else |
---|
| 492 | { |
---|
| 493 | $page['rotation_angle'] = 0; |
---|
| 494 | } |
---|
[13736] | 495 | mysql_close($pwg_db_link); |
---|
[13038] | 496 | |
---|
| 497 | try_switch_source($params, $src_mtime); |
---|
| 498 | |
---|
[12865] | 499 | if (!mkgetdir(dirname($page['derivative_path']))) |
---|
| 500 | { |
---|
| 501 | ierror("dir create error", 500); |
---|
| 502 | } |
---|
[12796] | 503 | |
---|
[12851] | 504 | ignore_user_abort(true); |
---|
[16073] | 505 | @set_time_limit(0); |
---|
[12851] | 506 | |
---|
[12796] | 507 | $image = new pwg_image($page['src_path']); |
---|
[12908] | 508 | $timing['load'] = time_step($step); |
---|
[12796] | 509 | |
---|
| 510 | $changes = 0; |
---|
| 511 | |
---|
[13843] | 512 | // rotate |
---|
[15551] | 513 | if (0 != $page['rotation_angle']) |
---|
[13843] | 514 | { |
---|
| 515 | $image->rotate($page['rotation_angle']); |
---|
[13848] | 516 | $changes++; |
---|
| 517 | $timing['rotate'] = time_step($step); |
---|
[13843] | 518 | } |
---|
[12796] | 519 | |
---|
| 520 | // Crop & scale |
---|
[12851] | 521 | $o_size = $d_size = array($image->get_width(),$image->get_height()); |
---|
| 522 | $params->sizing->compute($o_size , $page['coi'], $crop_rect, $scaled_size ); |
---|
[12796] | 523 | if ($crop_rect) |
---|
| 524 | { |
---|
| 525 | $changes++; |
---|
| 526 | $image->crop( $crop_rect->width(), $crop_rect->height(), $crop_rect->l, $crop_rect->t); |
---|
[12908] | 527 | $timing['crop'] = time_step($step); |
---|
[12796] | 528 | } |
---|
| 529 | |
---|
[12851] | 530 | if ($scaled_size) |
---|
[12796] | 531 | { |
---|
| 532 | $changes++; |
---|
[12851] | 533 | $image->resize( $scaled_size[0], $scaled_size[1] ); |
---|
| 534 | $d_size = $scaled_size; |
---|
[12908] | 535 | $timing['scale'] = time_step($step); |
---|
[12796] | 536 | } |
---|
| 537 | |
---|
[12851] | 538 | if ($params->sharpen) |
---|
| 539 | { |
---|
| 540 | $changes += $image->sharpen( $params->sharpen ); |
---|
[12908] | 541 | $timing['sharpen'] = time_step($step); |
---|
[12851] | 542 | } |
---|
| 543 | |
---|
[16990] | 544 | if ($params->will_watermark($d_size)) |
---|
[12851] | 545 | { |
---|
| 546 | $wm = ImageStdParams::get_watermark(); |
---|
| 547 | $wm_image = new pwg_image(PHPWG_ROOT_PATH.$wm->file); |
---|
| 548 | $wm_size = array($wm_image->get_width(),$wm_image->get_height()); |
---|
| 549 | if ($d_size[0]<$wm_size[0] or $d_size[1]<$wm_size[1]) |
---|
| 550 | { |
---|
| 551 | $wm_scaling_params = SizingParams::classic($d_size[0], $d_size[1]); |
---|
| 552 | $wm_scaling_params->compute($wm_size, null, $tmp, $wm_scaled_size); |
---|
| 553 | $wm_size = $wm_scaled_size; |
---|
| 554 | $wm_image->resize( $wm_scaled_size[0], $wm_scaled_size[1] ); |
---|
| 555 | } |
---|
| 556 | $x = round( ($wm->xpos/100)*($d_size[0]-$wm_size[0]) ); |
---|
| 557 | $y = round( ($wm->ypos/100)*($d_size[1]-$wm_size[1]) ); |
---|
| 558 | if ($image->compose($wm_image, $x, $y, $wm->opacity)) |
---|
| 559 | { |
---|
| 560 | $changes++; |
---|
| 561 | if ($wm->xrepeat) |
---|
| 562 | { |
---|
| 563 | // todo |
---|
[12865] | 564 | $pad = $wm_size[0] + max(30, round($wm_size[0]/4)); |
---|
| 565 | for($i=-$wm->xrepeat; $i<=$wm->xrepeat; $i++) |
---|
| 566 | { |
---|
| 567 | if (!$i) continue; |
---|
| 568 | $x2 = $x + $i * $pad; |
---|
| 569 | if ($x2>=0 && $x2+$wm_size[0]<$d_size[0]) |
---|
| 570 | if (!$image->compose($wm_image, $x2, $y, $wm->opacity)) |
---|
| 571 | break; |
---|
| 572 | } |
---|
[12851] | 573 | } |
---|
| 574 | } |
---|
| 575 | $wm_image->destroy(); |
---|
[12908] | 576 | $timing['watermark'] = time_step($step); |
---|
[12851] | 577 | } |
---|
| 578 | |
---|
[12796] | 579 | // no change required - redirect to source |
---|
| 580 | if (!$changes) |
---|
| 581 | { |
---|
| 582 | header("X-i: No change"); |
---|
| 583 | ierror( $page['src_url'], 301); |
---|
| 584 | } |
---|
| 585 | |
---|
[13426] | 586 | if ($d_size[0]*$d_size[1] < 256000) |
---|
[12958] | 587 | {// strip metadata for small images |
---|
| 588 | $image->strip(); |
---|
| 589 | } |
---|
[13843] | 590 | |
---|
[14649] | 591 | $image->set_compression_quality( ImageStdParams::$quality ); |
---|
[12796] | 592 | $image->write( $page['derivative_path'] ); |
---|
| 593 | $image->destroy(); |
---|
[12908] | 594 | $timing['save'] = time_step($step); |
---|
[12796] | 595 | |
---|
[12820] | 596 | send_derivative($expires); |
---|
[12908] | 597 | $timing['send'] = time_step($step); |
---|
| 598 | |
---|
| 599 | ilog('perf', |
---|
| 600 | basename($page['src_path']), $o_size, $o_size[0]*$o_size[1], |
---|
| 601 | basename($page['derivative_path']), $d_size, $d_size[0]*$d_size[1], |
---|
[12920] | 602 | function_exists('memory_get_peak_usage') ? round( memory_get_peak_usage()/(1024*1024), 1) : '', |
---|
[12908] | 603 | time_step($begin), |
---|
[12920] | 604 | '|', $timing); |
---|
[12796] | 605 | ?> |
---|