$key) { $req = $key; break; }*/ $page['root_path'] = PHPWG_ROOT_PATH; } $req = ltrim($req, '/'); !preg_match('#[^a-zA-Z0-9/_.-]#', $req) or ierror('Invalid chars in request', 400); $page['derivative_path'] = PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$req; $pos = strrpos($req, '.'); $pos!== false || ierror('Missing .', 400); $ext = substr($req, $pos); $req = substr($req, 0, $pos); $pos = strrpos($req, '-'); $pos!== false || ierror('Missing -', 400); $deriv = substr($req, $pos+1); $req = substr($req, 0, $pos); $deriv = explode('_', $deriv); foreach (ImageStdParams::get_defined_type_map() as $type => $params) { if (substr($type,0,2) == $deriv[0]) { $page['derivative_type'] = $type; $page['derivative_params'] = $params; break; } } if (!isset($page['derivative_type'])) { if (substr(IMG_CUSTOM,0,2) == $deriv[0]) { $page['derivative_type'] = IMG_CUSTOM; } else { ierror('Unknown parsing type', 400); } } array_shift($deriv); $page['coi'] = ''; if (count($deriv) && $deriv[0][0]=='c' && $deriv[0][1]=='i') { $page['coi'] = substr(array_shift($deriv), 2); preg_match('#^[a-z]{4}$#', $page['coi']) or ierror('Invalid center of interest', 400); } if ($page['derivative_type'] == IMG_CUSTOM) { try { $page['derivative_params'] = ImageParams::from_url_tokens($deriv); } catch (Exception $e) { ierror($e->getMessage(), 400); } } if ($req[0]!='g' && $req[0]!='u') $req = '../'.$req; $page['src_location'] = $req.$ext; $page['src_path'] = PHPWG_ROOT_PATH.$page['src_location']; $page['src_url'] = $page['root_path'].$page['src_location']; } $page=array(); include_once( dirname(__FILE__).'/include/derivative_params.inc.php'); include_once( dirname(__FILE__).'/include/derivative_std_params.inc.php'); ImageStdParams::load_from_file(); parse_request(); //var_export($page); $params = $page['derivative_params']; if ($params->sizing->ideal_size[0] < 20 or $params->sizing->ideal_size[1] < 20) { ierror('Invalid size', 400); } if ($params->sizing->max_crop < 0 or $params->sizing->max_crop > 1) { ierror('Invalid crop', 400); } include_once(PHPWG_ROOT_PATH . 'admin/include/image.class.php'); $image = new pwg_image($page['src_path']); if (!mkgetdir(dirname($page['derivative_path']))) { ierror("dir create error", 500); } $changes = 0; // todo rotate // Crop & scale $params->sizing->compute( array($image->get_width(),$image->get_height()), $page['coi'], $crop_rect, $scale_width ); if ($crop_rect) { $changes++; $image->crop( $crop_rect->width(), $crop_rect->height(), $crop_rect->l, $crop_rect->t); } if ($scale_width) { $changes++; $image->resize( $scale_width[0], $scale_width[1] ); } // no change required - redirect to source if (!$changes) { header("X-i: No change"); ierror( $page['src_url'], 301); } $image->write( $page['derivative_path'] ); $image->destroy(); $fp = fopen($page['derivative_path'], 'rb'); $fstat = fstat($fp); $gmt_mtime = gmdate('D, d M Y H:i:s', $fstat['mtime']).' GMT'; header('Last-Modified: '.$gmt_mtime); header('Content-length: '.$fstat['size']); header('Connection: close'); // todo send the right headers header("Content-Type: image/jpeg"); fpassthru($fp); exit; ?>