Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined( '_JEXEC' ) or die( 'Restricted access' );
- $p = strpos($_SERVER['REQUEST_URI'], '/photo/');
- if($p !==false || $_GET['option']=='photo' ){
- jimport('my.agency');
- jimport('my.flatUtils');
- $q = explode('/', substr($_SERVER['REQUEST_URI'], $p+7));
- //print_r( $q );
- switch( count($q) ){
- case 2:
- $type = $q[0];
- $name = $q[1];
- break;
- case 3:
- $type = $q[0];
- if(is_numeric($q[1]) || $q[1]=='small' || $q[1]=='middle')
- $size = $q[1];
- else
- $object = $q[1];
- $name = $q[2];
- break;
- case 4:
- $type = $q[0];
- $object = $q[1];
- $size = $q[2];
- $name = $q[3];
- break;
- }
- list($path, $name) = parsePhotoName($name, $object);
- $last = substr($path, -1);
- if($last != '/' || $last != '\\')
- $path .= DS;
- $path .= 'photo'.DS.$type.DS;
- //echo $path.'cooked'.DS.$name;
- //echo $path.'raw'.DS.$name;
- //exit;
- if(!$type) $type = 'flat';
- $img_type = getPhotoExt($name);
- $raw_path = $path.'raw'.DS.$name;
- if(!file_exists($raw_path)){
- header('Content-Type: image/jpeg');
- $mt = filemtime("photo/$type/none.gif");
- checkIfModifiedSince($mt);
- echo file_get_contents("photo/$type/none.gif");
- }
- else {
- header('Content-Type: image/'.$img_type);
- if($size && $size != 'large'){
- list($w, $h) = translateSize($size);
- $dir = $w.'x'.$h;
- $cooked_path = $path.'cooked'.DS.$dir.DS.$name;
- if(!file_exists($cooked_path)){
- mkdir($path.'cooked'.DS.$dir, 0777, true);
- createthumb($raw_path, $cooked_path, $w, $h, $img_type);
- header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
- }
- else {
- $mt = filemtime($cooked_path);
- checkIfModifiedSince($mt);
- }
- echo file_get_contents($cooked_path);
- }
- else {
- $mt = filemtime($raw_path);
- checkIfModifiedSince($mt);
- echo file_get_contents($raw_path);
- }
- }
- exit;
- }
- function date2unixstamp($s) {
- $months = array (
- 'Jan' => 1, 'Feb' => 2, 'Mar' =>3,
- 'Apr' => 4, 'May' => 5, 'Jun' =>6,
- 'Jul' => 7, 'Aug' => 8, 'Sep' =>9,
- 'Oct' => 10, 'Nov' => 11, 'Dec' =>12
- );
- $a = explode(' ', $s);
- $b = explode(':', $a[4]);
- return gmmktime($b[0], $b[1], $b[2], $months[$a[2]], $a[1], $a[3]);
- }
- function checkIfModifiedSince($mt){
- $mt_str = gmdate('D, d M Y H:i:s', $mt).' GMT';
- if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
- $cache_mt = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
- if (date2unixstamp($cache_mt) >= $mt) {
- header('HTTP/1.1 304 Not Modified');
- exit;
- }
- }
- header('Last-Modified: '.$mt_str);
- }
- function translateSize($size){
- switch($size){
- case 'small': return array(80, 80);
- case 'middle': return array(150, 150);
- default: {
- if($size>1000) $size = 1000;
- return array($size, $size);
- }
- }
- }
- function parsePhotoName($name, $object = null){
- if(strpos($name, '.')!==false){
- if($object){
- list($ag_code) = FlatUtils::parseFlatId($object);
- return array( Agency::getPath($ag_code), $name);
- }
- return array( JPATH_BASE, $name);
- }
- list($id, $num) = explode('-', $name);
- if(!is_numeric($num)) $num = 1;
- $name = $id.'_'.$num.'.jpg';
- list($ag_code) = FlatUtils::parseFlatId($id);
- return array( Agency::getPath($ag_code), $name );
- }
- function getPhotoExt($name) {
- preg_match("/\\.([^\\.]+)$/", $name, $m);
- return strtolower($m[1]);
- }
- function createthumb($name, $filename, $new_w = 80, $new_h = 80, $type = "jpg"){
- switch($type){
- case 'jpg':
- case 'jpeg':
- $src_img = imagecreatefromjpeg($name);
- break;
- case 'gif':
- $src_img = imagecreatefromgif($name);
- break;
- case 'png':
- $src_img = imagecreatefrompng($name);
- break;
- }
- $old_x = imageSX($src_img);
- $old_y = imageSY($src_img);
- if ($old_x > $old_y) {
- $thumb_w = $new_w;
- $percent = ($new_w * 100) / $old_x;
- $thumb_h = ($percent * $old_y) / 100;
- }
- else if ($old_x < $old_y) {
- $percent = ($new_h * 100) / $old_y;
- $thumb_w = ($percent * $old_x) / 100;
- $thumb_h = $new_h;
- }
- else {
- $thumb_w = $new_w;
- $thumb_h = $new_h;
- }
- $dst_img = ImageCreateTrueColor($thumb_w,$thumb_h);
- imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
- switch($type){
- case 'jpg':
- case 'jpeg':
- imagejpeg($dst_img,$filename);
- break;
- case 'gif':
- imagegif($dst_img,$filename);
- break;
- case 'png':
- imagepng($dst_img,$filename);
- break;
- }
- imagedestroy($dst_img);
- imagedestroy($src_img);
- }
- ?>
Add Comment
Please, Sign In to add comment