Advertisement
Guest User

GetImageSize

a guest
Jun 4th, 2015
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Core\Image;
  4.  
  5. class Getimagesize {
  6. private $strpos = 0;
  7. private $str;
  8. private $uri;
  9. private $type;
  10. private $handle;
  11. public function __construct($uri = null) {
  12. if ($uri)
  13. $this->load ( $uri );
  14. }
  15. public function load($uri) {
  16. if ($this->handle)
  17. $this->close ();
  18.  
  19. $this->uri = $uri;
  20. $this->handle = @fopen ( $uri, 'r' );
  21. }
  22. public function close() {
  23. if ($this->handle)
  24. fclose ( $this->handle );
  25. }
  26. public function getSize() {
  27. $this->strpos = 0;
  28. if ($this->getType ()) {
  29. return $this->parseSize ();
  30. }
  31.  
  32. return false;
  33. }
  34. public function getType() {
  35. $this->strpos = 0;
  36.  
  37. if (! $this->type) {
  38. switch ($this->getChars ( 2 )) {
  39. case "BM" :
  40. return $this->type = 'bmp';
  41. case "GI" :
  42. return $this->type = 'gif';
  43. case chr ( 0xFF ) . chr ( 0xd8 ) :
  44. return $this->type = 'jpeg';
  45. case chr ( 0x89 ) . 'P' :
  46. return $this->type = 'png';
  47. default :
  48. return false;
  49. }
  50. }
  51.  
  52. return $this->type;
  53. }
  54. private function parseSize() {
  55. $this->strpos = 0;
  56. switch ($this->type) {
  57. case 'png' :
  58. $info = $this->parseSizeForPNG ();
  59. if ($info) {
  60. $info = array (
  61. $info [1],
  62. $info [2],
  63. 3,
  64. 'width="' . $info [1] . '" height="' . $info [2] . '"',
  65. 'bits' => 8,
  66. 'mime' => 'image/png'
  67. );
  68. }
  69. return $info;
  70. case 'gif' :
  71. $info = $this->parseSizeForGIF ();
  72. if ($info) {
  73. $info = array (
  74. $info [1],
  75. $info [2],
  76. 1,
  77. 'width="' . $info [1] . '" height="' . $info [2] . '"',
  78. 'bits' => 8,
  79. 'mime' => 'image/gif'
  80. );
  81. }
  82. return $info;
  83. case 'bmp' :
  84. $info = $this->parseSizeForBMP ();
  85. if ($info) {
  86. $info = array (
  87. $info [1],
  88. $info [2],
  89. 6,
  90. 'width="' . $info [1] . '" height="' . $info [2] . '"',
  91. 'bits' => 8,
  92. 'mime' => 'image/x-ms-bmp'
  93. );
  94. }
  95. return $info;
  96. case 'jpeg' :
  97. $info = $this->parseSizeForJPEG ();
  98. if ($info) {
  99. $info = array (
  100. $info [0],
  101. $info [1],
  102. 2,
  103. 'width="' . $info [0] . '" height="' . $info [1] . '"',
  104. 'bits' => 8,
  105. 'mime' => 'image/jpeg'
  106. );
  107. }
  108. return $info;
  109. }
  110.  
  111. return null;
  112. }
  113. private function parseSizeForPNG() {
  114. $chars = $this->getChars ( 25 );
  115.  
  116. return unpack ( "N*", substr ( $chars, 16, 8 ) );
  117. }
  118. private function parseSizeForGIF() {
  119. $chars = $this->getChars ( 11 );
  120.  
  121. return unpack ( "S*", substr ( $chars, 6, 4 ) );
  122. }
  123. private function parseSizeForBMP() {
  124. $chars = $this->getChars ( 29 );
  125. $chars = substr ( $chars, 14, 14 );
  126. $type = unpack ( 'C', $chars );
  127.  
  128. return (reset ( $type ) == 40) ? unpack ( 'L*', substr ( $chars, 4 ) ) : unpack ( 'L*', substr ( $chars, 4, 8 ) );
  129. }
  130. private function parseSizeForJPEG() {
  131. $state = null;
  132. $i = 0;
  133.  
  134. while ( true ) {
  135. switch ($state) {
  136. default :
  137. $this->getChars ( 2 );
  138. $state = 'started';
  139. break;
  140.  
  141. case 'started' :
  142. $b = $this->getByte ();
  143. if ($b === false)
  144. return false;
  145.  
  146. $state = $b == 0xFF ? 'sof' : 'started';
  147. break;
  148.  
  149. case 'sof' :
  150. $b = $this->getByte ();
  151. if (in_array ( $b, range ( 0xe0, 0xef ) )) {
  152. $state = 'skipframe';
  153. } elseif (in_array ( $b, array_merge ( range ( 0xC0, 0xC3 ), range ( 0xC5, 0xC7 ), range ( 0xC9, 0xCB ), range ( 0xCD, 0xCF ) ) )) {
  154. $state = 'readsize';
  155. } elseif ($b == 0xFF) {
  156. $state = 'sof';
  157. } else {
  158. $state = 'skipframe';
  159. }
  160. break;
  161.  
  162. case 'skipframe' :
  163. $skip = $this->readInt ( $this->getChars ( 2 ) ) - 2;
  164. $state = 'doskip';
  165. break;
  166.  
  167. case 'doskip' :
  168. $this->getChars ( $skip );
  169. $state = 'started';
  170. break;
  171.  
  172. case 'readsize' :
  173. $c = $this->getChars ( 7 );
  174.  
  175. return array (
  176. $this->readInt ( substr ( $c, 5, 2 ) ),
  177. $this->readInt ( substr ( $c, 3, 2 ) )
  178. );
  179. }
  180. }
  181. }
  182. private function getChars($n) {
  183. if (! $this->handle)
  184. return false;
  185. $response = null;
  186.  
  187. // do we need more data?
  188. if ($this->strpos + $n - 1 >= strlen ( $this->str )) {
  189. $end = ($this->strpos + $n);
  190.  
  191. while ( strlen ( $this->str ) < $end && $response !== false ) {
  192. // read more from the file handle
  193. $need = $end - ftell ( $this->handle );
  194.  
  195. if ($response = fread ( $this->handle, $need )) {
  196. $this->str .= $response;
  197. } else {
  198. return false;
  199. }
  200. }
  201. }
  202.  
  203. $result = substr ( $this->str, $this->strpos, $n );
  204. $this->strpos += $n;
  205.  
  206. // we are dealing with bytes here, so force the encoding
  207. return mb_convert_encoding ( $result, "8BIT" );
  208. }
  209. private function getByte() {
  210. $c = $this->getChars ( 1 );
  211. $b = unpack ( "C", $c );
  212.  
  213. return reset ( $b );
  214. }
  215. private function readInt($str) {
  216. $size = unpack ( "C*", $str );
  217.  
  218. return ($size [1] << 8) + $size [2];
  219. }
  220. public function __destruct() {
  221. $this->close ();
  222. }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement