AbstractEncoder.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 integer
  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. * Process a given image
  67. *
  68. * @param Image $image
  69. * @param string $format
  70. * @param integer $quality
  71. * @return Image
  72. */
  73. public function process(Image $image, $format = null, $quality = null)
  74. {
  75. $this->setImage($image);
  76. $this->setFormat($format);
  77. $this->setQuality($quality);
  78. switch (strtolower($this->format)) {
  79. case 'data-url':
  80. $this->result = $this->processDataUrl();
  81. break;
  82. case 'gif':
  83. case 'image/gif':
  84. $this->result = $this->processGif();
  85. break;
  86. case 'png':
  87. case 'image/png':
  88. case 'image/x-png':
  89. $this->result = $this->processPng();
  90. break;
  91. case 'jpg':
  92. case 'jpeg':
  93. case 'image/jpg':
  94. case 'image/jpeg':
  95. case 'image/pjpeg':
  96. $this->result = $this->processJpeg();
  97. break;
  98. case 'tif':
  99. case 'tiff':
  100. case 'image/tiff':
  101. case 'image/tif':
  102. case 'image/x-tif':
  103. case 'image/x-tiff':
  104. $this->result = $this->processTiff();
  105. break;
  106. case 'bmp':
  107. case 'image/bmp':
  108. case 'image/ms-bmp':
  109. case 'image/x-bitmap':
  110. case 'image/x-bmp':
  111. case 'image/x-ms-bmp':
  112. case 'image/x-win-bitmap':
  113. case 'image/x-windows-bmp':
  114. case 'image/x-xbitmap':
  115. $this->result = $this->processBmp();
  116. break;
  117. case 'ico':
  118. case 'image/x-ico':
  119. case 'image/x-icon':
  120. case 'image/vnd.microsoft.icon':
  121. $this->result = $this->processIco();
  122. break;
  123. case 'psd':
  124. case 'image/vnd.adobe.photoshop':
  125. $this->result = $this->processPsd();
  126. break;
  127. default:
  128. throw new \Intervention\Image\Exception\NotSupportedException(
  129. "Encoding format ({$format}) is not supported."
  130. );
  131. }
  132. $this->setImage(null);
  133. return $image->setEncoded($this->result);
  134. }
  135. /**
  136. * Processes and returns encoded image as data-url string
  137. *
  138. * @return string
  139. */
  140. protected function processDataUrl()
  141. {
  142. $mime = $this->image->mime ? $this->image->mime : 'image/png';
  143. return sprintf('data:%s;base64,%s',
  144. $mime,
  145. base64_encode($this->process($this->image, $mime, $this->quality))
  146. );
  147. }
  148. /**
  149. * Sets image to process
  150. *
  151. * @param Image $image
  152. */
  153. protected function setImage($image)
  154. {
  155. $this->image = $image;
  156. }
  157. /**
  158. * Determines output format
  159. *
  160. * @param string $format
  161. */
  162. protected function setFormat($format = null)
  163. {
  164. if ($format == '' && $this->image instanceof Image) {
  165. $format = $this->image->mime;
  166. }
  167. $this->format = $format ? $format : 'jpg';
  168. return $this;
  169. }
  170. /**
  171. * Determines output quality
  172. *
  173. * @param integer $quality
  174. */
  175. protected function setQuality($quality)
  176. {
  177. $quality = is_null($quality) ? 90 : $quality;
  178. $quality = $quality === 0 ? 1 : $quality;
  179. if ($quality < 0 || $quality > 100) {
  180. throw new \Intervention\Image\Exception\InvalidArgumentException(
  181. 'Quality must range from 0 to 100.'
  182. );
  183. }
  184. $this->quality = intval($quality);
  185. return $this;
  186. }
  187. }