Encoder.php 3.0 KB

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