[ Index ]

PHP Cross Reference of phpBB-3.3.11-deutsch

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * fast-image-size image type webp
   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  use \FastImageSize\FastImageSize;
  15  
  16  class TypeWebp extends TypeBase
  17  {
  18      /** @var string RIFF header */
  19      const WEBP_RIFF_HEADER = "RIFF";
  20  
  21      /** @var string Webp header */
  22      const WEBP_HEADER = "WEBP";
  23  
  24      /** @var string VP8 chunk header */
  25      const VP8_HEADER = "VP8";
  26  
  27      /** @var string Simple(lossy) webp format */
  28      const WEBP_FORMAT_SIMPLE = ' ';
  29  
  30      /** @var string Lossless webp format */
  31      const WEBP_FORMAT_LOSSLESS = 'L';
  32  
  33      /** @var string Extended webp format */
  34      const WEBP_FORMAT_EXTENDED = 'X';
  35  
  36      /** @var int WEBP header size needed for retrieving image size */
  37      const WEBP_HEADER_SIZE = 30;
  38  
  39      /** @var array Size info array */
  40      protected $size;
  41  
  42      /**
  43       * Constructor for webp image type. Adds missing constant if necessary.
  44       *
  45       * @param FastImageSize $fastImageSize
  46       */
  47  	public function __construct(FastImageSize $fastImageSize)
  48      {
  49          parent::__construct($fastImageSize);
  50  
  51          if (!defined('IMAGETYPE_WEBP'))
  52          {
  53              define('IMAGETYPE_WEBP', 18);
  54          }
  55      }
  56  
  57      /**
  58       * {@inheritdoc}
  59       */
  60  	public function getSize($filename)
  61      {
  62          // Do not force length of header
  63          $data = $this->fastImageSize->getImage($filename, 0, self::WEBP_HEADER_SIZE);
  64  
  65          $this->size = array();
  66  
  67          $webpFormat = substr($data, 15, 1);
  68  
  69          if (!$this->hasWebpHeader($data) || !$this->isValidFormat($webpFormat))
  70          {
  71              return;
  72          }
  73  
  74          $data = substr($data, 16, 14);
  75  
  76          $this->getWebpSize($data, $webpFormat);
  77  
  78          $this->fastImageSize->setSize($this->size);
  79          $this->fastImageSize->setImageType(IMAGETYPE_WEBP);
  80      }
  81  
  82      /**
  83       * Check if $data has valid WebP header
  84       *
  85       * @param string $data Image data
  86       *
  87       * @return bool True if $data has valid WebP header, false if not
  88       */
  89  	protected function hasWebpHeader($data)
  90      {
  91          $riffSignature = substr($data, 0, self::LONG_SIZE);
  92          $webpSignature = substr($data, 8, self::LONG_SIZE);
  93          $vp8Signature = substr($data, 12, self::SHORT_SIZE + 1);
  94  
  95          return !empty($data) && $riffSignature === self::WEBP_RIFF_HEADER &&
  96              $webpSignature === self::WEBP_HEADER && $vp8Signature === self::VP8_HEADER;
  97      }
  98  
  99      /**
 100       * Check if $format is a valid WebP format
 101       *
 102       * @param string $format Format string
 103       * @return bool True if format is valid WebP format, false if not
 104       */
 105  	protected function isValidFormat($format)
 106      {
 107          return in_array($format, array(self::WEBP_FORMAT_SIMPLE, self::WEBP_FORMAT_LOSSLESS, self::WEBP_FORMAT_EXTENDED));
 108      }
 109  
 110      /**
 111       * Get webp size info depending on format type and set size array values
 112       *
 113       * @param string $data Data string
 114       * @param string $format Format string
 115       */
 116  	protected function getWebpSize($data, $format)
 117      {
 118          switch ($format)
 119          {
 120              case self::WEBP_FORMAT_SIMPLE:
 121                  $this->size = unpack('vwidth/vheight', substr($data, 10, 4));
 122              break;
 123  
 124              case self::WEBP_FORMAT_LOSSLESS:
 125                  // Lossless uses 14-bit values so we'll have to use bitwise shifting
 126                  $this->size = array(
 127                      'width'        => ord($data[5]) + ((ord($data[6]) & 0x3F) << 8) + 1,
 128                      'height'    => (ord($data[6]) >> 6) + (ord($data[7]) << 2) + ((ord($data[8]) & 0xF) << 10) + 1,
 129                  );
 130              break;
 131  
 132              case self::WEBP_FORMAT_EXTENDED:
 133                  // Extended uses 24-bit values cause 14-bit for lossless wasn't weird enough
 134                  $this->size = array(
 135                      'width'        => ord($data[8]) + (ord($data[9]) << 8) + (ord($data[10]) << 16) + 1,
 136                      'height'    => ord($data[11]) + (ord($data[12]) << 8) + (ord($data[13]) << 16) + 1,
 137                  );
 138              break;
 139          }
 140      }
 141  }


Generated: Sat Nov 4 14:26:03 2023 Cross-referenced by PHPXref 0.7.1