[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/task/checkfiles/src/Extension/ -> Checkfiles.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugins
   5   * @subpackage  Task.CheckFiles
   6   *
   7   * @copyright   (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Plugin\Task\Checkfiles\Extension;
  12  
  13  use Joomla\CMS\Image\Image;
  14  use Joomla\CMS\Plugin\CMSPlugin;
  15  use Joomla\Component\Scheduler\Administrator\Event\ExecuteTaskEvent;
  16  use Joomla\Component\Scheduler\Administrator\Task\Status as TaskStatus;
  17  use Joomla\Component\Scheduler\Administrator\Traits\TaskPluginTrait;
  18  use Joomla\Event\DispatcherInterface;
  19  use Joomla\Event\SubscriberInterface;
  20  use Joomla\Filesystem\Folder;
  21  use Joomla\Filesystem\Path;
  22  use LogicException;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('_JEXEC') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * Task plugin with routines that offer checks on files.
  30   * At the moment, offers a single routine to check and resize image files in a directory.
  31   *
  32   * @since  4.1.0
  33   */
  34  final class Checkfiles extends CMSPlugin implements SubscriberInterface
  35  {
  36      use TaskPluginTrait;
  37  
  38      /**
  39       * @var string[]
  40       *
  41       * @since 4.1.0
  42       */
  43      protected const TASKS_MAP = [
  44          'checkfiles.imagesize' => [
  45              'langConstPrefix' => 'PLG_TASK_CHECK_FILES_TASK_IMAGE_SIZE',
  46              'form'            => 'image_size',
  47              'method'          => 'checkImages',
  48          ],
  49      ];
  50  
  51      /**
  52       * @inheritDoc
  53       *
  54       * @return string[]
  55       *
  56       * @since 4.1.0
  57       */
  58      public static function getSubscribedEvents(): array
  59      {
  60          return [
  61              'onTaskOptionsList'    => 'advertiseRoutines',
  62              'onExecuteTask'        => 'standardRoutineHandler',
  63              'onContentPrepareForm' => 'enhanceTaskItemForm',
  64          ];
  65      }
  66  
  67      /**
  68       * @var boolean
  69       * @since 4.1.0
  70       */
  71      protected $autoloadLanguage = true;
  72  
  73      /**
  74       * The root directory path
  75       *
  76       * @var    string
  77       * @since  4.2.0
  78       */
  79      private $rootDirectory;
  80  
  81      /**
  82       * Constructor.
  83       *
  84       * @param   DispatcherInterface  $dispatcher     The dispatcher
  85       * @param   array                $config         An optional associative array of configuration settings
  86       * @param   string               $rootDirectory  The root directory to look for images
  87       *
  88       * @since   4.2.0
  89       */
  90      public function __construct(DispatcherInterface $dispatcher, array $config, string $rootDirectory)
  91      {
  92          parent::__construct($dispatcher, $config);
  93  
  94          $this->rootDirectory = $rootDirectory;
  95      }
  96  
  97      /**
  98       * @param   ExecuteTaskEvent  $event  The onExecuteTask event
  99       *
 100       * @return integer  The exit code
 101       *
 102       * @since 4.1.0
 103       * @throws RuntimeException
 104       * @throws LogicException
 105       */
 106      protected function checkImages(ExecuteTaskEvent $event): int
 107      {
 108          $params    = $event->getArgument('params');
 109          $path      = Path::check($this->rootDirectory . $params->path);
 110          $dimension = $params->dimension;
 111          $limit     = $params->limit;
 112          $numImages = max(1, (int) $params->numImages ?? 1);
 113  
 114          if (!is_dir($path)) {
 115              $this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_CHECK_FILES_LOG_IMAGE_PATH_NA'), 'warning');
 116  
 117              return TaskStatus::NO_RUN;
 118          }
 119  
 120          foreach (Folder::files($path, '^.*\.(jpg|jpeg|png|gif|webp)', 2, true) as $imageFilename) {
 121              $properties = Image::getImageFileProperties($imageFilename);
 122              $resize     = $properties->$dimension > $limit;
 123  
 124              if (!$resize) {
 125                  continue;
 126              }
 127  
 128              $height = $properties->height;
 129              $width  = $properties->width;
 130  
 131              $newHeight = $dimension === 'height' ? $limit : $height * $limit / $width;
 132              $newWidth  = $dimension === 'width' ? $limit : $width * $limit / $height;
 133  
 134              $this->logTask(sprintf(
 135                  $this->getApplication()->getLanguage()->_('PLG_TASK_CHECK_FILES_LOG_RESIZING_IMAGE'),
 136                  $width,
 137                  $height,
 138                  $newWidth,
 139                  $newHeight,
 140                  $imageFilename
 141              ));
 142  
 143              $image = new Image($imageFilename);
 144  
 145              try {
 146                  $image->resize($newWidth, $newHeight, false);
 147              } catch (LogicException $e) {
 148                  $this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_CHECK_FILES_LOG_RESIZE_FAIL'), 'error');
 149  
 150                  return TaskStatus::KNOCKOUT;
 151              }
 152  
 153              if (!$image->toFile($imageFilename, $properties->type)) {
 154                  $this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_CHECK_FILES_LOG_IMAGE_SAVE_FAIL'), 'error');
 155  
 156                  return TaskStatus::KNOCKOUT;
 157              }
 158  
 159              --$numImages;
 160  
 161              // We do a limited number of resize per execution
 162              if ($numImages == 0) {
 163                  break;
 164              }
 165          }
 166  
 167          return TaskStatus::OK;
 168      }
 169  }


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