AbstractDecoder.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. * Init from given stream
  65. *
  66. * @param $stream
  67. * @return \Intervention\Image\Image
  68. */
  69. public function initFromStream($stream)
  70. {
  71. $offset = ftell($stream);
  72. rewind($stream);
  73. $data = @stream_get_contents($stream);
  74. fseek($stream, $offset);
  75. if ($data) {
  76. return $this->initFromBinary($data);
  77. }
  78. throw new \Intervention\Image\Exception\NotReadableException(
  79. "Unable to init from given stream"
  80. );
  81. }
  82. /**
  83. * Determines if current source data is GD resource
  84. *
  85. * @return boolean
  86. */
  87. public function isGdResource()
  88. {
  89. if (is_resource($this->data)) {
  90. return (get_resource_type($this->data) == 'gd');
  91. }
  92. return false;
  93. }
  94. /**
  95. * Determines if current source data is Imagick object
  96. *
  97. * @return boolean
  98. */
  99. public function isImagick()
  100. {
  101. return is_a($this->data, 'Imagick');
  102. }
  103. /**
  104. * Determines if current source data is Intervention\Image\Image object
  105. *
  106. * @return boolean
  107. */
  108. public function isInterventionImage()
  109. {
  110. return is_a($this->data, '\Intervention\Image\Image');
  111. }
  112. /**
  113. * Determines if current data is SplFileInfo object
  114. *
  115. * @return boolean
  116. */
  117. public function isSplFileInfo()
  118. {
  119. return is_a($this->data, 'SplFileInfo');
  120. }
  121. /**
  122. * Determines if current data is Symfony UploadedFile component
  123. *
  124. * @return boolean
  125. */
  126. public function isSymfonyUpload()
  127. {
  128. return is_a($this->data, 'Symfony\Component\HttpFoundation\File\UploadedFile');
  129. }
  130. /**
  131. * Determines if current source data is file path
  132. *
  133. * @return boolean
  134. */
  135. public function isFilePath()
  136. {
  137. if (is_string($this->data)) {
  138. return is_file($this->data);
  139. }
  140. return false;
  141. }
  142. /**
  143. * Determines if current source data is url
  144. *
  145. * @return boolean
  146. */
  147. public function isUrl()
  148. {
  149. return (bool) filter_var($this->data, FILTER_VALIDATE_URL);
  150. }
  151. /**
  152. * Determines if current source data is a stream resource
  153. *
  154. * @return boolean
  155. */
  156. public function isStream()
  157. {
  158. if (!is_resource($this->data)) return false;
  159. if (get_resource_type($this->data) !== 'stream') return false;
  160. return true;
  161. }
  162. /**
  163. * Determines if current source data is binary data
  164. *
  165. * @return boolean
  166. */
  167. public function isBinary()
  168. {
  169. if (is_string($this->data)) {
  170. $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $this->data);
  171. return (substr($mime, 0, 4) != 'text' && $mime != 'application/x-empty');
  172. }
  173. return false;
  174. }
  175. /**
  176. * Determines if current source data is data-url
  177. *
  178. * @return boolean
  179. */
  180. public function isDataUrl()
  181. {
  182. $data = $this->decodeDataUrl($this->data);
  183. return is_null($data) ? false : true;
  184. }
  185. /**
  186. * Determines if current source data is base64 encoded
  187. *
  188. * @return boolean
  189. */
  190. public function isBase64()
  191. {
  192. if (!is_string($this->data)) {
  193. return false;
  194. }
  195. return base64_encode(base64_decode($this->data)) === $this->data;
  196. }
  197. /**
  198. * Initiates new Image from Intervention\Image\Image
  199. *
  200. * @param Image $object
  201. * @return \Intervention\Image\Image
  202. */
  203. public function initFromInterventionImage($object)
  204. {
  205. return $object;
  206. }
  207. /**
  208. * Parses and decodes binary image data from data-url
  209. *
  210. * @param string $data_url
  211. * @return string
  212. */
  213. private function decodeDataUrl($data_url)
  214. {
  215. if (!is_string($data_url)) {
  216. return null;
  217. }
  218. $pattern = "/^data:(?:image\/[a-zA-Z\-\.]+)(?:charset=\".+\")?;base64,(?P<data>.+)$/";
  219. preg_match($pattern, $data_url, $matches);
  220. if (is_array($matches) && array_key_exists('data', $matches)) {
  221. return base64_decode($matches['data']);
  222. }
  223. return null;
  224. }
  225. /**
  226. * Initiates new image from mixed data
  227. *
  228. * @param mixed $data
  229. * @return \Intervention\Image\Image
  230. */
  231. public function init($data)
  232. {
  233. $this->data = $data;
  234. switch (true) {
  235. case $this->isGdResource():
  236. return $this->initFromGdResource($this->data);
  237. case $this->isImagick():
  238. return $this->initFromImagick($this->data);
  239. case $this->isInterventionImage():
  240. return $this->initFromInterventionImage($this->data);
  241. case $this->isSplFileInfo():
  242. return $this->initFromPath($this->data->getRealPath());
  243. case $this->isBinary():
  244. return $this->initFromBinary($this->data);
  245. case $this->isUrl():
  246. return $this->initFromUrl($this->data);
  247. case $this->isStream():
  248. return $this->initFromStream($this->data);
  249. case $this->isFilePath():
  250. return $this->initFromPath($this->data);
  251. case $this->isDataUrl():
  252. return $this->initFromBinary($this->decodeDataUrl($this->data));
  253. case $this->isBase64():
  254. return $this->initFromBinary(base64_decode($this->data));
  255. default:
  256. throw new Exception\NotReadableException("Image source not readable");
  257. }
  258. }
  259. /**
  260. * Decoder object transforms to string source data
  261. *
  262. * @return string
  263. */
  264. public function __toString()
  265. {
  266. return (string) $this->data;
  267. }
  268. }