AbstractDecoder.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace Intervention\Image;
  3. abstract class AbstractDecoder
  4. {
  5. /**
  6. * Initiates new image from path in filesystem
  7. *
  8. * @param string $path
  9. * @return \Intervention\Image\Image
  10. */
  11. abstract public function initFromPath($path);
  12. /**
  13. * Initiates new image from binary data
  14. *
  15. * @param string $data
  16. * @return \Intervention\Image\Image
  17. */
  18. abstract public function initFromBinary($data);
  19. /**
  20. * Initiates new image from GD resource
  21. *
  22. * @param Resource $resource
  23. * @return \Intervention\Image\Image
  24. */
  25. abstract public function initFromGdResource($resource);
  26. /**
  27. * Initiates new image from Imagick object
  28. *
  29. * @param Imagick $object
  30. * @return \Intervention\Image\Image
  31. */
  32. abstract public function initFromImagick(\Imagick $object);
  33. /**
  34. * Buffer of input data
  35. *
  36. * @var mixed
  37. */
  38. private $data;
  39. /**
  40. * Creates new Decoder with data
  41. *
  42. * @param mixed $data
  43. */
  44. public function __construct($data = null)
  45. {
  46. $this->data = $data;
  47. }
  48. /**
  49. * Init from fiven URL
  50. *
  51. * @param string $url
  52. * @return \Intervention\Image\Image
  53. */
  54. public function initFromUrl($url)
  55. {
  56. if ($data = @file_get_contents($url)) {
  57. return $this->initFromBinary($data);
  58. }
  59. throw new \Intervention\Image\Exception\NotReadableException(
  60. "Unable to init from given url (".$url.")."
  61. );
  62. }
  63. /**
  64. * Determines if current source data is GD resource
  65. *
  66. * @return boolean
  67. */
  68. public function isGdResource()
  69. {
  70. if (is_resource($this->data)) {
  71. return (get_resource_type($this->data) == 'gd');
  72. }
  73. return false;
  74. }
  75. /**
  76. * Determines if current source data is Imagick object
  77. *
  78. * @return boolean
  79. */
  80. public function isImagick()
  81. {
  82. return is_a($this->data, 'Imagick');
  83. }
  84. /**
  85. * Determines if current source data is Intervention\Image\Image object
  86. *
  87. * @return boolean
  88. */
  89. public function isInterventionImage()
  90. {
  91. return is_a($this->data, '\Intervention\Image\Image');
  92. }
  93. /**
  94. * Determines if current data is SplFileInfo object
  95. *
  96. * @return boolean
  97. */
  98. public function isSplFileInfo()
  99. {
  100. return is_a($this->data, 'SplFileInfo');
  101. }
  102. /**
  103. * Determines if current data is Symfony UploadedFile component
  104. *
  105. * @return boolean
  106. */
  107. public function isSymfonyUpload()
  108. {
  109. return is_a($this->data, 'Symfony\Component\HttpFoundation\File\UploadedFile');
  110. }
  111. /**
  112. * Determines if current source data is file path
  113. *
  114. * @return boolean
  115. */
  116. public function isFilePath()
  117. {
  118. if (is_string($this->data)) {
  119. return is_file($this->data);
  120. }
  121. return false;
  122. }
  123. /**
  124. * Determines if current source data is url
  125. *
  126. * @return boolean
  127. */
  128. public function isUrl()
  129. {
  130. return (bool) filter_var($this->data, FILTER_VALIDATE_URL);
  131. }
  132. /**
  133. * Determines if current source data is binary data
  134. *
  135. * @return boolean
  136. */
  137. public function isBinary()
  138. {
  139. if (is_string($this->data)) {
  140. $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $this->data);
  141. return (substr($mime, 0, 4) != 'text' && $mime != 'application/x-empty');
  142. }
  143. return false;
  144. }
  145. /**
  146. * Determines if current source data is data-url
  147. *
  148. * @return boolean
  149. */
  150. public function isDataUrl()
  151. {
  152. $data = $this->decodeDataUrl($this->data);
  153. return is_null($data) ? false : true;
  154. }
  155. /**
  156. * Determines if current source data is base64 encoded
  157. *
  158. * @return boolean
  159. */
  160. public function isBase64()
  161. {
  162. return base64_encode(base64_decode($this->data)) === $this->data;
  163. }
  164. /**
  165. * Initiates new Image from Intervention\Image\Image
  166. *
  167. * @param Image $object
  168. * @return \Intervention\Image\Image
  169. */
  170. public function initFromInterventionImage($object)
  171. {
  172. return $object;
  173. }
  174. /**
  175. * Parses and decodes binary image data from data-url
  176. *
  177. * @param string $data_url
  178. * @return string
  179. */
  180. private function decodeDataUrl($data_url)
  181. {
  182. $pattern = "/^data:(?:image\/[a-zA-Z\-\.]+)(?:charset=\".+\")?;base64,(?P<data>.+)$/";
  183. preg_match($pattern, $data_url, $matches);
  184. if (is_array($matches) && array_key_exists('data', $matches)) {
  185. return base64_decode($matches['data']);
  186. }
  187. return null;
  188. }
  189. /**
  190. * Initiates new image from mixed data
  191. *
  192. * @param mixed $data
  193. * @return \Intervention\Image\Image
  194. */
  195. public function init($data)
  196. {
  197. $this->data = $data;
  198. switch (true) {
  199. case $this->isGdResource():
  200. return $this->initFromGdResource($this->data);
  201. case $this->isImagick():
  202. return $this->initFromImagick($this->data);
  203. case $this->isInterventionImage():
  204. return $this->initFromInterventionImage($this->data);
  205. case $this->isSplFileInfo():
  206. return $this->initFromPath($this->data->getRealPath());
  207. case $this->isBinary():
  208. return $this->initFromBinary($this->data);
  209. case $this->isUrl():
  210. return $this->initFromUrl($this->data);
  211. case $this->isFilePath():
  212. return $this->initFromPath($this->data);
  213. case $this->isDataUrl():
  214. return $this->initFromBinary($this->decodeDataUrl($this->data));
  215. case $this->isBase64():
  216. return $this->initFromBinary(base64_decode($this->data));
  217. default:
  218. throw new Exception\NotReadableException("Image source not readable");
  219. }
  220. }
  221. /**
  222. * Decoder object transforms to string source data
  223. *
  224. * @return string
  225. */
  226. public function __toString()
  227. {
  228. return (string) $this->data;
  229. }
  230. }