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

version 1.1.4

parent 9ad7296b
No related branches found
No related tags found
No related merge requests found
Pipeline #200791 failed
......@@ -16,8 +16,8 @@ class ImageapiOptimizeAvifWebpHelper {
* @return string
* URI of the image without extension part.
*/
public function extensionlessUri(string $original_uri): string {
return substr($original_uri, 0, strrpos($original_uri, '.'));
public function extensionlessUri(string $original_uri): string {
return $original_uri;
}
}
......@@ -28,16 +28,16 @@ class AvifWebp extends ConfigurableImageAPIOptimizeProcessorBase {
if ($path_info['extension'] == 'avif' || $path_info['extension'] == 'webp') {
return FALSE;
}
$toolkit_id = $this->imageFactory->getToolkitId();
$toolkit_id = $this->imageFactory->getToolkitId();
$source_image = $this->imageFactory->get($image_uri, $toolkit_id);
$avif_quality = $this->configuration['avif_quality'];
$webp_quality = $this->configuration['webp_quality'];
$extensionless_image_uri = \Drupal::service('imageapi_optimize_avif_webp.helper')->extensionlessUri($image_uri);
/*********** Webp image ****************/
$source_image = $this->imageFactory->get($image_uri, $toolkit_id);
if ($source_image->isValid()) {
$destination = $extensionless_image_uri . '.webp';
if ($source_image) {
/*********** Webp image ****************/
$destination = $extensionless_image_uri . '.webp';
// Check if ImageMagick supports webp.
if ($toolkit_id == 'imagemagick' && in_array('webp', $this->imageFactory->getSupportedExtensions())) {
try {
......@@ -45,10 +45,11 @@ class AvifWebp extends ConfigurableImageAPIOptimizeProcessorBase {
$source_image->save($destination);
// Fix issue where sometimes image fails to generate.
if (file_exists($destination) && filesize($destination) % 2 == 1) {
if (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.
......@@ -59,35 +60,26 @@ class AvifWebp extends ConfigurableImageAPIOptimizeProcessorBase {
else {
imagewebp($source_image->getToolkit()->getResource(), $destination, $webp_quality);
// Fix issue where sometimes image fails to generate.
if (file_exists($destination) && filesize($destination) % 2 == 1) {
if (filesize($destination) % 2 == 1) {
file_put_contents($destination, "\0", FILE_APPEND);
}
}
}
/*********** 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);
$source_avif = $this->imageFactory->get($temp_image_uri, $toolkit_id);
if ($source_avif->isValid()) {
/*********** Avif image ****************/
$destination = $extensionless_image_uri . '.avif';
// Check if ImageMagick supports avif.
if ($toolkit_id == 'imagemagick' && in_array('avif', $this->imageFactory->getSupportedExtensions())) {
try {
$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);
$source_image->apply('convert', ['extension' => 'avif', 'quality' => $avif_quality]);
$source_image->save($destination);
// Fix issue where sometimes image fails to generate.
if (file_exists($abs_destination) && filesize($abs_destination) % 2 == 1) {
file_put_contents($abs_destination, "\0", FILE_APPEND);
if (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()]);
}
......@@ -95,23 +87,21 @@ class AvifWebp extends ConfigurableImageAPIOptimizeProcessorBase {
// Go with GD.
else {
try {
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 {
$this->logger->error('AVIF: function imageavif does not exist. https://www.php.net/manual/en/function.imageavif.php');
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);
}
//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()]);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment