Encoder.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Intervention\Image\Gd;
  3. use Intervention\Image\Exception\NotSupportedException;
  4. class Encoder extends \Intervention\Image\AbstractEncoder
  5. {
  6. /**
  7. * Processes and returns encoded image as JPEG string
  8. *
  9. * @return string
  10. */
  11. protected function processJpeg()
  12. {
  13. ob_start();
  14. imagejpeg($this->image->getCore(), null, $this->quality);
  15. $this->image->mime = image_type_to_mime_type(IMAGETYPE_JPEG);
  16. $buffer = ob_get_contents();
  17. ob_end_clean();
  18. return $buffer;
  19. }
  20. /**
  21. * Processes and returns encoded image as PNG string
  22. *
  23. * @return string
  24. */
  25. protected function processPng()
  26. {
  27. ob_start();
  28. $resource = $this->image->getCore();
  29. imagealphablending($resource, false);
  30. imagesavealpha($resource, true);
  31. imagepng($resource, null, -1);
  32. $this->image->mime = image_type_to_mime_type(IMAGETYPE_PNG);
  33. $buffer = ob_get_contents();
  34. ob_end_clean();
  35. return $buffer;
  36. }
  37. /**
  38. * Processes and returns encoded image as GIF string
  39. *
  40. * @return string
  41. */
  42. protected function processGif()
  43. {
  44. ob_start();
  45. imagegif($this->image->getCore());
  46. $this->image->mime = image_type_to_mime_type(IMAGETYPE_GIF);
  47. $buffer = ob_get_contents();
  48. ob_end_clean();
  49. return $buffer;
  50. }
  51. protected function processWebp()
  52. {
  53. if ( ! function_exists('imagewebp')) {
  54. throw new NotSupportedException(
  55. "Webp format is not supported by PHP installation."
  56. );
  57. }
  58. ob_start();
  59. imagewebp($this->image->getCore(), null, $this->quality);
  60. $this->image->mime = defined('IMAGETYPE_WEBP') ? image_type_to_mime_type(IMAGETYPE_WEBP) : 'image/webp';
  61. $buffer = ob_get_contents();
  62. ob_end_clean();
  63. return $buffer;
  64. }
  65. /**
  66. * Processes and returns encoded image as TIFF string
  67. *
  68. * @return string
  69. */
  70. protected function processTiff()
  71. {
  72. throw new NotSupportedException(
  73. "TIFF format is not supported by Gd Driver."
  74. );
  75. }
  76. /**
  77. * Processes and returns encoded image as BMP string
  78. *
  79. * @return string
  80. */
  81. protected function processBmp()
  82. {
  83. throw new NotSupportedException(
  84. "BMP format is not supported by Gd Driver."
  85. );
  86. }
  87. /**
  88. * Processes and returns encoded image as ICO string
  89. *
  90. * @return string
  91. */
  92. protected function processIco()
  93. {
  94. throw new NotSupportedException(
  95. "ICO format is not supported by Gd Driver."
  96. );
  97. }
  98. /**
  99. * Processes and returns encoded image as PSD string
  100. *
  101. * @return string
  102. */
  103. protected function processPsd()
  104. {
  105. throw new NotSupportedException(
  106. "PSD format is not supported by Gd Driver."
  107. );
  108. }
  109. }