You are here

class Resize in IMCE 8

Same name and namespace in other branches
  1. 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

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\ImcePlugin
View 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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
ImcePluginBase::alterProfileForm public function Alters entity form of an Imce Profile. Overrides ImcePluginInterface::alterProfileForm
ImcePluginBase::processUserConf public function Processes profile configuration for a user. Overrides ImcePluginInterface::processUserConf
ImcePluginBase::validateProfileForm public function Validates entity form of an Imce Profile. Overrides ImcePluginInterface::validateProfileForm
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
Resize::buildPage public function Builds imce page by adding required libraries and elements. Overrides ImcePluginBase::buildPage
Resize::opResize public function Operation handler: resize.
Resize::permissionInfo public function Returns folder permission definitions. Overrides ImcePluginBase::permissionInfo
Resize::resizeItems public function Resizes a list of imce items and returns succeeded ones.
Resize::validateResize public function Validates item resizing.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.