AbstractEncoder.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace Intervention\Image;
  3. use Intervention\Image\Exception\InvalidArgumentException;
  4. use Intervention\Image\Exception\NotSupportedException;
  5. abstract class AbstractEncoder
  6. {
  7. /**
  8. * Buffer of encode result data
  9. *
  10. * @var string
  11. */
  12. public $result;
  13. /**
  14. * Image object to encode
  15. *
  16. * @var Image
  17. */
  18. public $image;
  19. /**
  20. * Output format of encoder instance
  21. *
  22. * @var string
  23. */
  24. public $format;
  25. /**
  26. * Output quality of encoder instance
  27. *
  28. * @var int
  29. */
  30. public $quality;
  31. /**
  32. * Processes and returns encoded image as JPEG string
  33. *
  34. * @return string
  35. */
  36. abstract protected function processJpeg();
  37. /**
  38. * Processes and returns encoded image as PNG string
  39. *
  40. * @return string
  41. */
  42. abstract protected function processPng();
  43. /**
  44. * Processes and returns encoded image as GIF string
  45. *
  46. * @return string
  47. */
  48. abstract protected function processGif();
  49. /**
  50. * Processes and returns encoded image as TIFF string
  51. *
  52. * @return string
  53. */
  54. abstract protected function processTiff();
  55. /**
  56. * Processes and returns encoded image as BMP string
  57. *
  58. * @return string
  59. */
  60. abstract protected function processBmp();
  61. /**
  62. * Processes and returns encoded image as ICO string
  63. *
  64. * @return string
  65. */
  66. abstract protected function processIco();
  67. /**
  68. * Processes and returns image as WebP encoded string
  69. *
  70. * @return string
  71. */
  72. abstract protected function processWebp();
  73. /**
  74. * Process a given image
  75. *
  76. * @param Image $image
  77. * @param string $format
  78. * @param int $quality
  79. * @return Image
  80. */
  81. public function process(Image $image, $format = null, $quality = null)
  82. {
  83. $this->setImage($image);
  84. $this->setFormat($format);
  85. $this->setQuality($quality);
  86. switch (strtolower($this->format)) {
  87. case 'data-url':
  88. $this->result = $this->processDataUrl();
  89. break;
  90. case 'gif':
  91. case 'image/gif':
  92. $this->result = $this->processGif();
  93. break;
  94. case 'png':
  95. case 'image/png':
  96. case 'image/x-png':
  97. $this->result = $this->processPng();
  98. break;
  99. case 'jpg':
  100. case 'jpeg':
  101. case 'image/jpg':
  102. case 'image/jpeg':
  103. case 'image/pjpeg':
  104. $this->result = $this->processJpeg();
  105. break;
  106. case 'tif':
  107. case 'tiff':
  108. case 'image/tiff':
  109. case 'image/tif':
  110. case 'image/x-tif':
  111. case 'image/x-tiff':
  112. $this->result = $this->processTiff();
  113. break;
  114. case 'bmp':
  115. case 'bmp':
  116. case 'ms-bmp':
  117. case 'x-bitmap':
  118. case 'x-bmp':
  119. case 'x-ms-bmp':
  120. case 'x-win-bitmap':
  121. case 'x-windows-bmp':
  122. case 'x-xbitmap':
  123. case 'image/ms-bmp':
  124. case 'image/x-bitmap':
  125. case 'image/x-bmp':
  126. case 'image/x-ms-bmp':
  127. case 'image/x-win-bitmap':
  128. case 'image/x-windows-bmp':
  129. case 'image/x-xbitmap':
  130. $this->result = $this->processBmp();
  131. break;
  132. case 'ico':
  133. case 'image/x-ico':
  134. case 'image/x-icon':
  135. case 'image/vnd.microsoft.icon':
  136. $this->result = $this->processIco();
  137. break;
  138. case 'psd':
  139. case 'image/vnd.adobe.photoshop':
  140. $this->result = $this->processPsd();
  141. break;
  142. case 'webp':
  143. case 'image/webp':
  144. case 'image/x-webp':
  145. $this->result = $this->processWebp();
  146. break;
  147. default:
  148. throw new NotSupportedException(
  149. "Encoding format ({$format}) is not supported."
  150. );
  151. }
  152. $this->setImage(null);
  153. return $image->setEncoded($this->result);
  154. }
  155. /**
  156. * Processes and returns encoded image as data-url string
  157. *
  158. * @return string
  159. */
  160. protected function processDataUrl()
  161. {
  162. $mime = $this->image->mime ? $this->image->mime : 'image/png';
  163. return sprintf('data:%s;base64,%s',
  164. $mime,
  165. base64_encode($this->process($this->image, $mime, $this->quality))
  166. );
  167. }
  168. /**
  169. * Sets image to process
  170. *
  171. * @param Image $image
  172. */
  173. protected function setImage($image)
  174. {
  175. $this->image = $image;
  176. }
  177. /**
  178. * Determines output format
  179. *
  180. * @param string $format
  181. */
  182. protected function setFormat($format = null)
  183. {
  184. if ($format == '' && $this->image instanceof Image) {
  185. $format = $this->image->mime;
  186. }
  187. $this->format = $format ? $format : 'jpg';
  188. return $this;
  189. }
  190. /**
  191. * Determines output quality
  192. *
  193. * @param int $quality
  194. */
  195. protected function setQuality($quality)
  196. {
  197. $quality = is_null($quality) ? 90 : $quality;
  198. $quality = $quality === 0 ? 1 : $quality;
  199. if ($quality < 0 || $quality > 100) {
  200. throw new InvalidArgumentException(
  201. 'Quality must range from 0 to 100.'
  202. );
  203. }
  204. $this->quality = intval($quality);
  205. return $this;
  206. }
  207. }