private function FilterImageResize::getImages in Image Resize Filter 8
Locate all images in a piece of text that need replacing.
An array of settings that will be used to identify which images need updating. Includes the following:
- image_locations: An array of acceptable image locations. of the following values: "remote". Remote image will be downloaded and saved locally. This procedure is intensive as the images need to be retrieved to have their dimensions checked.
Parameters
string $text: The text to be updated with the new img src tags.
Return value
string $images An list of images.
1 call to FilterImageResize::getImages()
- FilterImageResize::process in src/
Plugin/ Filter/ FilterImageResize.php - Performs the filter processing.
File
- src/
Plugin/ Filter/ FilterImageResize.php, line 129
Class
- FilterImageResize
- Provides a filter to resize images.
Namespace
Drupal\image_resize_filter\Plugin\FilterCode
private function getImages($text) {
$images = image_resize_filter_get_images($this->settings, $text);
$search = [];
$replace = [];
foreach ($images as $image) {
// Copy remote images locally.
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'];
// Once Drupal 8.7.x is unsupported remove this IF statement.
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']);
// Once Drupal 8.7.x is unsupported remove this IF statement.
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'])) {
// Basic flood prevention of resizing.
$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);
// Create the resize directory.
$directory = dirname($image['destination']);
$this->fileSystem
->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
// Move remote images into place if they are already the right size.
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) {
// Image loaded successfully; resize.
$res
->resize($image['expected_size']['width'], $image['expected_size']['height']);
$res
->save();
}
else {
// Image failed to load - type doesn't match extension or invalid; keep original file
$handle = fopen($image['destination'], 'w');
fwrite($handle, file_get_contents($image['local_path']));
fclose($handle);
}
}
@chmod($image['destination'], 0664);
}
// Delete our temporary file if this is a remote image.
image_resize_filter_delete_temp_file($image['location'], $image['local_path']);
// Replace the existing image source with the resized image.
// Set the image we're currently updating in the callback function.
$search[] = $image['img_tag'];
$replace[] = image_resize_filter_image_tag($image, $this->settings);
}
return str_replace($search, $replace, $text);
}