You are here

public function ImagickToolkit::save in Imagick 8

Writes an image resource to a destination file.

Parameters

string $destination: A string file URI or path where the image should be saved.

Return value

bool TRUE on success, FALSE on failure.

Overrides ImageToolkitInterface::save

File

src/Plugin/ImageToolkit/ImagickToolkit.php, line 133

Class

ImagickToolkit
Defines the Imagick toolkit for image manipulation within Drupal.

Namespace

Drupal\imagick\Plugin\ImageToolkit

Code

public function save($destination) {
  $resource = $this
    ->getResource();
  if (is_null($resource)) {
    return FALSE;
  }
  if ($this
    ->isValidUri($destination)) {

    // If destination is not local, save image to temporary local file.
    if ($this
      ->isRemoteUri($destination)) {
      $permanent_destination = $destination;
      $destination = $this->fileSystem
        ->tempnam(self::TEMP_DIR, self::TEMP_PREFIX);
    }

    // Convert stream wrapper URI to normal path.
    $destination = $this->fileSystem
      ->realpath($destination);
  }

  // If preferred format is set, use it as prefix for writeImage
  // If not this will throw a ImagickException exception
  try {
    $image_format = $resource
      ->getImageFormat();
    $destination = implode(':', [
      $image_format,
      $destination,
    ]);
  } catch (ImagickException $e) {
  }

  // Only compress JPEG files because other filetypes will increase in filesize
  if (isset($image_format) && in_array($image_format, [
    'JPEG',
    'JPG',
    'JPE',
  ])) {

    // Get image quality from effect or global setting
    $quality = $resource
      ->getImageProperty('quality') ?: $this->configFactory
      ->get(self::CONFIG)
      ->get(self::CONFIG_JPEG_QUALITY);

    // Set image compression quality
    $resource
      ->setImageCompressionQuality($quality);

    // Optimize images
    if ($this->configFactory
      ->get(self::CONFIG)
      ->get(self::CONFIG_OPTIMIZE)) {

      // Using recommendations from Google's Page Speed docs: https://developers.google.com/speed/docs/insights/OptimizeImages
      $resource
        ->setSamplingFactors([
        '2x2',
        '1x1',
        '1x1',
      ]);
      $resource
        ->setColorspace(Imagick::COLORSPACE_RGB);
      $resource
        ->setInterlaceScheme(Imagick::INTERLACE_JPEG);
    }
  }

  // Strip metadata
  if ($this->configFactory
    ->get(self::CONFIG)
    ->get(self::CONFIG_STRIP_METADATA)) {
    $resource
      ->stripImage();
  }

  // Write image to destination
  if (isset($image_format) && in_array($image_format, [
    'GIF',
  ])) {
    if (!$resource
      ->writeImages($destination, TRUE)) {
      return FALSE;
    }
  }
  else {
    if (!$resource
      ->writeImage($destination)) {
      return FALSE;
    }
  }

  // Move temporary local file to remote destination.
  if (isset($permanent_destination)) {
    return (bool) $this->fileSystem
      ->move($destination, $permanent_destination, FileSystemInterface::EXISTS_REPLACE);
  }
  return TRUE;
}