public function GD::applyToImage in ImageAPI Optimize GD 2.x
Same name and namespace in other branches
- 8 src/Plugin/ImageAPIOptimizeProcessor/GD.php \Drupal\imageapi_optimize_gd\Plugin\ImageAPIOptimizeProcessor\GD::applyToImage()
Apply this image optimize processor to the given image.
Image processors should modify the file in-place or overwrite the file on disk with an optimized version.
Parameters
string $image_uri: Original image file URI.
Return value
bool TRUE if an optimized image was generated, or FALSE if the image could not be optimized.
Overrides ImageAPIOptimizeProcessorInterface::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;
}