gd.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Grav\Plugin;
  3. require_once 'interface.php';
  4. /**
  5. * Class GDAdapter
  6. * @package Grav\Plugin
  7. */
  8. class GDAdapter implements ResizeAdapterInterface
  9. {
  10. private $image;
  11. private $target;
  12. private $format;
  13. private $quality;
  14. private $original_width;
  15. private $original_height;
  16. /**
  17. * Initiates a new GDAdapter instance
  18. * @param string $source - Source image path
  19. */
  20. public function __construct($source)
  21. {
  22. $size = getimagesize($source);
  23. $pathinfo = pathinfo($source);
  24. $extension = strtolower($pathinfo['extension']);
  25. $this->original_width = $size[0];
  26. $this->original_height = $size[1];
  27. if (preg_match('/jpe?g/', $extension)) {
  28. $this->image = imagecreatefromjpeg($source);
  29. $this->format = 'JPEG';
  30. } else if ($extension == 'png') {
  31. $this->image = imagecreatefrompng($source);
  32. $this->format = 'PNG';
  33. }
  34. return $this;
  35. }
  36. /**
  37. * Gets the image format
  38. * @return string - Either 'JPEG' or 'PNG'
  39. */
  40. public function getFormat()
  41. {
  42. return $this->format;
  43. }
  44. /**
  45. * Resizes the image to the specified dimensions
  46. * @param float $width
  47. * @param float $height
  48. * @return GDAdapter - Returns $this
  49. */
  50. public function resize($width, $height)
  51. {
  52. $this->target = imagecreatetruecolor($width, $height);
  53. $format = $this->getFormat();
  54. if ($format == 'PNG') {
  55. $transparent = imagecolorallocatealpha($this->target, 255, 255, 255, 127);
  56. imagealphablending($this->target, false);
  57. imagesavealpha($this->target, true);
  58. imagefilledrectangle($this->target, 0, 0, $width, $height, $transparent);
  59. }
  60. imagecopyresampled($this->target, $this->image, 0, 0, 0, 0, $width, $height, $this->original_width, $this->original_height);
  61. return $this;
  62. }
  63. /**
  64. * Sets JPEG quality of target image
  65. * @param int $quality
  66. * @return GDAdapter - Returns $this
  67. */
  68. public function setQuality($quality)
  69. {
  70. $this->quality = $quality;
  71. return $this;
  72. }
  73. /**
  74. * Generates image and saves it to disk
  75. * @param string $filename - Target filename for image
  76. * @return bool - Returns true if successful, false otherwise
  77. */
  78. public function save($filename)
  79. {
  80. $format = $this->getFormat();
  81. if ($format == 'JPEG') {
  82. $result = imagejpeg($this->target, $filename, $this->quality);
  83. } else if ($format == 'PNG') {
  84. $result = imagepng($this->target, $filename, 9);
  85. }
  86. imagedestroy($this->image);
  87. imagedestroy($this->target);
  88. return $result;
  89. }
  90. }