fork download
  1. <?php
  2. class Thumbnail {
  3. public $name;
  4.  
  5. public $crop = false;
  6.  
  7. public $width;
  8. public $height;
  9.  
  10. public $maxWidth;
  11. public $maxHeight;
  12.  
  13. public $x = 0;
  14. public $y = 0;
  15.  
  16. public $cropX = 0;
  17. public $cropY = 0;
  18.  
  19. public $newWidth;
  20. public $newHeight;
  21.  
  22. public $newWidth_p;
  23. public $newHeight_p;
  24.  
  25. public function __construct($name, $maxWidth, $maxHeight, $crop) {
  26. $this->name = $name;
  27. $this->maxWidth = $maxWidth;
  28. $this->maxHeight = $maxHeight;
  29. $this->crop = $crop;
  30. }
  31.  
  32. public function calculateImage($directory) {
  33. list($this->width, $this->height) = getimagesize(__DIR__ . '/../uploads/' . $directory . '/' . $this->name);
  34.  
  35. if ($this->maxWidth == '' || $this->maxWidth > $this->width) $this->maxWidth = $this->width;
  36. if ($this->maxHeight == '' || $this->maxHeight > $this->height) $this->maxHeight = $this->height;
  37.  
  38. if ($this->crop) {
  39. $cropRatioX = $this->maxWidth / $this->width;
  40. $cropRatioY = $this->maxHeight / $this->height;
  41. $cropRatio = max(array($cropRatioX, $cropRatioY));
  42.  
  43. $this->newWidth = round($this->width * $cropRatio);
  44. $this->newHeight = round($this->height * $cropRatio);
  45.  
  46. $this->cropX = ($this->newWidth > $this->newHeight) ? ($this->newWidth - $this->maxWidth) / 2 : 0;
  47. $this->cropY = ($this->newHeight > $this->newWidth) ? ($this->newWidth - $this->maxWidth) / 2 : 0;
  48.  
  49. $this->x = ($this->maxWidth > $this->maxHeight) ? ($this->cropX / 2) * (-1) : 0;
  50. $this->y = ($this->maxHeight > $this->maxWidth) ? ($this->cropY / 2) * (-1) : 0;
  51.  
  52. $this->newWidth_p = $this->maxWidth;
  53. $this->newHeight_p = $this->maxHeight;
  54. } else {
  55. $ratioY = $this->maxWidth / $this->width;
  56. $ratioX = $this->maxHeight / $this->height;
  57. $ratio = min(array($ratioX, $ratioY));
  58.  
  59. $this->newWidth = round($this->width * $ratio);
  60. $this->newHeight = round($this->height * $ratio);
  61.  
  62. $this->newWidth_p = $this->newWidth;
  63. $this->newHeight_p = $this->newHeight;
  64. }
  65.  
  66. $thumbnailName = 'thumbnail' . $this->newWidth_p . 'x' . $this->newHeight_p . '_' . $name;
  67.  
  68. return array($newWidth_p, $newHeight_p, $thumbnailName);
  69. }
  70.  
  71. public function createThumbnail($thumbnailDirectory, $thumbnailName) {
  72. $image_p = imagecreatetruecolor($this->newWidth_p, $this->newHeight_p);
  73. $image = imagecreatefromjpeg(__DIR__ . '/../uploads/' . $directory . '/' . $this->name);
  74.  
  75. imagecopyresampled($image_p, $image, $this->x, $this->y, $this->cropX, $this->cropY, $this->newWidth, $this->newHeight, $this->width, $this->height);
  76.  
  77. imagejpeg($image_p, __DIR__ . '/../thumbnails/' . $thumbnailDirectory . '/' . $thumbnailName, 100);
  78. }
  79. }
  80. ?>
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
Standard output is empty