[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/media-action/resize/ -> resize.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugin
   5   * @subpackage  Media-Action.resize
   6   *
   7   * @copyright   (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9  
  10   * @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
  11   */
  12  
  13  use Joomla\CMS\Image\Image;
  14  
  15  // phpcs:disable PSR1.Files.SideEffects
  16  \defined('_JEXEC') or die;
  17  // phpcs:enable PSR1.Files.SideEffects
  18  
  19  /**
  20   * Media Manager Resize Action
  21   *
  22   * @since  4.0.0
  23   */
  24  class PlgMediaActionResize extends \Joomla\Component\Media\Administrator\Plugin\MediaActionPlugin
  25  {
  26      /**
  27       * The save event.
  28       *
  29       * @param   string   $context  The context
  30       * @param   object   $item     The item
  31       * @param   boolean  $isNew    Is new item
  32       * @param   array    $data     The validated data
  33       *
  34       * @return  void
  35       *
  36       * @since   4.0.0
  37       */
  38      public function onContentBeforeSave($context, $item, $isNew, $data = array())
  39      {
  40          if ($context != 'com_media.file') {
  41              return;
  42          }
  43  
  44          if (!$this->params->get('batch_width') && !$this->params->get('batch_height')) {
  45              return;
  46          }
  47  
  48          if (!in_array($item->extension, ['jpg', 'jpeg', 'png', 'gif'])) {
  49              return;
  50          }
  51  
  52          $imgObject = new Image(imagecreatefromstring($item->data));
  53  
  54          if (
  55              $imgObject->getWidth() < $this->params->get('batch_width', 0)
  56              && $imgObject->getHeight() < $this->params->get('batch_height', 0)
  57          ) {
  58              return;
  59          }
  60  
  61          $imgObject->resize(
  62              $this->params->get('batch_width', 0),
  63              $this->params->get('batch_height', 0),
  64              false,
  65              Image::SCALE_INSIDE
  66          );
  67  
  68          $type = IMAGETYPE_JPEG;
  69  
  70          switch ($item->extension) {
  71              case 'gif':
  72                  $type = IMAGETYPE_GIF;
  73                  break;
  74              case 'png':
  75                  $type = IMAGETYPE_PNG;
  76          }
  77  
  78          ob_start();
  79          $imgObject->toFile(null, $type);
  80          $item->data = ob_get_contents();
  81          ob_end_clean();
  82      }
  83  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer