Skip to content
Snippets Groups Projects
Commit 181501b8 authored by hoangd's avatar hoangd
Browse files

AVIF 'convert' bugs fix & responsive_image module must be installed to use...

AVIF 'convert' bugs fix & responsive_image module must be installed to use imageapi_optimize_avif_webp_responsive module
parent 70c39cf0
No related branches found
No related tags found
1 merge request!45AVIF 'convert' bugs fix & responsive_image module must be installed to use...
Pipeline #171681 failed
......@@ -5,3 +5,4 @@ core_version_requirement: ^8.9 || ^9 || ^10
package: 'Image Optimize'
dependencies:
- imageapi_optimize_avif_webp:imageapi_optimize_avif_webp
- responsive_image:responsive_image
......@@ -42,11 +42,8 @@ class ImageAPIOptimizeAvifWebpPipeline extends ImageAPIOptimizePipeline {
$extensionless_temp_image_uri = $extensionless_image_service->extensionlessUri($temp_image_uri);
$temp_avif_uri = $extensionless_temp_image_uri . '.avif';
if (file_exists($temp_avif_uri)) {
$_temp_avif_uri = \Drupal::service('file_system')->copy($temp_avif_uri, $avif_uri, FileSystemInterface::EXISTS_RENAME);
if ($_temp_avif_uri) {
// $this->temporaryFiles[] = $_temp_avif_uri;
break;
}
\Drupal::service('file_system')->copy($temp_avif_uri, $avif_uri, FileSystemInterface::EXISTS_RENAME);
$this->temporaryFiles[] = $temp_avif_uri;
}
}
......@@ -55,11 +52,9 @@ class ImageAPIOptimizeAvifWebpPipeline extends ImageAPIOptimizePipeline {
$extensionless_temp_image_uri = $extensionless_image_service->extensionlessUri($temp_image_uri);
$temp_webp_uri = $extensionless_temp_image_uri . '.webp';
if (file_exists($temp_webp_uri)) {
$_temp_webp_uri = \Drupal::service('file_system')->copy($temp_webp_uri, $webp_uri, FileSystemInterface::EXISTS_RENAME);
if ($_temp_webp_uri) {
// $this->temporaryFiles[] = $_temp_webp_uri;
break;
}
\Drupal::service('file_system')->copy($temp_webp_uri, $webp_uri, FileSystemInterface::EXISTS_RENAME);
$this->temporaryFiles[] = $temp_webp_uri;
}
}
}
......
<?php
namespace Drupal\imageapi_optimize_avif_webp\Plugin\ImageAPIOptimizeProcessor;
use Drupal\Core\Form\FormStateInterface;
use Drupal\imageapi_optimize\ConfigurableImageAPIOptimizeProcessorBase;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\File\FileSystemInterface;
/**
* AVIF & WebP Deriver.
*
......@@ -29,7 +33,7 @@ class AvifWebp extends ConfigurableImageAPIOptimizeProcessorBase {
$webp_quality = $this->configuration['webp_quality'];
$extensionless_image_uri = \Drupal::service('imageapi_optimize_avif_webp.helper')->extensionlessUri($image_uri);
if ($source_image) {
if ($source_image->isValid()) {
/*********** Webp image ****************/
$destination = $extensionless_image_uri . '.webp';
......@@ -40,39 +44,45 @@ class AvifWebp extends ConfigurableImageAPIOptimizeProcessorBase {
$source_image->save($destination);
// Fix issue where sometimes image fails to generate.
if (filesize($destination) % 2 == 1) {
if (file_exists($destination) && filesize($destination) % 2 == 1) {
file_put_contents($destination, "\0", FILE_APPEND);
}
//return TRUE;
}
catch (\Exception $error) {
// Something else went wrong, unrelated to the Tinify API.
$this->logger->error('AVIF: Failed to optimize image using ImageMagick due to "%error".', ['%error' => $error->getMessage()]);
$this->logger->error('WEBP: Failed to optimize image using ImageMagick due to "%error".', ['%error' => $error->getMessage()]);
}
}
// Go with GD.
else {
imagewebp($source_image->getToolkit()->getResource(), $destination, $webp_quality);
// Fix issue where sometimes image fails to generate.
if (filesize($destination) % 2 == 1) {
if (file_exists($destination) && filesize($destination) % 2 == 1) {
file_put_contents($destination, "\0", FILE_APPEND);
}
}
/*********** Avif image ****************/
}
/*********** Avif image ****************/
$file_extension = strtolower(substr(strrchr($image_uri,'.'),1));
$temp_image_uri = 'temporary://image_api_optimize_' . Crypt::randomBytesBase64(8) . '.' . $file_extension;
$temp_image_uri = \Drupal::service('file_system')->copy($image_uri, $temp_image_uri, FileSystemInterface::EXISTS_RENAME);
$this->temporaryFiles[] = $temp_image_uri;
$source_avif = $this->imageFactory->get($temp_image_uri, $toolkit_id);
if ($source_avif->isValid()) {
$destination = $extensionless_image_uri . '.avif';
// Check if ImageMagick supports avif.
if ($toolkit_id == 'imagemagick' && in_array('avif', $this->imageFactory->getSupportedExtensions())) {
try {
$source_image->apply('convert', ['extension' => 'avif', 'quality' => $avif_quality]);
$source_image->save($destination);
$abs_image_uri = \Drupal::service('file_system')->realpath($image_uri);
$abs_destination = \Drupal::service('file_system')->realpath($destination);
exec('convert '.$abs_image_uri.' -density 72 -units PixelsPerInch -quality '.$avif_quality.' '.$abs_destination);
// Fix issue where sometimes image fails to generate.
if (filesize($destination) % 2 == 1) {
if (file_exists($destination) && filesize($destination) % 2 == 1) {
file_put_contents($destination, "\0", FILE_APPEND);
}
//return TRUE;
}
catch (\Exception $error) {
// Something else went wrong, unrelated to the Tinify API.
......@@ -82,25 +92,28 @@ class AvifWebp extends ConfigurableImageAPIOptimizeProcessorBase {
// Go with GD.
else {
try {
imageavif(
$source_image->getToolkit()->getResource(),
$destination,
$avif_quality
);
// Fix issue where sometimes image fails to generate.
if (filesize($destination) % 2 == 1) {
file_put_contents($destination, "\0", FILE_APPEND);
if (function_exists('imageavif')) {
imageavif(
$source_avif->getToolkit()->getResource(),
$destination,
$avif_quality
);
// Fix issue where sometimes image fails to generate.
if (file_exists($destination) && filesize($destination) % 2 == 1) {
file_put_contents($destination, "\0", FILE_APPEND);
}
}
else {
\Drupal::logger('GD2')->error('Function imageavif does not exist.');
}
//return TRUE;
}
catch (\Exception $error) {
// Something else went wrong, unrelated to the Tinify API.
$this->logger->error('AVIF: Failed to optimize image using GD due to "%error".', ['%error' => $error->getMessage()]);
}
}
return TRUE;
return TRUE;
}
return FALSE;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment