You are here

public function GDToolkitWebP::save in Picture 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 GDToolkit::save

File

src/Plugin/ImageToolkit/GDToolkitWebP.php, line 66
Contains \Drupal\system\Plugin\ImageToolkit\GDToolkit.

Class

GDToolkitWebP
Defines the GD2 toolkit for image manipulation within Drupal.

Namespace

Drupal\picture\Plugin\ImageToolkit

Code

public function save($destination) {
  $scheme = file_uri_scheme($destination);

  // Work around lack of stream wrapper support in imagejpeg() and imagepng().
  if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {

    // If destination is not local, save image to temporary local file.
    $local_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL);
    if (!isset($local_wrappers[$scheme])) {
      $permanent_destination = $destination;
      $destination = drupal_tempnam('temporary://', 'gd_');
    }

    // Convert stream wrapper URI to normal path.
    $destination = drupal_realpath($destination);
  }
  switch ($this
    ->getType()) {
    case GDToolkitWebP::IMAGETYPE_WEBP:
      $function = 'imagewebp';
      break;
    default:
      $function = 'image' . image_type_to_extension($this
        ->getType(), FALSE);
      break;
  }
  if (!function_exists($function)) {
    return FALSE;
  }
  if ($this
    ->getType() == IMAGETYPE_JPEG) {
    $success = $function($this
      ->getResource(), $destination, $this->configFactory
      ->get('system.image.gd')
      ->get('jpeg_quality'));
  }
  else {

    // Always save PNG images with full transparency.
    if ($this
      ->getType() == IMAGETYPE_PNG) {
      imagealphablending($this
        ->getResource(), FALSE);
      imagesavealpha($this
        ->getResource(), TRUE);
    }
    $success = $function($this
      ->getResource(), $destination);
  }

  // Move temporary local file to remote destination.
  if (isset($permanent_destination) && $success) {
    return (bool) file_unmanaged_move($destination, $permanent_destination, FILE_EXISTS_REPLACE);
  }
  return $success;
}