Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

zash

By: a guest on Dec 6th, 2009  |  syntax: PHP  |  size: 2.79 KB  |  views: 46  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. include 'global.php';
  3.  
  4. require_once 'mcw.php';
  5. $cache = new Filecacher('cache');
  6.  
  7. function notfound() {
  8.         header('HTTP/1.1 404 Not found');
  9.         require 'HTTP.php';
  10.         switch(HTTP::negotiateMimeType(array('text/html','text/plain','image/png'),'text/plain')) {
  11.         case 'text/plain':
  12.                 header('Content-Type: text/plain');
  13.                 echo "Not found\n";
  14.                 break;
  15.  
  16.         case 'text/html':
  17.                 header('Content-Type: text/html');
  18.                 readfile('/home/www/status/404.html');
  19.                 break;
  20.  
  21.         case 'image/png':
  22.                 header('Content-Type: image/png');
  23.                 readfile('/home/www/status/404.png');
  24.         }
  25.         exit;
  26. }
  27.  
  28. if(file_exists(IMG_DIR . $_SERVER['REQUEST_URI'])) {
  29.         header(sprintf('Location: %s', IMG_DIR . $_SERVER['REQUEST_URI']));
  30.         exit;
  31. }
  32.  
  33. $thumbname = ltrim($_SERVER['REQUEST_URI'], '/');
  34.  
  35. if(!preg_match('/^'.THMB_DIR.'\//', $thumbname))
  36.         notfound();
  37.  
  38. $filename = $cache->thumbs[$thumbname];
  39.  
  40. if(!file_exists($filename) or !is_readable($filename))
  41.         notfound();
  42.  
  43. $size = getimagesize($filename) or 0;
  44. $mimetype = $size['mime'];
  45.  
  46. $exif_data = exif_read_data($filename);
  47.  
  48. $mime = explode('/', $mimetype);
  49. switch($mime[0]) {
  50.         case 'image':
  51.         switch($mime[1]) {
  52.                 case 'jpeg':
  53.                 case 'tiff':
  54.                 if($ximage = exif_thumbnail($filename, $xwidth, $xheight, $xtype)) {
  55.                         $source = imagecreatefromstring($ximage);
  56.                         $size = array($xwidth, $xheight);
  57.                         break;
  58.                 }
  59.                 $source = imagecreatefromjpeg($filename);
  60.                 break;
  61.  
  62.                 case 'gif':
  63.                 $source = imagecreatefromgif($filename);
  64.                 break;
  65.  
  66.                 case 'png':
  67.                 $source = imagecreatefrompng($filename);
  68.                 break;
  69.  
  70.                 case 'x-xpixmap':
  71.                 case 'x-xpm':
  72.                 case 'x-xpmi':
  73.                 $source = imagecreatefromxpm($filename);
  74.                 break;
  75.  
  76.                 case 'x-xbitmap':
  77.                 case 'x-xbm':
  78.                 $source = imagecreatefromxbm($filename);
  79.                 break;
  80.         }
  81.         break;
  82.  
  83.         default:
  84. }
  85.  
  86. list($width, $height) = $size;
  87.  
  88. list($t_width, $t_height) = thmb_size($size);
  89.  
  90. $thumb = imagecreatetruecolor($t_width, $t_height);
  91. if(THMB_POSTFIX == '.png') {
  92.         imagealphablending($thumb, true);
  93.         imagesavealpha($thumb, true);
  94. }
  95.  
  96. imagefill($thumb, 0, 0, THMB_POSTFIX == '.png' ? 0x7FFFFFFF : 0xFFFFFF);
  97. imagecopyresampled($thumb, $source, 0, 0, 0, 0, $t_width, $t_height, $width, $height);
  98. #error_log(__FILE__ . ':' . memory_get_usage());
  99. if(isset($exif_data['Orientation']) and $exif_data['Orientation'] > 1 and function_exists('imagerotate')) {
  100.         switch($exif_data['Orientation']) {
  101.                 case 6:
  102.                 $thumb = imagerotate($thumb, 270, 0x7FFFFFFF);
  103.                 break;
  104.  
  105.                 case 8:
  106.                 $thumb = imagerotate($thumb, 90, 0x7FFFFFFF);
  107.                 break;
  108.         }
  109. }
  110.  
  111. switch(strtolower(THMB_POSTFIX)) {
  112.         default:
  113.         case '.png':
  114.         imagepng($thumb, $thumbname, THMB_QUALITY);
  115.         header('Content-Type: image/png');
  116.         break;
  117.  
  118.         case '.jpg':
  119.         case '.jpeg':
  120.         imagejpeg($thumb, $thumbname, THMB_QUALITY);
  121.         header('Content-Type: image/jpeg');
  122.         break;
  123. }
  124.  
  125. header('HTTP/1.1 201 Created');
  126. imagedestroy($thumb);
  127. readfile($thumbname);
clone this paste RAW Paste Data