View source
<?php
namespace Drupal\image_resize_filter\Plugin\Filter;
use Drupal\Core\Config\Config;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\Core\Form\FormStateInterface;
class FilterImageResize extends FilterBase implements ContainerFactoryPluginInterface {
protected $imageFactory;
protected $systemFileConfig;
protected $streamWrapperManager;
protected $fileSystem;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ImageFactory $image_factory, Config $system_file_config, StreamWrapperManagerInterface $stream_wrapper_manager, FileSystemInterface $file_system) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->imageFactory = $image_factory;
$this->systemFileConfig = $system_file_config;
$this->streamWrapperManager = $stream_wrapper_manager;
$this->fileSystem = $file_system;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('image.factory'), $container
->get('config.factory')
->get('system.file'), $container
->get('stream_wrapper_manager'), $container
->get('file_system'));
}
public function process($text, $langcode) {
return new FilterProcessResult($this
->getImages($text));
}
private function getImages($text) {
$images = image_resize_filter_get_images($this->settings, $text);
$search = [];
$replace = [];
foreach ($images as $image) {
if ($image['location'] == 'remote') {
$local_file_path = 'resize/remote/' . md5(file_get_contents($image['local_path'])) . '-' . $image['expected_size']['width'] . 'x' . $image['expected_size']['height'] . '.' . $image['extension'];
if (floatval(\Drupal::VERSION) >= 8.800000000000001) {
$image['destination'] = $this->systemFileConfig
->get('default_scheme') . '://' . $local_file_path;
}
else {
$image['destination'] = file_default_scheme() . '://' . $local_file_path;
}
}
elseif (!$image['resize']) {
$image['destination'] = $image['local_path'];
}
else {
$path_info = image_resize_filter_pathinfo($image['local_path']);
if (floatval(\Drupal::VERSION) >= 8.800000000000001) {
$local_file_dir = $this->streamWrapperManager
->getTarget($path_info['dirname']);
}
else {
$local_file_dir = file_uri_target($path_info['dirname']);
}
$local_file_path = 'resize/' . ($local_file_dir ? $local_file_dir . '/' : '') . $path_info['filename'] . '-' . $image['expected_size']['width'] . 'x' . $image['expected_size']['height'] . '.' . $path_info['extension'];
$image['destination'] = $path_info['scheme'] . '://' . $local_file_path;
}
if (!file_exists($image['destination'])) {
$resize_threshold = 10;
$flood = \Drupal::flood();
if (!$flood
->isAllowed('image_resize_filter_resize', $resize_threshold, 120)) {
$this->messenger
->addMessage(t('Image resize threshold of @count per minute reached. Some images have not been resized. Resave the content to resize remaining images.', [
'@count' => floor($resize_threshold / 2),
]), 'error', FALSE);
continue;
}
$flood
->register('image_resize_filter_resize', 120);
$directory = dirname($image['destination']);
$this->fileSystem
->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
if ($image['location'] == 'remote' && !$image['resize']) {
$handle = fopen($image['destination'], 'w');
fwrite($handle, file_get_contents($image['local_path']));
fclose($handle);
}
elseif ($image['resize']) {
$copy = $this->fileSystem
->copy($image['local_path'], $image['destination']);
$res = $this->imageFactory
->get($copy);
if ($res) {
$res
->resize($image['expected_size']['width'], $image['expected_size']['height']);
$res
->save();
}
else {
$handle = fopen($image['destination'], 'w');
fwrite($handle, file_get_contents($image['local_path']));
fclose($handle);
}
}
@chmod($image['destination'], 0664);
}
image_resize_filter_delete_temp_file($image['location'], $image['local_path']);
$search[] = $image['img_tag'];
$replace[] = image_resize_filter_image_tag($image, $this->settings);
}
return str_replace($search, $replace, $text);
}
function settingsForm(array $form, FormStateInterface $form_state) {
$settings['image_locations'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Resize images stored'),
'#options' => [
'local' => $this
->t('Locally'),
'remote' => $this
->t('On remote servers (note: this copies <em>all</em> remote images locally)'),
],
'#default_value' => array_keys(array_filter($this->settings['image_locations'])),
'#description' => $this
->t('This option will determine which images will be analyzed for <img> tag differences. Enabling resizing of remote images can have performance impacts, as all images in the filtered text needs to be transferred via HTTP each time the filter cache is cleared.'),
];
return $settings;
}
}