AbstractDecoder.php 8.3 KB

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