fork download
  1. <?php
  2. namespace PreviewGenerator;
  3.  
  4. use WideImage\WideImage;
  5. use WideImage\Image;
  6.  
  7. /*
  8. $pg = new PreviewGenerator;
  9. $image = WideImage::load('image.jpg');
  10. $wm = WideImage::load('logo.png');
  11. $pg->putWatermark($image, $wm)->saveToFile('image.jpg');
  12.  
  13. $pg->create($image, 240, 180)->saveToFile('preview.jpg');
  14. */
  15.  
  16. class PreviewGenerator
  17. {
  18. const PREVIEW_WIDTH = 160;
  19. const PREVIEW_HEIGHT = 120;
  20. const BG_RED = 255;
  21. const BG_GREEN = 255;
  22. const BG_BLUE = 255;
  23. const WM_RATIO = 0.2;
  24.  
  25. private $previewWidth;
  26. private $previewHeight;
  27. private $image;
  28.  
  29. public function create(
  30. Image $image,
  31. $width = self::PREVIEW_WIDTH,
  32. $height = self::PREVIEW_HEIGHT,
  33. $red = self::BG_RED,
  34. $green = self::BG_GREEN,
  35. $blue = self::BG_BLUE
  36. ) {
  37. $this->previewWidth = $width;
  38. $this->previewHeight = $height;
  39. $this->image = $image;
  40.  
  41. $bg = WideImage::createPaletteImage($width, $height);
  42. $bgColor = $bg->allocateColor($red, $green, $blue);
  43. $bg->fill(0, 0, $bgColor);
  44.  
  45. if ($this->tooSmall()) {
  46. $preview = $bg->merge($this->image);
  47. } elseif ($this->isWide()) {
  48. $resized = $this->image->resizeDown($this->previewWidth);
  49. $preview = $this->bg->merge(
  50. $resized, 0, $this->getTopOffset($resized)
  51. );
  52. } else {
  53. $resized = $this->image->resizeDown(null, $this->previewHeight);
  54. $preview = $bg->merge(
  55. $resized, $this->getLeftOffset($resized), 0
  56. );
  57. }
  58. return $preview;
  59. }
  60.  
  61. public function putWatermark(
  62. Image $image,
  63. Image $wm,
  64. $opacity = 100,
  65. $wmRatio = self::WM_RATIO,
  66. $margin = 10
  67. ) {
  68. $desiredWidth = intval($this->getSmallerSide() * $wmRatio);
  69. $wmResized = $wm->resizeDown($desiredWidth);
  70. return $image->merge(
  71. $wmResized,
  72. $this->image->getWidth() - $wmResized->getWidth() - $margin,
  73. $this->image->getHeight() - $wmResized->getHeight() - $margin,
  74. $opacity
  75. );
  76. }
  77.  
  78. private function isWide()
  79. {
  80. return $this->imageRatio() >= $this->previewRatio();
  81. }
  82.  
  83. private function imageRatio()
  84. {
  85. return $this->image->getWidth()
  86. / $this->image->getHeight();
  87. }
  88.  
  89. private function previewRatio()
  90. {
  91. return $this->previewWidth / $this->previewHeight;
  92. }
  93.  
  94. private function tooSmall()
  95. {
  96. return ($this->image->getWidth() <= $this->previewWidth)
  97. and ($this->image->getHeight() <= $this->previewHeight);
  98. }
  99.  
  100. private function getSmallerSide()
  101. {
  102. if ($this->image->getWidth() < $this->image->getHeight()) {
  103. return $this->image->getWidth();
  104. } else {
  105. return $this->image->getHeight();
  106. }
  107. }
  108.  
  109. private function getTopOffset($resized)
  110. {
  111. return intval(
  112. ($this->previewHeight - $resized->getHeight()) / 2
  113. );
  114. }
  115.  
  116. private function getLeftOffset($resized)
  117. {
  118. return intval(
  119. ($this->previewWidth - $resized->getWidth()) / 2
  120. );
  121. }
  122. }
  123.  
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
Standard output is empty