You are here

private function Webp::createImageMagickImage in WebP 8

Creates a WebP copy of a source image URI using imagemagick toolkit.

Parameters

string $uri: Image URI.

int $quality: Image quality factor (optional).

Return value

bool|string The location of the WebP image if successful, FALSE if not successful.

1 call to Webp::createImageMagickImage()
Webp::createWebpCopy in src/Webp.php
Creates a WebP copy of a source image URI.

File

src/Webp.php, line 194

Class

Webp
Class Webp.

Namespace

Drupal\webp

Code

private function createImageMagickImage($uri, $quality = NULL) {
  $webp = FALSE;
  $ImageMagickImg = $this->imageFactory
    ->get($uri, 'imagemagick');

  // We'll convert the image into webp.
  $ImageMagickImg
    ->apply('convert', [
    'extension' => 'webp',
    'quality' => $quality,
  ]);
  $pathInfo = pathinfo($uri);
  $destination = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.webp';
  if ($ImageMagickImg
    ->save($pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.webp')) {
    $webp = $destination;
    $msg = $this
      ->t('Generated WebP image with Image Magick. Quality: ' . $quality . ' Destination:' . $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.webp');
    $this->logger
      ->info($msg);
  }
  else {
    $error = $this
      ->t('Imagemagick issue: Could not generate WebP image.');
    $this->logger
      ->error($error);
  }
  return $webp;
}