Advertisement
lucasgautheron

Untitled

Mar 20th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.37 KB | None | 0 0
  1. <?php
  2. header('Content-type: image/jpeg');
  3. define('IMAGES_PATH', './thumbs');
  4. define('TMP_PATH', './tmp');
  5.  
  6. define('MAX_WIDTH', 150);
  7. define('MAX_HEIGHT', 150);
  8.  
  9. if(empty($_GET['image'])) die('no image specified');
  10.  
  11. $outputname = IMAGES_PATH . '/' . md5($_GET['image']);
  12. if(file_exists($outputname))
  13. {
  14.     readfile($outputname);
  15.     die();
  16. }
  17.  
  18. $tmpname = TMP_PATH . '/' . uniqid();
  19.  
  20. $ch = curl_init(str_replace(" ", "%20", $_GET['image']));
  21. $fp = fopen($tmpname, 'wb');
  22. curl_setopt($ch, CURLOPT_FILE, $fp);
  23. curl_setopt($ch, CURLOPT_HEADER, 0);
  24. curl_exec($ch);
  25. fclose($fp);
  26. if(curl_errno($ch)) die();
  27.  
  28. $imagesize = @getimagesize($tmpname);
  29. if(!$imagesize) die('invalid image specified');
  30.  
  31. $source = null;
  32. $thumb = null;
  33. $thumb_width = MAX_WIDTH;
  34. $thumb_height = MAX_HEIGHT;
  35.  
  36. $source_width = $source_height = -1;
  37. $dest_x = $dest_y = 0;
  38. $source_x = $source_y = 0;
  39.  
  40. if($imagesize[0] <= MAX_WIDTH)
  41. {
  42.     $thumb_width = $source_width = $imagesize[0];
  43.     $thumb_height = $source_height = min($imagesize[1], MAX_HEIGHT);
  44.     $source_y = max($imagesize[1] - $source_height, 0) / 2;
  45. }
  46. else if($imagesize[1] <= MAX_HEIGHT)
  47. {
  48.     $thumb_height = $source_height = $imagesize[1];
  49.     $thumb_width = $source_width = min($imagesize[0], MAX_WIDTH);
  50.     $source_x = max($imagesize[0] - $source_width, 0) / 2;
  51. }
  52.  
  53. if($source_width <= 0)
  54. {
  55.     if($imagesize[0] >= $imagesize[1])
  56.     {
  57.         $source_height = $imagesize[1];
  58.         $source_width = (MAX_WIDTH/MAX_HEIGHT)*($source_height);
  59.         $source_x = max($imagesize[0] - $source_width, 0) / 2;
  60.     }
  61.     else
  62.     {
  63.         $source_width = $imagesize[0];
  64.         $source_height = (MAX_HEIGHT/MAX_WIDTH)*($source_width);
  65.         $source_y = max($imagesize[1] - $source_height, 0) / 2;
  66.     }
  67. }
  68.  
  69. switch($imagesize['mime'])
  70. {
  71.     case 'image/jpg':
  72.     case 'image/jpeg':
  73.         $source = imagecreatefromjpeg($tmpname);
  74.     break;
  75.    
  76.     case 'image/png':
  77.         $source = imagecreatefrompng($tmpname);
  78.     break;
  79.    
  80.     default: break;
  81. }
  82.  
  83. if(!$source) die('invalid image specified (mime type not supported)');
  84.  
  85. $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
  86. imagecopyresampled($thumb, $source, $dest_x, $dest_y, $source_x, $source_y, $thumb_width, $thumb_height, $source_width, $source_height);
  87.  
  88. imagejpeg($thumb, null, 100);
  89. imagejpeg($thumb, $outputname, 100);
  90. unlink($tmpname);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement