Advertisement
Guest User

Untitled

a guest
May 31st, 2012
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.01 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Generate new size and save images to cache
  4.  * @param $_GET['id'] id of image
  5.  * @param $_GET['x'] new image width
  6.  * @param $_GET['y'] new image height
  7.  * @param $_GET['type'] 0-fit  or 1-resize
  8.  * @param $_GET['grey'] 0-nomral 1-greyscale
  9.  */
  10. $id       = preg_replace('#[^a-zA-Z0-9_/\.]#', '', $_GET['id']);
  11. $id       = str_replace('..', '', $id);
  12. $x        = (int) $_GET['x'];
  13. $y        = (int) $_GET['y'];
  14. $type     = (int) $_GET['type'];
  15. $filename = '/var/www/static/cache' . $id . '-' . $x . '-' . $y . '-' . $type . '.jpg';
  16. require_once 'Image/Transform.php';
  17.  
  18. function chceckError($item)
  19. {
  20.     if (PEAR::isError($item))
  21.     {
  22.         header("HTTP/1.0 404 Not Found");
  23.         die();
  24.     }
  25. }
  26.  
  27. $it = Image_Transform::factory('GD');
  28. print_r($it);
  29. //chceckError($it);
  30. if (PEAR::isError($it->load('orginal/' . $id . '.jpg')))
  31. {
  32.     chceckError($it->load('no_image.jpg'));
  33. }
  34.  
  35.  
  36. switch ($type)
  37. {
  38.  
  39.     case 0:
  40.         if ($x == 0)
  41.             $x             = 10000;
  42.         if ($y == 0)
  43.             $y             = 10000;
  44.         chceckError($it->fit($x, $y));
  45.         break;
  46.     case 1:
  47.         $ratio         = $x / $y;
  48.         $orginal_ratio = ($it->getImageWidth() / $it->getImageHeight());
  49.  
  50.         if ($orginal_ratio < $ratio)
  51.         {
  52.             chceckError($it->scaleByX($x));
  53.             $starty = ($it->getNewImageHeight() - $y) / 3;
  54.             chceckError($it->crop($x, $y, 0, $starty));
  55.         }
  56.         else
  57.         {
  58.             chceckError($it->scaleByY($y));
  59.             $startx = ($it->getNewImageWidth() - $x) / 2;
  60.             chceckError($it->crop($x, $y, $startx, 0));
  61.         }
  62.         break;
  63.     case 2:
  64.         break;
  65.     default:
  66.         header("HTTP/1.0 404 Not Found");
  67.         die();
  68. }
  69.  
  70. if ($_GET['grey'] == 1)
  71.     chceckError($it->greyscale());
  72.  
  73. $it2 = clone $it;
  74.  
  75. chceckError($it2->save($filename));
  76. $time = gmdate('D, d M Y H:i:s', filemtime($filename)) . ' GMT';
  77. header('Last-Modified: ' . $time);
  78. chceckError($it->display());
  79. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement