[ Index ]

PHP Cross Reference of phpBB-3.2.11-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 256 kiB bytes */
  18      const TIF_HEADER_SIZE = 262144;
  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 type for short */
  27      const TIF_TAG_TYPE_SHORT = 3;
  28  
  29      /** @var int TIF IFD entry size */
  30      const TIF_IFD_ENTRY_SIZE = 12;
  31  
  32      /** @var string TIF signature of intel type */
  33      const TIF_SIGNATURE_INTEL = 'II';
  34  
  35      /** @var string TIF signature of motorola type */
  36      const TIF_SIGNATURE_MOTOROLA = 'MM';
  37  
  38      /** @var array Size info array */
  39      protected $size;
  40  
  41      /** @var string Bit type of long field */
  42      public $typeLong;
  43  
  44      /** @var string Bit type of short field */
  45      public $typeShort;
  46  
  47      /**
  48       * {@inheritdoc}
  49       */
  50  	public function getSize($filename)
  51      {
  52          // Do not force length of header
  53          $data = $this->fastImageSize->getImage($filename, 0, self::TIF_HEADER_SIZE, false);
  54  
  55          $this->size = array();
  56  
  57          $signature = substr($data, 0, self::SHORT_SIZE);
  58  
  59          if (!in_array($signature, array(self::TIF_SIGNATURE_INTEL, self::TIF_SIGNATURE_MOTOROLA)))
  60          {
  61              return;
  62          }
  63  
  64          // Set byte type
  65          $this->setByteType($signature);
  66  
  67          // Get offset of IFD
  68          list(, $offset) = unpack($this->typeLong, substr($data, self::LONG_SIZE, self::LONG_SIZE));
  69  
  70          // Get size of IFD
  71          $ifdSizeInfo = substr($data, $offset, self::SHORT_SIZE);
  72          if (empty($ifdSizeInfo))
  73          {
  74              return;
  75          }
  76          list(, $sizeIfd) = unpack($this->typeShort, $ifdSizeInfo);
  77  
  78          // Skip 2 bytes that define the IFD size
  79          $offset += self::SHORT_SIZE;
  80  
  81          // Ensure size can't exceed data length
  82          $sizeIfd = min($sizeIfd, floor((strlen($data) - $offset) / self::TIF_IFD_ENTRY_SIZE));
  83  
  84          // Filter through IFD
  85          for ($i = 0; $i < $sizeIfd; $i++)
  86          {
  87              // Get IFD tag
  88              $type = unpack($this->typeShort, substr($data, $offset, self::SHORT_SIZE));
  89  
  90              // Get field type of tag
  91              $fieldType = unpack($this->typeShort . 'type', substr($data, $offset + self::SHORT_SIZE, self::SHORT_SIZE));
  92  
  93              // Get IFD entry
  94              $ifdValue = substr($data, $offset + 2 * self::LONG_SIZE, self::LONG_SIZE);
  95  
  96              // Set size of field
  97              $this->setSizeInfo($type[1], $fieldType['type'], $ifdValue);
  98  
  99              $offset += self::TIF_IFD_ENTRY_SIZE;
 100          }
 101  
 102          $this->fastImageSize->setSize($this->size);
 103      }
 104  
 105      /**
 106       * Set byte type based on signature in header
 107       *
 108       * @param string $signature Header signature
 109       */
 110  	public function setByteType($signature)
 111      {
 112          if ($signature === self::TIF_SIGNATURE_INTEL)
 113          {
 114              $this->typeLong = 'V';
 115              $this->typeShort = 'v';
 116              $this->size['type'] = IMAGETYPE_TIFF_II;
 117          }
 118          else
 119          {
 120              $this->typeLong = 'N';
 121              $this->typeShort = 'n';
 122              $this->size['type'] = IMAGETYPE_TIFF_MM;
 123          }
 124      }
 125  
 126      /**
 127       * Set size info
 128       *
 129       * @param int $dimensionType Type of dimension. Either width or height
 130       * @param int $fieldLength Length of field. Either short or long
 131       * @param string $ifdValue String value of IFD field
 132       */
 133  	protected function setSizeInfo($dimensionType, $fieldLength, $ifdValue)
 134      {
 135          // Set size of field
 136          $fieldSize = $fieldLength === self::TIF_TAG_TYPE_SHORT ? $this->typeShort : $this->typeLong;
 137  
 138          // Get actual dimensions from IFD
 139          if ($dimensionType === self::TIF_TAG_IMAGE_HEIGHT)
 140          {
 141              $this->size = array_merge($this->size, unpack($fieldSize . 'height', $ifdValue));
 142          }
 143          else if ($dimensionType === self::TIF_TAG_IMAGE_WIDTH)
 144          {
 145              $this->size = array_merge($this->size, unpack($fieldSize . 'width', $ifdValue));
 146          }
 147      }
 148  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1