From 29a7870947b54fb37534a279369b6584f99ac75c Mon Sep 17 00:00:00 2001 From: Will Washburn Date: Thu, 7 May 2015 14:31:29 -0400 Subject: [PATCH] Add support for the .tiff file format --- Fastimage.php | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/Fastimage.php b/Fastimage.php index 1db470e..cf8f6ea 100644 --- a/Fastimage.php +++ b/Fastimage.php @@ -68,6 +68,9 @@ public function getType() return $this->type = 'jpeg'; case chr(0x89).'P': return $this->type = 'png'; + case "II": + case "MM": + return $this->type = 'tiff'; default: return false; } @@ -90,7 +93,9 @@ private function parseSize() case 'bmp': return $this->parseSizeForBMP(); case 'jpeg': - return $this->parseSizeForJPEG(); + return $this->parseSizeForJPEG(); + case 'tiff': + return $this->parseSizeForTiff(); } return null; @@ -182,6 +187,63 @@ private function parseSizeForJPEG() } } + private function parseSizeForTiff() + { + $byte_order = $this->getChars(2); + + switch ( $byte_order ) { + case 'II': + $short = 'v'; + $long = 'V'; + break; + case 'MM': + $short = 'n'; + $long = 'N'; + break; + default: + return false; + break; + } + + $this->getChars(2); + + $offset = current(unpack($long,$this->getChars(4))); + + $this->getChars($offset-8); + + $tag_count = current(unpack($short,$this->getChars(2))); + + for($i = $tag_count; $i > 0; $i--) { + + $type = current(unpack($short,$this->getChars(2))); + $this->getChars(6); + $data = current(unpack($short,$this->getChars(2))); + + switch ($type) { + case 0x0100: + $width = $data; + break; + case 0x0101: + $height = $data; + break; + case 0x0112: + $orientation = $data; + break; + } + + if ( isset($width) && isset($height) && isset($orientation) ) { + + if($orientation >= 5) { + return [$height,$width]; + } + + return [$width,$height]; + } + + $this->getChars(2); + } + } + private function getChars($n) {