fast_image.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/9/15
  6. * Time: 下午4:37
  7. */
  8. namespace tools;
  9. class fast_image
  10. {
  11. private $strpos = 0;
  12. private $str;
  13. private $type;
  14. private $handle;
  15. public function __construct($uri = null)
  16. {
  17. if ($uri) $this->load($uri);
  18. }
  19. public function load($uri)
  20. {
  21. if ($this->handle) $this->close();
  22. $this->handle = fopen($uri, 'r');
  23. }
  24. public function close()
  25. {
  26. if ($this->handle)
  27. {
  28. fclose($this->handle);
  29. $this->handle = null;
  30. $this->type = null;
  31. $this->str = null;
  32. }
  33. }
  34. public function getSize()
  35. {
  36. $this->strpos = 0;
  37. if ($this->getType()) {
  38. return array_values($this->parseSize());
  39. }
  40. return false;
  41. }
  42. public function getType()
  43. {
  44. $this->strpos = 0;
  45. if (!$this->type)
  46. {
  47. switch ($this->getChars(2))
  48. {
  49. case "BM":
  50. return $this->type = 'bmp';
  51. case "GI":
  52. return $this->type = 'gif';
  53. case chr(0xFF).chr(0xd8):
  54. return $this->type = 'jpeg';
  55. case chr(0x89).'P':
  56. return $this->type = 'png';
  57. default:
  58. return false;
  59. }
  60. }
  61. return $this->type;
  62. }
  63. private function parseSize()
  64. {
  65. $this->strpos = 0;
  66. switch ($this->type)
  67. {
  68. case 'png':
  69. return $this->parseSizeForPNG();
  70. case 'gif':
  71. return $this->parseSizeForGIF();
  72. case 'bmp':
  73. return $this->parseSizeForBMP();
  74. case 'jpeg':
  75. return $this->parseSizeForJPEG();
  76. }
  77. return null;
  78. }
  79. private function parseSizeForPNG()
  80. {
  81. $chars = $this->getChars(25);
  82. return unpack("N*", substr($chars, 16, 8));
  83. }
  84. private function parseSizeForGIF()
  85. {
  86. $chars = $this->getChars(11);
  87. return unpack("S*", substr($chars, 6, 4));
  88. }
  89. private function parseSizeForBMP()
  90. {
  91. $chars = $this->getChars(29);
  92. $chars = substr($chars, 14, 14);
  93. $type = unpack('C', $chars);
  94. return (reset($type) == 40) ? unpack('L*', substr($chars, 4)) : unpack('L*', substr($chars, 4, 8));
  95. }
  96. private function parseSizeForJPEG()
  97. {
  98. $state = null;
  99. $i = 0;
  100. while (true)
  101. {
  102. switch ($state)
  103. {
  104. default:
  105. $this->getChars(2);
  106. $state = 'started';
  107. break;
  108. case 'started':
  109. $b = $this->getByte();
  110. if ($b === false) return false;
  111. $state = $b == 0xFF ? 'sof' : 'started';
  112. break;
  113. case 'sof':
  114. $b = $this->getByte();
  115. if (in_array($b, range(0xe0, 0xef)))
  116. {
  117. $state = 'skipframe';
  118. }
  119. elseif (in_array($b, array_merge(range(0xC0,0xC3), range(0xC5,0xC7), range(0xC9,0xCB), range(0xCD,0xCF))))
  120. {
  121. $state = 'readsize';
  122. }
  123. elseif ($b == 0xFF)
  124. {
  125. $state = 'sof';
  126. }
  127. else
  128. {
  129. $state = 'skipframe';
  130. }
  131. break;
  132. case 'skipframe':
  133. $skip = $this->readInt($this->getChars(2)) - 2;
  134. $state = 'doskip';
  135. break;
  136. case 'doskip':
  137. $this->getChars($skip);
  138. $state = 'started';
  139. break;
  140. case 'readsize':
  141. $c = $this->getChars(7);
  142. return array($this->readInt(substr($c, 5, 2)), $this->readInt(substr($c, 3, 2)));
  143. }
  144. }
  145. }
  146. private function getChars($n)
  147. {
  148. $response = null;
  149. // do we need more data?
  150. if ($this->strpos + $n -1 >= strlen($this->str))
  151. {
  152. $end = ($this->strpos + $n);
  153. while (strlen($this->str) < $end && $response !== false)
  154. {
  155. // read more from the file handle
  156. $need = $end - ftell($this->handle);
  157. if ($response = fread($this->handle, $need))
  158. {
  159. $this->str .= $response;
  160. }
  161. else
  162. {
  163. return false;
  164. }
  165. }
  166. }
  167. $result = substr($this->str, $this->strpos, $n);
  168. $this->strpos += $n;
  169. return $result;
  170. }
  171. private function getByte()
  172. {
  173. $c = $this->getChars(1);
  174. $b = unpack("C", $c);
  175. return reset($b);
  176. }
  177. private function readInt($str)
  178. {
  179. $size = unpack("C*", $str);
  180. return ($size[1] << 8) + $size[2];
  181. }
  182. public function __destruct()
  183. {
  184. $this->close();
  185. }
  186. }