5e5ddc72bcdeb9f1f91494709fbe52bb5730f7ba
[yaffs-website] / web / core / modules / image / tests / modules / image_module_test / src / Plugin / ImageEffect / UriDependentTestImageEffect.php
1 <?php
2
3 namespace Drupal\image_module_test\Plugin\ImageEffect;
4
5 use Drupal\Core\Image\ImageInterface;
6 use Drupal\image\ImageEffectBase;
7
8 /**
9  * Performs an image operation that depends on the URI of the original image.
10  *
11  * @ImageEffect(
12  *   id = "image_module_test_uri_dependent",
13  *   label = @Translation("URI dependent test image effect")
14  * )
15  */
16 class UriDependentTestImageEffect extends ImageEffectBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public function transformDimensions(array &$dimensions, $uri) {
22     $dimensions = $this->getUriDependentDimensions($uri);
23   }
24
25   /**
26    * {@inheritdoc}
27    */
28   public function applyEffect(ImageInterface $image) {
29     $dimensions = $this->getUriDependentDimensions($image->getSource());
30     return $image->resize($dimensions['width'], $dimensions['height']);
31   }
32
33   /**
34    * Make the image dimensions dependent on the image file extension.
35    *
36    * @param string $uri
37    *   Original image file URI.
38    *
39    * @return array
40    *   Associative array.
41    *   - width: Integer with the derivative image width.
42    *   - height: Integer with the derivative image height.
43    */
44   protected function getUriDependentDimensions($uri) {
45     $dimensions = [];
46     $extension = pathinfo($uri, PATHINFO_EXTENSION);
47     switch (strtolower($extension)) {
48       case 'png':
49         $dimensions['width'] = $dimensions['height'] = 100;
50         break;
51
52       case 'gif':
53         $dimensions['width'] = $dimensions['height'] = 50;
54         break;
55
56       default:
57         $dimensions['width'] = $dimensions['height'] = 20;
58         break;
59
60     }
61     return $dimensions;
62   }
63
64 }