ImageReplaceEffect.php in Image Replace 8
File
src/Plugin/ImageEffect/ImageReplaceEffect.php
View source
<?php
namespace Drupal\image_replace\Plugin\ImageEffect;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Image\ImageInterface;
use Drupal\image\ImageEffectBase;
use Drupal\image_replace\ImageReplaceStorageInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ImageReplaceEffect extends ImageEffectBase {
protected $imageFactory;
protected $imageReplaceStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, ImageFactory $image_factory, ImageReplaceStorageInterface $image_replace_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $logger);
$this
->setConfiguration($configuration);
$this->imageFactory = $image_factory;
$this->imageReplaceStorage = $image_replace_storage;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('logger.factory')
->get('image'), $container
->get('image.factory'), $container
->get('image_replace.storage'));
}
public function applyEffect(ImageInterface $image) {
$configuration = $this
->getConfiguration();
$replacement_file = $this->imageReplaceStorage
->get($configuration['data']['image_style'], $image
->getSource());
if ($replacement_file) {
$toolkit_id = $image
->getToolkitId();
$replacement_image = $this->imageFactory
->get($replacement_file, $toolkit_id);
if ($replacement_image) {
$image
->apply('image_replace', [
'replacement_image' => $replacement_image,
]);
}
}
}
}