AbstractDecoder.php 8.0 KB

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