3 namespace Drupal\Core\Image;
5 use Drupal\Core\ImageToolkit\ImageToolkitManager;
8 * Provides a factory for image objects.
13 * The image toolkit plugin manager.
15 * @var \Drupal\Core\ImageToolkit\ImageToolkitManager
17 protected $toolkitManager;
20 * The image toolkit ID to use for this factory.
27 * Constructs a new ImageFactory object.
29 * @param \Drupal\Core\ImageToolkit\ImageToolkitManager $toolkit_manager
30 * The image toolkit plugin manager.
32 public function __construct(ImageToolkitManager $toolkit_manager) {
33 $this->toolkitManager = $toolkit_manager;
34 $this->toolkitId = $this->toolkitManager->getDefaultToolkitId();
38 * Sets the ID of the image toolkit.
40 * @param string $toolkit_id
41 * The ID of the image toolkit to use for this image factory.
45 public function setToolkitId($toolkit_id) {
46 $this->toolkitId = $toolkit_id;
51 * Gets the ID of the image toolkit currently in use.
54 * The ID of the image toolkit in use by the image factory.
56 public function getToolkitId() {
57 return $this->toolkitId;
61 * Constructs a new Image object.
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
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.
78 * @return \Drupal\Core\Image\ImageInterface
81 * @see ImageFactory::setToolkitId()
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);
89 * Returns the image file extensions supported by the toolkit.
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.
96 * An array of supported image file extensions (e.g. png/jpeg/gif).
98 * @see \Drupal\Core\ImageToolkit\ImageToolkitInterface::getSupportedExtensions()
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');