function GDToolkit::save
Same name in other branches
- 9 core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php \Drupal\system\Plugin\ImageToolkit\GDToolkit::save()
- 8.9.x core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php \Drupal\system\Plugin\ImageToolkit\GDToolkit::save()
- 11.x core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php \Drupal\system\Plugin\ImageToolkit\GDToolkit::save()
Overrides ImageToolkitInterface::save
File
-
core/
modules/ system/ src/ Plugin/ ImageToolkit/ GDToolkit.php, line 318
Class
- GDToolkit
- Defines the GD2 toolkit for image manipulation within Drupal.
Namespace
Drupal\system\Plugin\ImageToolkitCode
public function save($destination) {
$scheme = StreamWrapperManager::getScheme($destination);
// Work around lack of stream wrapper support in imagejpeg() and imagepng().
if ($scheme && $this->streamWrapperManager
->isValidScheme($scheme)) {
// If destination is not local, save image to temporary local file.
$local_wrappers = $this->streamWrapperManager
->getWrappers(StreamWrapperInterface::LOCAL);
if (!isset($local_wrappers[$scheme])) {
$permanent_destination = $destination;
$destination = $this->fileSystem
->tempnam('temporary://', 'gd_');
}
// Convert stream wrapper URI to normal path.
$destination = $this->fileSystem
->realpath($destination);
}
$function = 'image' . image_type_to_extension($this->getType(), FALSE);
if (!function_exists($function)) {
return FALSE;
}
if ($this->getType() == IMAGETYPE_JPEG) {
try {
$success = $function($this->getImage(), $destination, $this->configFactory
->get('system.image.gd')
->get('jpeg_quality'));
} catch (\Throwable $t) {
$this->logger
->error("The image toolkit '@toolkit' failed saving image '@image'. Reported error: @class - @message", [
'@toolkit' => $this->getPluginId(),
'@image' => $destination,
'@class' => get_class($t),
'@message' => $t->getMessage(),
]);
$success = FALSE;
}
}
else {
// Image types that support alpha need to be saved accordingly.
if (in_array($this->getType(), [
IMAGETYPE_PNG,
IMAGETYPE_WEBP,
], TRUE)) {
imagealphablending($this->getImage(), FALSE);
imagesavealpha($this->getImage(), TRUE);
}
try {
$success = $function($this->getImage(), $destination);
} catch (\Throwable $t) {
$this->logger
->error("The image toolkit '@toolkit' failed saving image '@image'. Reported error: @class - @message", [
'@toolkit' => $this->getPluginId(),
'@image' => $destination,
'@class' => get_class($t),
'@message' => $t->getMessage(),
]);
$success = FALSE;
}
}
// Move temporary local file to remote destination.
if (isset($permanent_destination) && $success) {
try {
$this->fileSystem
->move($destination, $permanent_destination, FileExists::Replace);
return TRUE;
} catch (FileException $e) {
return FALSE;
}
}
return $success;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.