[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/vendor/marc1706/fast-image-size/lib/Type/ -> TypeTif.php (source)

   1  <?php
   2  
   3  /**
   4   * fast-image-size image type tif
   5   * @package fast-image-size
   6   * @copyright (c) Marc Alexander <admin@m-a-styles.de>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace FastImageSize\Type;
  13  
  14  class TypeTif extends TypeBase
  15  {
  16      /** @var int TIF header size. The header might be larger but the dimensions
  17       *            should be in the first 51200 bytes */
  18      const TIF_HEADER_SIZE = 51200;
  19  
  20      /** @var int TIF tag for image height */
  21      const TIF_TAG_IMAGE_HEIGHT = 257;
  22  
  23      /** @var int TIF tag for image width */
  24      const TIF_TAG_IMAGE_WIDTH = 256;
  25  
  26      /** @var int TIF tag for exif IFD offset */
  27      const TIF_TAG_EXIF_OFFSET = 34665;
  28  
  29      /** @var int TIF tag for Image X resolution in pixels */
  30      const TIF_TAG_EXIF_IMAGE_WIDTH = 0xA002;
  31  
  32      /** @var int TIF tag for Image Y resolution in pixels */
  33      const TIF_TAG_EXIF_IMAGE_HEIGHT = 0xA003;
  34  
  35      /** @var int TIF tag type for short */
  36      const TIF_TAG_TYPE_SHORT = 3;
  37  
  38      /** @var int TIF IFD entry size */
  39      const TIF_IFD_ENTRY_SIZE = 12;
  40  
  41      /** @var string TIF signature of intel type */
  42      const TIF_SIGNATURE_INTEL = 'II';
  43  
  44      /** @var string TIF signature of motorola type */
  45      const TIF_SIGNATURE_MOTOROLA = 'MM';
  46  
  47      /** @var array Size info array */
  48      protected $size;
  49  
  50      /** @var string Bit type of long field */
  51      public $typeLong;
  52  
  53      /** @var string Bit type of short field */
  54      public $typeShort;
  55  
  56      /**
  57       * {@inheritdoc}
  58       */
  59  	public function getSize($filename)
  60      {
  61          // Do not force length of header
  62          $data = $this->fastImageSize->getImage($filename, 0, self::TIF_HEADER_SIZE, false);
  63  
  64          $this->size = array();
  65  
  66          $signature = substr($data, 0, self::SHORT_SIZE);
  67  
  68          if (!in_array($signature, array(self::TIF_SIGNATURE_INTEL, self::TIF_SIGNATURE_MOTOROLA)))
  69          {
  70              return;
  71          }
  72  
  73          // Set byte type
  74          $this->setByteType($signature);
  75  
  76          // Get offset of IFD
  77          list(, $offset) = unpack($this->typeLong, substr($data, self::LONG_SIZE, self::LONG_SIZE));
  78  
  79          // Get size of IFD
  80          list(, $sizeIfd) = unpack($this->typeShort, substr($data, $offset, self::SHORT_SIZE));
  81  
  82          // Skip 2 bytes that define the IFD size
  83          $offset += self::SHORT_SIZE;
  84  
  85          // Ensure size can't exceed data length
  86          $sizeIfd = min($sizeIfd, floor((strlen($data) - $offset) / self::TIF_IFD_ENTRY_SIZE));
  87  
  88          // Filter through IFD
  89          for ($i = 0; $i < $sizeIfd; $i++)
  90          {
  91              // Get IFD tag
  92              $type = unpack($this->typeShort, substr($data, $offset, self::SHORT_SIZE));
  93  
  94              // Get field type of tag
  95              $fieldType = unpack($this->typeShort . 'type', substr($data, $offset + self::SHORT_SIZE, self::SHORT_SIZE));
  96  
  97              // Get IFD entry
  98              $ifdValue = substr($data, $offset + 2 * self::LONG_SIZE, self::LONG_SIZE);
  99  
 100              // Set size of field
 101              $this->setSizeInfo($type[1], $fieldType['type'], $ifdValue);
 102  
 103              $offset += self::TIF_IFD_ENTRY_SIZE;
 104          }
 105  
 106          $this->fastImageSize->setSize($this->size);
 107      }
 108  
 109      /**
 110       * Set byte type based on signature in header
 111       *
 112       * @param string $signature Header signature
 113       */
 114  	public function setByteType($signature)
 115      {
 116          if ($signature === self::TIF_SIGNATURE_INTEL)
 117          {
 118              $this->typeLong = 'V';
 119              $this->typeShort = 'v';
 120              $this->size['type'] = IMAGETYPE_TIFF_II;
 121          }
 122          else
 123          {
 124              $this->typeLong = 'N';
 125              $this->typeShort = 'n';
 126              $this->size['type'] = IMAGETYPE_TIFF_MM;
 127          }
 128      }
 129  
 130      /**
 131       * Set size info
 132       *
 133       * @param int $dimensionType Type of dimension. Either width or height
 134       * @param int $fieldLength Length of field. Either short or long
 135       * @param string $ifdValue String value of IFD field
 136       */
 137  	protected function setSizeInfo($dimensionType, $fieldLength, $ifdValue)
 138      {
 139          // Set size of field
 140          $fieldSize = $fieldLength === self::TIF_TAG_TYPE_SHORT ? $this->typeShort : $this->typeLong;
 141  
 142          // Get actual dimensions from IFD
 143          if ($dimensionType === self::TIF_TAG_IMAGE_HEIGHT)
 144          {
 145              $this->size = array_merge($this->size, unpack($fieldSize . 'height', $ifdValue));
 146          }
 147          else if ($dimensionType === self::TIF_TAG_IMAGE_WIDTH)
 148          {
 149              $this->size = array_merge($this->size, unpack($fieldSize . 'width', $ifdValue));
 150          }
 151      }
 152  }


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1