Unverified Commit 71c58d67 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3202016 by mondrake, andypost, acbramley, mstrelan, hestenet, benmorss,...

Issue #3202016 by mondrake, andypost, acbramley, mstrelan, hestenet, benmorss, smustgrave, catch, alexpott, xjm, Mixologic: Let GDToolkit support AVIF image format
parent 616d1046
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ core/.phpstan-baseline.php text eol=lf whitespace=blank-at-eol,-blank-at-eof,-sp
# Define binary file attributes.
# - Do not treat them as text.
# - Include binary diff in patches instead of "binary files differ."
*.avif    -text diff
*.eot     -text diff
*.exe     -text diff
*.gif     -text diff
+2 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ autoplace
autoplay
autoreply
autosubmit
avif
backlink
bakeware
barchart
@@ -234,6 +235,7 @@ icann
iconwrap
idekey
iframeupload
imageavif
imagecache
indexname
inglés
+46 −2
Original line number Diff line number Diff line
@@ -270,7 +270,11 @@ public function save($destination) {
    }
    else {
      // Image types that support alpha need to be saved accordingly.
      if (in_array($this->getType(), [IMAGETYPE_PNG, IMAGETYPE_WEBP], TRUE)) {
      if (in_array($this->getType(), [
        IMAGETYPE_PNG,
        IMAGETYPE_WEBP,
        IMAGETYPE_AVIF,
      ], TRUE)) {
        imagealphablending($this->getImage(), FALSE);
        imagesavealpha($this->getImage(), TRUE);
      }
@@ -428,8 +432,12 @@ public function getRequirements() {
      IMG_JPG => 'JPEG',
      IMG_PNG => 'PNG',
      IMG_WEBP => 'WEBP',
      IMG_AVIF => 'AVIF',
    ];
    $supported_formats = array_filter($check_formats, fn($type) => imagetypes() & $type, ARRAY_FILTER_USE_KEY);
    if (isset($supported_formats[IMG_AVIF]) && !$this->checkAvifSupport()) {
      unset($supported_formats[IMG_AVIF]);
    }
    $unsupported_formats = array_diff_key($check_formats, $supported_formats);

    $descriptions = [];
@@ -454,6 +462,11 @@ public function getRequirements() {
        '@unsupported' => $unsupported,
        '@ref' => $fix_info,
      ]);
      if (isset($unsupported_formats[IMG_AVIF])) {
        $descriptions[] = $this->t('AVIF is not supported, likely because of PHP missing a codec for encoding images. See <a href=":cr_url">the change record</a> for more information.', [
          ':cr_url' => 'https://www.drupal.org/node/3348348',
        ]);
      }
    }

    // Check for filter and rotate support.
@@ -528,6 +541,31 @@ public function extensionToImageType($extension) {
    return IMAGETYPE_UNKNOWN;
  }

  /**
   * Checks if AVIF can encode image.
   *
   * This method tries to create an AVIF image and save it to disk via
   * imageavif(). If that fails, it's likely a codec missing, or the function
   * was disabled. This is an expensive operation to run, so we cache its
   * result.
   *
   * @return bool
   *   TRUE if AVIF is fully supported, FALSE otherwise.
   */
  protected function checkAvifSupport(): bool {
    static $supported = NULL;

    if ($supported !== NULL) {
      return $supported;
    }

    $tempFile = fopen('php://memory', 'r+');
    $supported = imageavif(imagecreatetruecolor(1, 1), $tempFile, 0, 10) && fstat($tempFile)['size'] > 0;
    fclose($tempFile);

    return $supported;
  }

  /**
   * Returns a list of image types supported by the toolkit.
   *
@@ -536,7 +574,13 @@ public function extensionToImageType($extension) {
   *   IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG, IMAGETYPE_PNG, etc.).
   */
  protected static function supportedTypes() {
    return [IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_WEBP];
    return [
      IMAGETYPE_PNG,
      IMAGETYPE_JPEG,
      IMAGETYPE_GIF,
      IMAGETYPE_WEBP,
      IMAGETYPE_AVIF,
    ];
  }

}
+1 −0
Original line number Diff line number Diff line
@@ -92,6 +92,7 @@ protected function execute(array $arguments) {
    switch ($type) {
      case IMAGETYPE_PNG:
      case IMAGETYPE_WEBP:
      case IMAGETYPE_AVIF:
        imagealphablending($image, FALSE);
        $transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
        imagefill($image, 0, 0, $transparency);
+1 −1
Original line number Diff line number Diff line
@@ -83,7 +83,7 @@ public function testGdToolkitRequirements(): void {
    // Get Status Report.
    $this->drupalGet('admin/reports/status');
    $this->assertSession()->pageTextContains('GD2 image manipulation toolkit');
    $this->assertSession()->pageTextContains('Supported image file formats: GIF, JPEG, PNG, WEBP.');
    $this->assertSession()->pageTextContains('Supported image file formats: GIF, JPEG, PNG, WEBP, AVIF.');
  }

}
Loading