You are here

public function GDToolkitWebP::parseFile in Picture 8

Determines if a file contains a valid image.

Drupal supports GIF, JPG and PNG file formats when used with the GD toolkit, and may support others, depending on which toolkits are installed.

Return value

bool TRUE if the file could be found and is an image, FALSE otherwise.

Overrides GDToolkit::parseFile

File

src/Plugin/ImageToolkit/GDToolkitWebP.php, line 112
Contains \Drupal\system\Plugin\ImageToolkit\GDToolkit.

Class

GDToolkitWebP
Defines the GD2 toolkit for image manipulation within Drupal.

Namespace

Drupal\picture\Plugin\ImageToolkit

Code

public function parseFile() {
  $data = @getimagesize($this
    ->getImage()
    ->getSource());
  if ($data && in_array($data[2], static::supportedTypes())) {
    $this
      ->setType($data[2]);
    $this->preLoadInfo = $data;
    return TRUE;
  }
  else {

    // Determine if this is a WebP image.
    $handle = fopen($this
      ->getImage()
      ->getSource(), 'rb');
    $header = fread($handle, 12);
    if (Unicode::substr($header, 0, 4) === 'RIFF' && Unicode::substr($header, -4) === 'WEBP') {
      switch (fread($handle, 4)) {
        case 'VP8 ':
          fseek($handle, 26);
          $dimensions = fread($handle, 4);
          $width = unpack('V', $dimensions);
          $this->preLoadInfo[0] = $width[1] & 0x3fff;
          $this->preLoadInfo[1] = $width[1] >> 16 & 0x3fff;
          break;
        case 'VP8L':
          fseek($handle, 21);
          $dimensions = fread($handle, 4);
          $width = unpack('V', $dimensions);
          $this->preLoadInfo[0] = ($width[1] & 0x3fff) + 1;
          $this->preLoadInfo[1] = ($width[1] >> 14 & 0x3fff) + 1;
          break;
      }
      $this
        ->setType(GDToolkitWebP::IMAGETYPE_WEBP);
      $this->preLoadInfo[2] = GDToolkitWebP::IMAGETYPE_WEBP;
      $this->preLoadInfo[3] = 'height="' . $this->preLoadInfo[1] . '" width="' . $this->preLoadInfo[1] . '"';
      $this->preLoadInfo['mime'] = 'image/webp';

      // The 'channels' and 'bits' elements are not required, so omit them.
      fclose($handle);
      return TRUE;
    }
    fclose($handle);
  }
  return FALSE;
}