Guest User

Untitled

a guest
Jul 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. <?php
  2.  
  3. defined( '_JEXEC' ) or die( 'Restricted access' );
  4.  
  5. $p = strpos($_SERVER['REQUEST_URI'], '/photo/');
  6.  
  7. if($p !==false || $_GET['option']=='photo' ){
  8.  
  9. jimport('my.agency');
  10. jimport('my.flatUtils');
  11.  
  12. $q = explode('/', substr($_SERVER['REQUEST_URI'], $p+7));
  13. //print_r( $q );
  14.  
  15. switch( count($q) ){
  16. case 2:
  17. $type = $q[0];
  18. $name = $q[1];
  19. break;
  20. case 3:
  21. $type = $q[0];
  22. if(is_numeric($q[1]) || $q[1]=='small' || $q[1]=='middle')
  23. $size = $q[1];
  24. else
  25. $object = $q[1];
  26. $name = $q[2];
  27. break;
  28. case 4:
  29. $type = $q[0];
  30. $object = $q[1];
  31. $size = $q[2];
  32. $name = $q[3];
  33. break;
  34. }
  35.  
  36. list($path, $name) = parsePhotoName($name, $object);
  37.  
  38. $last = substr($path, -1);
  39. if($last != '/' || $last != '\\')
  40. $path .= DS;
  41.  
  42. $path .= 'photo'.DS.$type.DS;
  43.  
  44. //echo $path.'cooked'.DS.$name;
  45. //echo $path.'raw'.DS.$name;
  46. //exit;
  47.  
  48. if(!$type) $type = 'flat';
  49. $img_type = getPhotoExt($name);
  50. $raw_path = $path.'raw'.DS.$name;
  51.  
  52. if(!file_exists($raw_path)){
  53. header('Content-Type: image/jpeg');
  54. $mt = filemtime("photo/$type/none.gif");
  55. checkIfModifiedSince($mt);
  56. echo file_get_contents("photo/$type/none.gif");
  57. }
  58. else {
  59.  
  60. header('Content-Type: image/'.$img_type);
  61. if($size && $size != 'large'){
  62. list($w, $h) = translateSize($size);
  63. $dir = $w.'x'.$h;
  64. $cooked_path = $path.'cooked'.DS.$dir.DS.$name;
  65. if(!file_exists($cooked_path)){
  66. mkdir($path.'cooked'.DS.$dir, 0777, true);
  67. createthumb($raw_path, $cooked_path, $w, $h, $img_type);
  68. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  69. }
  70. else {
  71. $mt = filemtime($cooked_path);
  72. checkIfModifiedSince($mt);
  73. }
  74. echo file_get_contents($cooked_path);
  75. }
  76. else {
  77. $mt = filemtime($raw_path);
  78. checkIfModifiedSince($mt);
  79. echo file_get_contents($raw_path);
  80. }
  81. }
  82.  
  83. exit;
  84. }
  85.  
  86. function date2unixstamp($s) {
  87. $months = array (
  88. 'Jan' => 1, 'Feb' => 2, 'Mar' =>3,
  89. 'Apr' => 4, 'May' => 5, 'Jun' =>6,
  90. 'Jul' => 7, 'Aug' => 8, 'Sep' =>9,
  91. 'Oct' => 10, 'Nov' => 11, 'Dec' =>12
  92. );
  93. $a = explode(' ', $s);
  94. $b = explode(':', $a[4]);
  95. return gmmktime($b[0], $b[1], $b[2], $months[$a[2]], $a[1], $a[3]);
  96. }
  97.  
  98. function checkIfModifiedSince($mt){
  99. $mt_str = gmdate('D, d M Y H:i:s', $mt).' GMT';
  100. if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  101. $cache_mt = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
  102. if (date2unixstamp($cache_mt) >= $mt) {
  103. header('HTTP/1.1 304 Not Modified');
  104. exit;
  105. }
  106. }
  107. header('Last-Modified: '.$mt_str);
  108. }
  109.  
  110. function translateSize($size){
  111. switch($size){
  112. case 'small': return array(80, 80);
  113. case 'middle': return array(150, 150);
  114. default: {
  115. if($size>1000) $size = 1000;
  116. return array($size, $size);
  117. }
  118. }
  119. }
  120.  
  121. function parsePhotoName($name, $object = null){
  122.  
  123. if(strpos($name, '.')!==false){
  124. if($object){
  125. list($ag_code) = FlatUtils::parseFlatId($object);
  126. return array( Agency::getPath($ag_code), $name);
  127. }
  128. return array( JPATH_BASE, $name);
  129. }
  130.  
  131. list($id, $num) = explode('-', $name);
  132. if(!is_numeric($num)) $num = 1;
  133. $name = $id.'_'.$num.'.jpg';
  134.  
  135. list($ag_code) = FlatUtils::parseFlatId($id);
  136.  
  137. return array( Agency::getPath($ag_code), $name );
  138. }
  139.  
  140. function getPhotoExt($name) {
  141. preg_match("/\\.([^\\.]+)$/", $name, $m);
  142. return strtolower($m[1]);
  143. }
  144.  
  145. function createthumb($name, $filename, $new_w = 80, $new_h = 80, $type = "jpg"){
  146. switch($type){
  147. case 'jpg':
  148. case 'jpeg':
  149. $src_img = imagecreatefromjpeg($name);
  150. break;
  151. case 'gif':
  152. $src_img = imagecreatefromgif($name);
  153. break;
  154. case 'png':
  155. $src_img = imagecreatefrompng($name);
  156. break;
  157. }
  158.  
  159. $old_x = imageSX($src_img);
  160. $old_y = imageSY($src_img);
  161. if ($old_x > $old_y) {
  162. $thumb_w = $new_w;
  163. $percent = ($new_w * 100) / $old_x;
  164. $thumb_h = ($percent * $old_y) / 100;
  165. }
  166. else if ($old_x < $old_y) {
  167. $percent = ($new_h * 100) / $old_y;
  168. $thumb_w = ($percent * $old_x) / 100;
  169.  
  170. $thumb_h = $new_h;
  171. }
  172. else {
  173. $thumb_w = $new_w;
  174. $thumb_h = $new_h;
  175. }
  176.  
  177. $dst_img = ImageCreateTrueColor($thumb_w,$thumb_h);
  178. imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
  179.  
  180. switch($type){
  181. case 'jpg':
  182. case 'jpeg':
  183. imagejpeg($dst_img,$filename);
  184. break;
  185. case 'gif':
  186. imagegif($dst_img,$filename);
  187. break;
  188. case 'png':
  189. imagepng($dst_img,$filename);
  190. break;
  191. }
  192.  
  193. imagedestroy($dst_img);
  194. imagedestroy($src_img);
  195. }
  196. ?>
Add Comment
Please, Sign In to add comment