Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Image / ImageFactory.php
1 <?php
2
3 namespace Drupal\Core\Image;
4
5 use Drupal\Core\ImageToolkit\ImageToolkitManager;
6
7 /**
8  * Provides a factory for image objects.
9  */
10 class ImageFactory {
11
12   /**
13    * The image toolkit plugin manager.
14    *
15    * @var \Drupal\Core\ImageToolkit\ImageToolkitManager
16    */
17   protected $toolkitManager;
18
19   /**
20    * The image toolkit ID to use for this factory.
21    *
22    * @var string
23    */
24   protected $toolkitId;
25
26   /**
27    * Constructs a new ImageFactory object.
28    *
29    * @param \Drupal\Core\ImageToolkit\ImageToolkitManager $toolkit_manager
30    *   The image toolkit plugin manager.
31    */
32   public function __construct(ImageToolkitManager $toolkit_manager) {
33     $this->toolkitManager = $toolkit_manager;
34     $this->toolkitId = $this->toolkitManager->getDefaultToolkitId();
35   }
36
37   /**
38    * Sets the ID of the image toolkit.
39    *
40    * @param string $toolkit_id
41    *   The ID of the image toolkit to use for this image factory.
42    *
43    * @return $this
44    */
45   public function setToolkitId($toolkit_id) {
46     $this->toolkitId = $toolkit_id;
47     return $this;
48   }
49
50   /**
51    * Gets the ID of the image toolkit currently in use.
52    *
53    * @return string
54    *   The ID of the image toolkit in use by the image factory.
55    */
56   public function getToolkitId() {
57     return $this->toolkitId;
58   }
59
60   /**
61    * Constructs a new Image object.
62    *
63    * Normally, the toolkit set as default in the admin UI is used by the
64    * factory to create new Image objects. This can be overridden through
65    * \Drupal\Core\Image\ImageInterface::setToolkitId() so that any new Image
66    * object created will use the new toolkit specified. Finally, a single
67    * Image object can be created using a specific toolkit, regardless of the
68    * current factory settings, by passing its plugin ID in the $toolkit_id
69    * argument.
70    *
71    * @param string|null $source
72    *   (optional) The path to an image file, or NULL to construct the object
73    *   with no image source.
74    * @param string|null $toolkit_id
75    *   (optional) The ID of the image toolkit to use for this image, or NULL
76    *   to use the current toolkit.
77    *
78    * @return \Drupal\Core\Image\ImageInterface
79    *   An Image object.
80    *
81    * @see ImageFactory::setToolkitId()
82    */
83   public function get($source = NULL, $toolkit_id = NULL) {
84     $toolkit_id = $toolkit_id ?: $this->toolkitId;
85     return new Image($this->toolkitManager->createInstance($toolkit_id), $source);
86   }
87
88   /**
89    * Returns the image file extensions supported by the toolkit.
90    *
91    * @param string|null $toolkit_id
92    *   (optional) The ID of the image toolkit to use for checking, or NULL
93    *   to use the current toolkit.
94    *
95    * @return array
96    *   An array of supported image file extensions (e.g. png/jpeg/gif).
97    *
98    * @see \Drupal\Core\ImageToolkit\ImageToolkitInterface::getSupportedExtensions()
99    */
100   public function getSupportedExtensions($toolkit_id = NULL) {
101     $toolkit_id = $toolkit_id ?: $this->toolkitId;
102     $definition = $this->toolkitManager->getDefinition($toolkit_id);
103     return call_user_func($definition['class'] . '::getSupportedExtensions');
104   }
105
106 }