class Resize in IMCE 8
Same name and namespace in other branches
- 8.2 src/Plugin/ImcePlugin/Resize.php \Drupal\imce\Plugin\ImcePlugin\Resize
Defines Imce Resize plugin.
Plugin annotation
@ImcePlugin(
id = "resize",
label = "Resize",
operations = {
"resize" = "opResize"
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\imce\ImcePluginBase implements ImcePluginInterface
- class \Drupal\imce\Plugin\ImcePlugin\Resize
- class \Drupal\imce\ImcePluginBase implements ImcePluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of Resize
1 file declares its use of Resize
- ResizeTest.php in tests/
src/ Kernel/ Plugin/ ImcePlugin/ ResizeTest.php
1 string reference to 'Resize'
- ResizeTest::getPluginDefinations in tests/
src/ Kernel/ Plugin/ ImcePlugin/ ResizeTest.php - Get plugins definations to new folder.
File
- src/
Plugin/ ImcePlugin/ Resize.php, line 20
Namespace
Drupal\imce\Plugin\ImcePluginView source
class Resize extends ImcePluginBase {
/**
* {@inheritdoc}
*/
public function permissionInfo() {
return [
'resize_images' => $this
->t('Resize images'),
];
}
/**
* {@inheritdoc}
*/
public function buildPage(array &$page, ImceFM $fm) {
// Check if resize permission exists.
if ($fm
->hasPermission('resize_images')) {
$page['#attached']['library'][] = 'imce/drupal.imce.resize';
}
}
/**
* Operation handler: resize.
*/
public function opResize(ImceFM $fm) {
$width = min(10000, (int) $fm
->getPost('width'));
$height = min(10000, (int) $fm
->getPost('height'));
$copy = (bool) $fm
->getPost('copy');
$items = $fm
->getSelection();
if ($this
->validateResize($fm, $items, $width, $height, $copy)) {
$this
->resizeItems($fm, $items, $width, $height, $copy);
}
}
/**
* Validates item resizing.
*/
public function validateResize(ImceFM $fm, array $items, $width, $height, $copy) {
return $items && $fm
->validateDimensions($items, $width, $height) && $fm
->validateImageTypes($items) && $fm
->validatePermissions($items, 'resize_images');
}
/**
* Resizes a list of imce items and returns succeeded ones.
*/
public function resizeItems(ImceFM $fm, array $items, $width, $height, $copy = FALSE) {
$factory = \Drupal::service('image.factory');
$fs = \Drupal::service('file_system');
$success = [];
foreach ($items as $item) {
$uri = $item
->getUri();
$image = $factory
->get($uri);
// Check vallidity.
if (!$image
->isValid()) {
continue;
}
// Check if resizing is needed.
$resize = $image
->getWidth() != $width || $image
->getHeight() != $height;
if (!$resize && !$copy) {
continue;
}
if ($resize && !$image
->resize($width, $height)) {
continue;
}
// Save.
$destination = $copy ? $fs
->createFilename($fs
->basename($uri), $fs
->dirname($uri)) : $uri;
if (!$image
->save($destination)) {
continue;
}
// Create a new file record.
if ($copy) {
$filename = $fs
->basename($destination);
$values = [
'uid' => $fm->user
->id(),
'status' => 1,
'filename' => $filename,
'uri' => $destination,
'filesize' => $image
->getFileSize(),
'filemime' => $image
->getMimeType(),
];
$file = \Drupal::entityTypeManager()
->getStorage('file')
->create($values);
// Check quota.
if ($errors = file_validate_size($file, 0, $fm
->getConf('quota'))) {
$fs
->delete($destination);
$fm
->setMessage($errors[0]);
}
else {
$file
->save();
// Add imce item.
$item->parent
->addFile($filename)
->addToJs();
}
}
else {
if ($file = Imce::getFileEntity($uri)) {
$file
->setSize($image
->getFileSize());
$file
->save();
}
// Add to js.
$item
->addToJs();
}
$success[] = $item;
}
return $success;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
ImcePluginBase:: |
public | function |
Alters entity form of an Imce Profile. Overrides ImcePluginInterface:: |
|
ImcePluginBase:: |
public | function |
Processes profile configuration for a user. Overrides ImcePluginInterface:: |
|
ImcePluginBase:: |
public | function |
Validates entity form of an Imce Profile. Overrides ImcePluginInterface:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
Resize:: |
public | function |
Builds imce page by adding required libraries and elements. Overrides ImcePluginBase:: |
|
Resize:: |
public | function | Operation handler: resize. | |
Resize:: |
public | function |
Returns folder permission definitions. Overrides ImcePluginBase:: |
|
Resize:: |
public | function | Resizes a list of imce items and returns succeeded ones. | |
Resize:: |
public | function | Validates item resizing. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |