AbstractEncoder.php 5.5 KB

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