3 namespace Drupal\image_module_test\Plugin\ImageEffect;
5 use Drupal\Core\Image\ImageInterface;
6 use Drupal\image\ImageEffectBase;
9 * Performs an image operation that depends on the URI of the original image.
12 * id = "image_module_test_uri_dependent",
13 * label = @Translation("URI dependent test image effect")
16 class UriDependentTestImageEffect extends ImageEffectBase {
21 public function transformDimensions(array &$dimensions, $uri) {
22 $dimensions = $this->getUriDependentDimensions($uri);
28 public function applyEffect(ImageInterface $image) {
29 $dimensions = $this->getUriDependentDimensions($image->getSource());
30 return $image->resize($dimensions['width'], $dimensions['height']);
34 * Make the image dimensions dependent on the image file extension.
37 * Original image file URI.
41 * - width: Integer with the derivative image width.
42 * - height: Integer with the derivative image height.
44 protected function getUriDependentDimensions($uri) {
46 $extension = pathinfo($uri, PATHINFO_EXTENSION);
47 switch (strtolower($extension)) {
49 $dimensions['width'] = $dimensions['height'] = 100;
53 $dimensions['width'] = $dimensions['height'] = 50;
57 $dimensions['width'] = $dimensions['height'] = 20;