public function GD::applyToImage in ImageAPI Optimize GD 8
Same name and namespace in other branches
- 2.x src/Plugin/ImageAPIOptimizeProcessor/GD.php \Drupal\imageapi_optimize_gd\Plugin\ImageAPIOptimizeProcessor\GD::applyToImage()
File
- src/
Plugin/ ImageAPIOptimizeProcessor/ GD.php, line 22
Class
- GD
- Provides a ImageAPI Optimize processor for GD.
Namespace
Drupal\imageapi_optimize_gd\Plugin\ImageAPIOptimizeProcessorCode
public function applyToImage($image_uri) {
$success = FALSE;
// Confirm GD library exists.
if (function_exists('imagegd2')) {
if (in_array($this
->getMimeType($image_uri), $this->configuration['file_types'])) {
$image = $this
->getImageFactory()
->get($image_uri, 'gd');
if (!$image
->isValid()) {
return FALSE;
}
// Get the correct function based on file type.
$function = 'image' . image_type_to_extension($image
->getToolkit()
->getType(), FALSE);
if (function_exists($function)) {
// Convert stream wrapper URI to normal path.
$destination = \Drupal::service('file_system')
->realpath($image_uri);
$success = $function($image
->getToolkit()
->getResource(), $destination, $this->configuration['quality']);
}
}
}
else {
$this->logger
->notice('The PHP GD library must be installed for the ImageAPI Optimize GD module to process images.');
}
return $success;
}