[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); |
---|
| 107 | exit; |
---|
| 108 | } |
---|
| 109 | if ($code>=400) |
---|
| 110 | { |
---|
| 111 | $protocol = $_SERVER["SERVER_PROTOCOL"]; |
---|
| 112 | if ( ('HTTP/1.1' != $protocol) && ('HTTP/1.0' != $protocol) ) |
---|
| 113 | $protocol = 'HTTP/1.0'; |
---|
| 114 | |
---|
| 115 | header( "$protocol $code $msg", true, $code ); |
---|
| 116 | } |
---|
| 117 | //todo improve |
---|
| 118 | echo $msg; |
---|
[15551] | 119 | ilog('ERROR', $code, $msg, $_SERVER['REQUEST_URI']); |
---|
[12796] | 120 | exit; |
---|
| 121 | } |
---|
| 122 | |
---|
[12908] | 123 | function time_step( &$step ) |
---|
| 124 | { |
---|
| 125 | $tmp = $step; |
---|
[12920] | 126 | $step = microtime(true); |
---|
[12908] | 127 | return intval(1000*($step - $tmp)); |
---|
| 128 | } |
---|
[12796] | 129 | |
---|
[13021] | 130 | function url_to_size($s) |
---|
| 131 | { |
---|
| 132 | $pos = strpos($s, 'x'); |
---|
| 133 | if ($pos===false) |
---|
| 134 | { |
---|
| 135 | return array((int)$s, (int)$s); |
---|
| 136 | } |
---|
| 137 | return array((int)substr($s,0,$pos), (int)substr($s,$pos+1)); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | function parse_custom_params($tokens) |
---|
| 141 | { |
---|
| 142 | if (count($tokens)<1) |
---|
| 143 | ierror('Empty array while parsing Sizing', 400); |
---|
| 144 | |
---|
| 145 | $crop = 0; |
---|
| 146 | $min_size = null; |
---|
| 147 | |
---|
| 148 | $token = array_shift($tokens); |
---|
| 149 | if ($token[0]=='s') |
---|
| 150 | { |
---|
| 151 | $size = url_to_size( substr($token,1) ); |
---|
| 152 | } |
---|
| 153 | elseif ($token[0]=='e') |
---|
| 154 | { |
---|
| 155 | $crop = 1; |
---|
| 156 | $size = $min_size = url_to_size( substr($token,1) ); |
---|
| 157 | } |
---|
| 158 | else |
---|
| 159 | { |
---|
| 160 | $size = url_to_size( $token ); |
---|
| 161 | if (count($tokens)<2) |
---|
| 162 | ierror('Sizing arr', 400); |
---|
| 163 | |
---|
| 164 | $token = array_shift($tokens); |
---|
| 165 | $crop = char_to_fraction($token); |
---|
| 166 | |
---|
| 167 | $token = array_shift($tokens); |
---|
| 168 | $min_size = url_to_size( $token ); |
---|
| 169 | } |
---|
| 170 | return new DerivativeParams( new SizingParams($size, $crop, $min_size) ); |
---|
| 171 | } |
---|
| 172 | |
---|
[12796] | 173 | function parse_request() |
---|
| 174 | { |
---|
| 175 | global $conf, $page; |
---|
| 176 | |
---|
| 177 | if ( $conf['question_mark_in_urls']==false and |
---|
| 178 | isset($_SERVER["PATH_INFO"]) and !empty($_SERVER["PATH_INFO"]) ) |
---|
| 179 | { |
---|
| 180 | $req = $_SERVER["PATH_INFO"]; |
---|
| 181 | $req = str_replace('//', '/', $req); |
---|
| 182 | $path_count = count( explode('/', $req) ); |
---|
| 183 | $page['root_path'] = PHPWG_ROOT_PATH.str_repeat('../', $path_count-1); |
---|
| 184 | } |
---|
| 185 | else |
---|
| 186 | { |
---|
| 187 | $req = $_SERVER["QUERY_STRING"]; |
---|
[12865] | 188 | if ($pos=strpos($req, '&')) |
---|
| 189 | { |
---|
| 190 | $req = substr($req, 0, $pos); |
---|
| 191 | } |
---|
[12796] | 192 | /*foreach (array_keys($_GET) as $keynum => $key) |
---|
| 193 | { |
---|
| 194 | $req = $key; |
---|
| 195 | break; |
---|
| 196 | }*/ |
---|
| 197 | $page['root_path'] = PHPWG_ROOT_PATH; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | $req = ltrim($req, '/'); |
---|
| 201 | |
---|
[13527] | 202 | foreach (preg_split('#/+#', $req) as $token) |
---|
| 203 | { |
---|
| 204 | preg_match($conf['sync_chars_regex'], $token) or ierror('Invalid chars in request', 400); |
---|
| 205 | } |
---|
| 206 | |
---|
[12796] | 207 | $page['derivative_path'] = PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$req; |
---|
| 208 | |
---|
| 209 | $pos = strrpos($req, '.'); |
---|
| 210 | $pos!== false || ierror('Missing .', 400); |
---|
| 211 | $ext = substr($req, $pos); |
---|
| 212 | $page['derivative_ext'] = $ext; |
---|
| 213 | $req = substr($req, 0, $pos); |
---|
| 214 | |
---|
| 215 | $pos = strrpos($req, '-'); |
---|
| 216 | $pos!== false || ierror('Missing -', 400); |
---|
| 217 | $deriv = substr($req, $pos+1); |
---|
| 218 | $req = substr($req, 0, $pos); |
---|
| 219 | |
---|
| 220 | $deriv = explode('_', $deriv); |
---|
| 221 | foreach (ImageStdParams::get_defined_type_map() as $type => $params) |
---|
| 222 | { |
---|
[12797] | 223 | if ( derivative_to_url($type) == $deriv[0]) |
---|
[12796] | 224 | { |
---|
| 225 | $page['derivative_type'] = $type; |
---|
| 226 | $page['derivative_params'] = $params; |
---|
| 227 | break; |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | if (!isset($page['derivative_type'])) |
---|
| 232 | { |
---|
[12797] | 233 | if (derivative_to_url(IMG_CUSTOM) == $deriv[0]) |
---|
[12796] | 234 | { |
---|
| 235 | $page['derivative_type'] = IMG_CUSTOM; |
---|
| 236 | } |
---|
| 237 | else |
---|
| 238 | { |
---|
| 239 | ierror('Unknown parsing type', 400); |
---|
| 240 | } |
---|
| 241 | } |
---|
| 242 | array_shift($deriv); |
---|
| 243 | |
---|
| 244 | if ($page['derivative_type'] == IMG_CUSTOM) |
---|
| 245 | { |
---|
[13021] | 246 | $params = $page['derivative_params'] = parse_custom_params($deriv); |
---|
[13651] | 247 | ImageStdParams::apply_global($params); |
---|
[13021] | 248 | |
---|
[12865] | 249 | if ($params->sizing->ideal_size[0] < 20 or $params->sizing->ideal_size[1] < 20) |
---|
| 250 | { |
---|
| 251 | ierror('Invalid size', 400); |
---|
| 252 | } |
---|
| 253 | if ($params->sizing->max_crop < 0 or $params->sizing->max_crop > 1) |
---|
| 254 | { |
---|
| 255 | ierror('Invalid crop', 400); |
---|
| 256 | } |
---|
[13021] | 257 | $greatest = ImageStdParams::get_by_type(IMG_XXLARGE); |
---|
[13038] | 258 | |
---|
[13021] | 259 | $key = array(); |
---|
| 260 | $params->add_url_tokens($key); |
---|
| 261 | $key = implode('_', $key); |
---|
| 262 | if (!isset(ImageStdParams::$custom[$key])) |
---|
| 263 | { |
---|
| 264 | ierror('Size not allowed', 403); |
---|
| 265 | } |
---|
[12796] | 266 | } |
---|
| 267 | |
---|
[13038] | 268 | if (is_file(PHPWG_ROOT_PATH.$req.$ext)) |
---|
| 269 | { |
---|
| 270 | $req = './'.$req; // will be used to match #iamges.path |
---|
| 271 | } |
---|
| 272 | elseif (is_file(PHPWG_ROOT_PATH.'../'.$req.$ext)) |
---|
| 273 | { |
---|
[12796] | 274 | $req = '../'.$req; |
---|
[13038] | 275 | } |
---|
[12796] | 276 | |
---|
| 277 | $page['src_location'] = $req.$ext; |
---|
| 278 | $page['src_path'] = PHPWG_ROOT_PATH.$page['src_location']; |
---|
| 279 | $page['src_url'] = $page['root_path'].$page['src_location']; |
---|
| 280 | } |
---|
| 281 | |
---|
[13038] | 282 | function try_switch_source(DerivativeParams $params, $original_mtime) |
---|
| 283 | { |
---|
| 284 | global $page; |
---|
| 285 | $candidates = array(); |
---|
| 286 | foreach(ImageStdParams::get_defined_type_map() as $candidate) |
---|
| 287 | { |
---|
| 288 | if ($candidate->type == $params->type) |
---|
| 289 | continue; |
---|
| 290 | if ($candidate->use_watermark != $params->use_watermark) |
---|
| 291 | continue; |
---|
| 292 | if ($candidate->max_width() < $params->max_width() || $candidate->max_height() < $params->max_height()) |
---|
| 293 | continue; |
---|
| 294 | if ($params->sizing->max_crop==0) |
---|
| 295 | { |
---|
| 296 | if ($candidate->sizing->max_crop!=0) |
---|
| 297 | continue; |
---|
| 298 | } |
---|
| 299 | else |
---|
| 300 | { |
---|
| 301 | if ($candidate->sizing->max_crop!=0) |
---|
| 302 | continue; // this could be optimized |
---|
| 303 | if (!isset($page['original_size'])) |
---|
| 304 | continue; |
---|
| 305 | $candidate_size = $candidate->compute_final_size($page['original_size']); |
---|
| 306 | if ($candidate_size[0] < $params->sizing->min_size[0] || $candidate_size[1] < $params->sizing->min_size[1] ) |
---|
| 307 | continue; |
---|
| 308 | } |
---|
| 309 | $candidates[] = $candidate; |
---|
| 310 | } |
---|
[12796] | 311 | |
---|
[13038] | 312 | foreach( array_reverse($candidates) as $candidate) |
---|
| 313 | { |
---|
| 314 | $candidate_path = $page['derivative_path']; |
---|
| 315 | $candidate_path = str_replace( '-'.derivative_to_url($params->type), '-'.derivative_to_url($candidate->type), $candidate_path); |
---|
| 316 | $candidate_mtime = @filemtime($candidate_path); |
---|
| 317 | if ($candidate_mtime === false |
---|
| 318 | || $candidate_mtime < $original_mtime |
---|
| 319 | || $candidate_mtime < $candidate->last_mod_time) |
---|
| 320 | continue; |
---|
| 321 | $params->use_watermark = false; |
---|
| 322 | $params->sharpen = min(1, $params->sharpen); |
---|
| 323 | $page['src_path'] = $candidate_path; |
---|
| 324 | $page['src_url'] = $page['root_path'] . substr($candidate_path, strlen(PHPWG_ROOT_PATH)); |
---|
[13848] | 325 | $page['rotation_angle'] = 0; |
---|
[13038] | 326 | } |
---|
| 327 | } |
---|
| 328 | |
---|
[12820] | 329 | function send_derivative($expires) |
---|
| 330 | { |
---|
| 331 | global $page; |
---|
[13444] | 332 | |
---|
| 333 | if (isset($_GET['ajaxload']) and $_GET['ajaxload'] == 'true') |
---|
| 334 | { |
---|
| 335 | include_once(PHPWG_ROOT_PATH.'include/functions_cookie.inc.php'); |
---|
| 336 | include_once(PHPWG_ROOT_PATH.'include/functions_url.inc.php'); |
---|
| 337 | |
---|
| 338 | $response = new json_response(); |
---|
| 339 | $response->url = embellish_url(get_absolute_root_url().$page['derivative_path']); |
---|
| 340 | echo json_encode($response); |
---|
| 341 | return; |
---|
| 342 | } |
---|
[12820] | 343 | $fp = fopen($page['derivative_path'], 'rb'); |
---|
[12796] | 344 | |
---|
[12820] | 345 | $fstat = fstat($fp); |
---|
| 346 | header('Last-Modified: '.gmdate('D, d M Y H:i:s', $fstat['mtime']).' GMT'); |
---|
| 347 | if ($expires!==false) |
---|
| 348 | { |
---|
| 349 | header('Expires: '.gmdate('D, d M Y H:i:s', $expires).' GMT'); |
---|
| 350 | } |
---|
| 351 | header('Content-length: '.$fstat['size']); |
---|
| 352 | header('Connection: close'); |
---|
| 353 | |
---|
| 354 | $ctype="application/octet-stream"; |
---|
| 355 | switch (strtolower($page['derivative_ext'])) |
---|
| 356 | { |
---|
| 357 | case ".jpe": case ".jpeg": case ".jpg": $ctype="image/jpeg"; break; |
---|
| 358 | case ".png": $ctype="image/png"; break; |
---|
| 359 | case ".gif": $ctype="image/gif"; break; |
---|
| 360 | } |
---|
| 361 | header("Content-Type: $ctype"); |
---|
| 362 | |
---|
| 363 | fpassthru($fp); |
---|
| 364 | fclose($fp); |
---|
| 365 | } |
---|
| 366 | |
---|
[13444] | 367 | class json_response |
---|
| 368 | { |
---|
| 369 | var $url; |
---|
| 370 | } |
---|
[12820] | 371 | |
---|
[12796] | 372 | $page=array(); |
---|
[12920] | 373 | $begin = $step = microtime(true); |
---|
[12908] | 374 | $timing=array(); |
---|
| 375 | foreach( explode(',','load,rotate,crop,scale,sharpen,watermark,save,send') as $k ) |
---|
| 376 | { |
---|
| 377 | $timing[$k] = ''; |
---|
| 378 | } |
---|
[12796] | 379 | |
---|
[13736] | 380 | include_once(PHPWG_ROOT_PATH .'include/dblayer/functions_'.$conf['dblayer'].'.inc.php'); |
---|
[12796] | 381 | include_once( PHPWG_ROOT_PATH .'/include/derivative_params.inc.php'); |
---|
| 382 | include_once( PHPWG_ROOT_PATH .'/include/derivative_std_params.inc.php'); |
---|
| 383 | |
---|
[13736] | 384 | try |
---|
| 385 | { |
---|
| 386 | $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'], |
---|
| 387 | $conf['db_password'], $conf['db_base']); |
---|
| 388 | } |
---|
| 389 | catch (Exception $e) |
---|
| 390 | { |
---|
| 391 | ilog("db error", $e->getMessage()); |
---|
| 392 | } |
---|
| 393 | list($conf['derivatives']) = pwg_db_fetch_row(pwg_query('SELECT value FROM '.$prefixeTable.'config WHERE param=\'derivatives\'')); |
---|
| 394 | ImageStdParams::load_from_db(); |
---|
[12796] | 395 | |
---|
| 396 | |
---|
| 397 | parse_request(); |
---|
| 398 | //var_export($page); |
---|
| 399 | |
---|
| 400 | $params = $page['derivative_params']; |
---|
| 401 | |
---|
| 402 | $src_mtime = @filemtime($page['src_path']); |
---|
| 403 | if ($src_mtime === false) |
---|
| 404 | { |
---|
| 405 | ierror('Source not found', 404); |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | $need_generate = false; |
---|
| 409 | $derivative_mtime = @filemtime($page['derivative_path']); |
---|
| 410 | if ($derivative_mtime === false or |
---|
| 411 | $derivative_mtime < $src_mtime or |
---|
| 412 | $derivative_mtime < $params->last_mod_time) |
---|
| 413 | { |
---|
| 414 | $need_generate = true; |
---|
| 415 | } |
---|
| 416 | |
---|
[12820] | 417 | $expires=false; |
---|
| 418 | $now = time(); |
---|
[12865] | 419 | if ( isset($_GET['b']) ) |
---|
| 420 | { |
---|
| 421 | $expires = $now + 100; |
---|
| 422 | header("Cache-control: no-store, max-age=100"); |
---|
| 423 | } |
---|
| 424 | elseif ( $now > (max($src_mtime, $params->last_mod_time) + 24*3600) ) |
---|
[12820] | 425 | {// somehow arbitrary - if derivative params or src didn't change for the last 24 hours, we send an expire header for several days |
---|
| 426 | $expires = $now + 10*24*3600; |
---|
| 427 | } |
---|
| 428 | |
---|
[12796] | 429 | if (!$need_generate) |
---|
| 430 | { |
---|
| 431 | if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) |
---|
| 432 | and strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $derivative_mtime) |
---|
| 433 | {// send the last mod time of the file back |
---|
| 434 | header('Last-Modified: '.gmdate('D, d M Y H:i:s', $derivative_mtime).' GMT', true, 304); |
---|
| 435 | header('Expires: '.gmdate('D, d M Y H:i:s', time()+10*24*3600).' GMT', true, 304); |
---|
| 436 | exit; |
---|
| 437 | } |
---|
[12820] | 438 | send_derivative($expires); |
---|
[13444] | 439 | exit; |
---|
[12796] | 440 | } |
---|
| 441 | |
---|
[13864] | 442 | include_once(PHPWG_ROOT_PATH . 'admin/include/image.class.php'); |
---|
[13038] | 443 | $page['coi'] = null; |
---|
| 444 | if (strpos($page['src_location'], '/pwg_representative/')===false |
---|
| 445 | && strpos($page['src_location'], 'themes/')===false |
---|
| 446 | && strpos($page['src_location'], 'plugins/')===false) |
---|
| 447 | { |
---|
| 448 | try |
---|
| 449 | { |
---|
[13843] | 450 | $query = ' |
---|
[13864] | 451 | SELECT * |
---|
[13843] | 452 | FROM '.$prefixeTable.'images |
---|
| 453 | WHERE path=\''.$page['src_location'].'\' |
---|
| 454 | ;'; |
---|
| 455 | |
---|
[13038] | 456 | if ( ($row=pwg_db_fetch_assoc(pwg_query($query))) ) |
---|
| 457 | { |
---|
| 458 | if (isset($row['width'])) |
---|
| 459 | { |
---|
| 460 | $page['original_size'] = array($row['width'],$row['height']); |
---|
| 461 | } |
---|
| 462 | $page['coi'] = $row['coi']; |
---|
[13843] | 463 | |
---|
[13864] | 464 | if (!isset($row['rotation'])) |
---|
[13843] | 465 | { |
---|
| 466 | $page['rotation_angle'] = pwg_image::get_rotation_angle($page['src_path']); |
---|
| 467 | |
---|
| 468 | single_update( |
---|
| 469 | $prefixeTable.'images', |
---|
| 470 | array('rotation' => pwg_image::get_rotation_code_from_angle($page['rotation_angle'])), |
---|
| 471 | array('id' => $row['id']) |
---|
| 472 | ); |
---|
| 473 | } |
---|
| 474 | else |
---|
| 475 | { |
---|
| 476 | $page['rotation_angle'] = pwg_image::get_rotation_angle_from_code($row['rotation']); |
---|
| 477 | } |
---|
| 478 | |
---|
[13038] | 479 | } |
---|
| 480 | if (!$row) |
---|
| 481 | { |
---|
| 482 | ierror('Db file path not found', 404); |
---|
| 483 | } |
---|
| 484 | } |
---|
| 485 | catch (Exception $e) |
---|
| 486 | { |
---|
| 487 | ilog("db error", $e->getMessage()); |
---|
| 488 | } |
---|
| 489 | } |
---|
[15551] | 490 | else |
---|
| 491 | { |
---|
| 492 | $page['rotation_angle'] = 0; |
---|
| 493 | } |
---|
[13736] | 494 | mysql_close($pwg_db_link); |
---|
[13038] | 495 | |
---|
| 496 | try_switch_source($params, $src_mtime); |
---|
| 497 | |
---|
[12865] | 498 | if (!mkgetdir(dirname($page['derivative_path']))) |
---|
| 499 | { |
---|
| 500 | ierror("dir create error", 500); |
---|
| 501 | } |
---|
[12796] | 502 | |
---|
[12851] | 503 | ignore_user_abort(true); |
---|
| 504 | set_time_limit(0); |
---|
| 505 | |
---|
[12796] | 506 | $image = new pwg_image($page['src_path']); |
---|
[12908] | 507 | $timing['load'] = time_step($step); |
---|
[12796] | 508 | |
---|
| 509 | $changes = 0; |
---|
| 510 | |
---|
[13843] | 511 | // rotate |
---|
[15551] | 512 | if (0 != $page['rotation_angle']) |
---|
[13843] | 513 | { |
---|
| 514 | $image->rotate($page['rotation_angle']); |
---|
[13848] | 515 | $changes++; |
---|
| 516 | $timing['rotate'] = time_step($step); |
---|
[13843] | 517 | } |
---|
[12796] | 518 | |
---|
| 519 | // Crop & scale |
---|
[12851] | 520 | $o_size = $d_size = array($image->get_width(),$image->get_height()); |
---|
| 521 | $params->sizing->compute($o_size , $page['coi'], $crop_rect, $scaled_size ); |
---|
[12796] | 522 | if ($crop_rect) |
---|
| 523 | { |
---|
| 524 | $changes++; |
---|
| 525 | $image->crop( $crop_rect->width(), $crop_rect->height(), $crop_rect->l, $crop_rect->t); |
---|
[12908] | 526 | $timing['crop'] = time_step($step); |
---|
[12796] | 527 | } |
---|
| 528 | |
---|
[12851] | 529 | if ($scaled_size) |
---|
[12796] | 530 | { |
---|
| 531 | $changes++; |
---|
[12851] | 532 | $image->resize( $scaled_size[0], $scaled_size[1] ); |
---|
| 533 | $d_size = $scaled_size; |
---|
[12908] | 534 | $timing['scale'] = time_step($step); |
---|
[12796] | 535 | } |
---|
| 536 | |
---|
[12851] | 537 | if ($params->sharpen) |
---|
| 538 | { |
---|
| 539 | $changes += $image->sharpen( $params->sharpen ); |
---|
[12908] | 540 | $timing['sharpen'] = time_step($step); |
---|
[12851] | 541 | } |
---|
| 542 | |
---|
| 543 | if ($params->use_watermark) |
---|
| 544 | { |
---|
| 545 | $wm = ImageStdParams::get_watermark(); |
---|
| 546 | $wm_image = new pwg_image(PHPWG_ROOT_PATH.$wm->file); |
---|
| 547 | $wm_size = array($wm_image->get_width(),$wm_image->get_height()); |
---|
| 548 | if ($d_size[0]<$wm_size[0] or $d_size[1]<$wm_size[1]) |
---|
| 549 | { |
---|
| 550 | $wm_scaling_params = SizingParams::classic($d_size[0], $d_size[1]); |
---|
| 551 | $wm_scaling_params->compute($wm_size, null, $tmp, $wm_scaled_size); |
---|
| 552 | $wm_size = $wm_scaled_size; |
---|
| 553 | $wm_image->resize( $wm_scaled_size[0], $wm_scaled_size[1] ); |
---|
| 554 | } |
---|
| 555 | $x = round( ($wm->xpos/100)*($d_size[0]-$wm_size[0]) ); |
---|
| 556 | $y = round( ($wm->ypos/100)*($d_size[1]-$wm_size[1]) ); |
---|
| 557 | if ($image->compose($wm_image, $x, $y, $wm->opacity)) |
---|
| 558 | { |
---|
| 559 | $changes++; |
---|
| 560 | if ($wm->xrepeat) |
---|
| 561 | { |
---|
| 562 | // todo |
---|
[12865] | 563 | $pad = $wm_size[0] + max(30, round($wm_size[0]/4)); |
---|
| 564 | for($i=-$wm->xrepeat; $i<=$wm->xrepeat; $i++) |
---|
| 565 | { |
---|
| 566 | if (!$i) continue; |
---|
| 567 | $x2 = $x + $i * $pad; |
---|
| 568 | if ($x2>=0 && $x2+$wm_size[0]<$d_size[0]) |
---|
| 569 | if (!$image->compose($wm_image, $x2, $y, $wm->opacity)) |
---|
| 570 | break; |
---|
| 571 | } |
---|
[12851] | 572 | } |
---|
| 573 | } |
---|
| 574 | $wm_image->destroy(); |
---|
[12908] | 575 | $timing['watermark'] = time_step($step); |
---|
[12851] | 576 | } |
---|
| 577 | |
---|
[12796] | 578 | // no change required - redirect to source |
---|
| 579 | if (!$changes) |
---|
| 580 | { |
---|
| 581 | header("X-i: No change"); |
---|
| 582 | ierror( $page['src_url'], 301); |
---|
| 583 | } |
---|
| 584 | |
---|
[13426] | 585 | if ($d_size[0]*$d_size[1] < 256000) |
---|
[12958] | 586 | {// strip metadata for small images |
---|
| 587 | $image->strip(); |
---|
| 588 | } |
---|
[13843] | 589 | |
---|
[14649] | 590 | $image->set_compression_quality( ImageStdParams::$quality ); |
---|
[12796] | 591 | $image->write( $page['derivative_path'] ); |
---|
| 592 | $image->destroy(); |
---|
[12908] | 593 | $timing['save'] = time_step($step); |
---|
[12796] | 594 | |
---|
[12820] | 595 | send_derivative($expires); |
---|
[12908] | 596 | $timing['send'] = time_step($step); |
---|
| 597 | |
---|
| 598 | ilog('perf', |
---|
| 599 | basename($page['src_path']), $o_size, $o_size[0]*$o_size[1], |
---|
| 600 | basename($page['derivative_path']), $d_size, $d_size[0]*$d_size[1], |
---|
[12920] | 601 | function_exists('memory_get_peak_usage') ? round( memory_get_peak_usage()/(1024*1024), 1) : '', |
---|
[12908] | 602 | time_step($begin), |
---|
[12920] | 603 | '|', $timing); |
---|
[12796] | 604 | ?> |
---|