ImageFactory.php 3.0 KB

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