AbstractDecoder.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. namespace Intervention\Image;
  3. use GuzzleHttp\Psr7\Stream;
  4. use Intervention\Image\Exception\NotReadableException;
  5. use Psr\Http\Message\StreamInterface;
  6. abstract class AbstractDecoder
  7. {
  8. /**
  9. * Initiates new image from path in filesystem
  10. *
  11. * @param string $path
  12. * @return \Intervention\Image\Image
  13. */
  14. abstract public function initFromPath($path);
  15. /**
  16. * Initiates new image from binary data
  17. *
  18. * @param string $data
  19. * @return \Intervention\Image\Image
  20. */
  21. abstract public function initFromBinary($data);
  22. /**
  23. * Initiates new image from GD resource
  24. *
  25. * @param Resource $resource
  26. * @return \Intervention\Image\Image
  27. */
  28. abstract public function initFromGdResource($resource);
  29. /**
  30. * Initiates new image from Imagick object
  31. *
  32. * @param \Imagick $object
  33. * @return \Intervention\Image\Image
  34. */
  35. abstract public function initFromImagick(\Imagick $object);
  36. /**
  37. * Buffer of input data
  38. *
  39. * @var mixed
  40. */
  41. private $data;
  42. /**
  43. * Creates new Decoder with data
  44. *
  45. * @param mixed $data
  46. */
  47. public function __construct($data = null)
  48. {
  49. $this->data = $data;
  50. }
  51. /**
  52. * Init from given URL
  53. *
  54. * @param string $url
  55. * @return \Intervention\Image\Image
  56. */
  57. public function initFromUrl($url)
  58. {
  59. $options = [
  60. 'http' => [
  61. 'method'=>"GET",
  62. 'header'=>"Accept-language: en\r\n".
  63. "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2\r\n"
  64. ]
  65. ];
  66. $context = stream_context_create($options);
  67. if ($data = @file_get_contents($url, false, $context)) {
  68. return $this->initFromBinary($data);
  69. }
  70. throw new NotReadableException(
  71. "Unable to init from given url (".$url.")."
  72. );
  73. }
  74. /**
  75. * Init from given stream
  76. *
  77. * @param StreamInterface|resource $stream
  78. * @return \Intervention\Image\Image
  79. */
  80. public function initFromStream($stream)
  81. {
  82. if (!$stream instanceof StreamInterface) {
  83. $stream = new Stream($stream);
  84. }
  85. try {
  86. $offset = $stream->tell();
  87. } catch (\RuntimeException $e) {
  88. $offset = 0;
  89. }
  90. $shouldAndCanSeek = $offset !== 0 && $stream->isSeekable();
  91. if ($shouldAndCanSeek) {
  92. $stream->rewind();
  93. }
  94. try {
  95. $data = $stream->getContents();
  96. } catch (\RuntimeException $e) {
  97. $data = null;
  98. }
  99. if ($shouldAndCanSeek) {
  100. $stream->seek($offset);
  101. }
  102. if ($data) {
  103. return $this->initFromBinary($data);
  104. }
  105. throw new NotReadableException(
  106. "Unable to init from given stream"
  107. );
  108. }
  109. /**
  110. * Determines if current source data is GD resource
  111. *
  112. * @return boolean
  113. */
  114. public function isGdResource()
  115. {
  116. if (is_resource($this->data)) {
  117. return (get_resource_type($this->data) == 'gd');
  118. }
  119. return false;
  120. }
  121. /**
  122. * Determines if current source data is Imagick object
  123. *
  124. * @return boolean
  125. */
  126. public function isImagick()
  127. {
  128. return is_a($this->data, 'Imagick');
  129. }
  130. /**
  131. * Determines if current source data is Intervention\Image\Image object
  132. *
  133. * @return boolean
  134. */
  135. public function isInterventionImage()
  136. {
  137. return is_a($this->data, '\Intervention\Image\Image');
  138. }
  139. /**
  140. * Determines if current data is SplFileInfo object
  141. *
  142. * @return boolean
  143. */
  144. public function isSplFileInfo()
  145. {
  146. return is_a($this->data, 'SplFileInfo');
  147. }
  148. /**
  149. * Determines if current data is Symfony UploadedFile component
  150. *
  151. * @return boolean
  152. */
  153. public function isSymfonyUpload()
  154. {
  155. return is_a($this->data, 'Symfony\Component\HttpFoundation\File\UploadedFile');
  156. }
  157. /**
  158. * Determines if current source data is file path
  159. *
  160. * @return boolean
  161. */
  162. public function isFilePath()
  163. {
  164. if (is_string($this->data)) {
  165. try {
  166. return is_file($this->data);
  167. } catch (\Exception $e) {
  168. return false;
  169. }
  170. }
  171. return false;
  172. }
  173. /**
  174. * Determines if current source data is url
  175. *
  176. * @return boolean
  177. */
  178. public function isUrl()
  179. {
  180. return (bool) filter_var($this->data, FILTER_VALIDATE_URL);
  181. }
  182. /**
  183. * Determines if current source data is a stream resource
  184. *
  185. * @return boolean
  186. */
  187. public function isStream()
  188. {
  189. if ($this->data instanceof StreamInterface) return true;
  190. if (!is_resource($this->data)) return false;
  191. if (get_resource_type($this->data) !== 'stream') return false;
  192. return true;
  193. }
  194. /**
  195. * Determines if current source data is binary data
  196. *
  197. * @return boolean
  198. */
  199. public function isBinary()
  200. {
  201. if (is_string($this->data)) {
  202. $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $this->data);
  203. return (substr($mime, 0, 4) != 'text' && $mime != 'application/x-empty');
  204. }
  205. return false;
  206. }
  207. /**
  208. * Determines if current source data is data-url
  209. *
  210. * @return boolean
  211. */
  212. public function isDataUrl()
  213. {
  214. $data = $this->decodeDataUrl($this->data);
  215. return is_null($data) ? false : true;
  216. }
  217. /**
  218. * Determines if current source data is base64 encoded
  219. *
  220. * @return boolean
  221. */
  222. public function isBase64()
  223. {
  224. if (!is_string($this->data)) {
  225. return false;
  226. }
  227. return base64_encode(base64_decode($this->data)) === str_replace(["\n", "\r"], '', $this->data);
  228. }
  229. /**
  230. * Initiates new Image from Intervention\Image\Image
  231. *
  232. * @param Image $object
  233. * @return \Intervention\Image\Image
  234. */
  235. public function initFromInterventionImage($object)
  236. {
  237. return $object;
  238. }
  239. /**
  240. * Parses and decodes binary image data from data-url
  241. *
  242. * @param string $data_url
  243. * @return string
  244. */
  245. private function decodeDataUrl($data_url)
  246. {
  247. if (!is_string($data_url)) {
  248. return null;
  249. }
  250. $pattern = "/^data:(?:image\/[a-zA-Z\-\.]+)(?:charset=\".+\")?;base64,(?P<data>.+)$/";
  251. preg_match($pattern, $data_url, $matches);
  252. if (is_array($matches) && array_key_exists('data', $matches)) {
  253. return base64_decode($matches['data']);
  254. }
  255. return null;
  256. }
  257. /**
  258. * Initiates new image from mixed data
  259. *
  260. * @param mixed $data
  261. * @return \Intervention\Image\Image
  262. */
  263. public function init($data)
  264. {
  265. $this->data = $data;
  266. switch (true) {
  267. case $this->isGdResource():
  268. return $this->initFromGdResource($this->data);
  269. case $this->isImagick():
  270. return $this->initFromImagick($this->data);
  271. case $this->isInterventionImage():
  272. return $this->initFromInterventionImage($this->data);
  273. case $this->isSplFileInfo():
  274. return $this->initFromPath($this->data->getRealPath());
  275. case $this->isBinary():
  276. return $this->initFromBinary($this->data);
  277. case $this->isUrl():
  278. return $this->initFromUrl($this->data);
  279. case $this->isStream():
  280. return $this->initFromStream($this->data);
  281. case $this->isDataUrl():
  282. return $this->initFromBinary($this->decodeDataUrl($this->data));
  283. case $this->isFilePath():
  284. return $this->initFromPath($this->data);
  285. // isBase64 has to be after isFilePath to prevent false positives
  286. case $this->isBase64():
  287. return $this->initFromBinary(base64_decode($this->data));
  288. default:
  289. throw new NotReadableException("Image source not readable");
  290. }
  291. }
  292. /**
  293. * Decoder object transforms to string source data
  294. *
  295. * @return string
  296. */
  297. public function __toString()
  298. {
  299. return (string) $this->data;
  300. }
  301. }