Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.1] Fix Image Size Check with scheduler #36653

Merged
merged 10 commits into from
Jan 14, 2022
4 changes: 3 additions & 1 deletion administrator/language/en-GB/plg_task_checkfiles.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

PLG_TASK_CHECK_FILES="Task - Check Files"
PLG_TASK_CHECK_FILES_LABEL_DIMENSION_LIMIT="Limit (px)"
PLG_TASK_CHECK_FILES_LABEL_MAXIMAGES="Maximum Images"
PLG_TASK_CHECK_FILES_LABEL_MAXIMAGES_DESC="The maximum number of images to be processed in a single run. Setting this number too high may cause the task to timeout."
PLG_TASK_CHECK_FILES_LABEL_DIRECTORY="Directory"
PLG_TASK_CHECK_FILES_LABEL_IMAGE_DIMENSION="Dimension"
PLG_TASK_CHECK_FILES_LOG_IMAGE_PATH_NA="Image path does exist!"
PLG_TASK_CHECK_FILES_LOG_IMAGE_SAVE_FAIL="Failed to save image file"
PLG_TASK_CHECK_FILES_LOG_RESIZE_FAIL="Failed to resize image due to an error in plugin logic..."
PLG_TASK_CHECK_FILES_LOG_RESIZING_IMAGE="Found image of size %1$sx%2$s px; resizing to %3$sx%4$s px. File: %5$s"
PLG_TASK_CHECK_FILES_TASK_IMAGE_SIZE_DESC="Check images, resize if larger than allowed."
PLG_TASK_CHECK_FILES_TASK_IMAGE_SIZE_DESC="Check images, resize if larger than allowed. Attention: The original file will be overwritten!"
PLG_TASK_CHECK_FILES_TASK_IMAGE_SIZE_TITLE="Image Size Check"
PLG_TASK_CHECK_FILES_XML_DESCRIPTION="Offers task routines for checking for oversized files, and related actions if possible."
15 changes: 11 additions & 4 deletions plugins/task/checkfiles/checkfiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Joomla\Component\Scheduler\Administrator\Task\Status as TaskStatus;
use Joomla\Component\Scheduler\Administrator\Traits\TaskPluginTrait;
use Joomla\Event\SubscriberInterface;
use Joomla\Filesystem\Path;

/**
* Task plugin with routines that offer checks on files.
Expand Down Expand Up @@ -77,9 +78,10 @@ protected function checkImages(ExecuteTaskEvent $event): int
{
$params = $event->getArgument('params');

$path = JPATH_ROOT . '/images/' . $params->path;
$path = Path::check(JPATH_ROOT . '/images/' . $params->path);
$dimension = $params->dimension;
$limit = $params->limit;
$numImages = max(1, (int) $params->numImages ?? 1);

if (!Folder::exists($path))
{
Expand Down Expand Up @@ -112,7 +114,7 @@ protected function checkImages(ExecuteTaskEvent $event): int

try
{
$image->resize($newWidth, $newHeight);
$image->resize($newWidth, $newHeight, false);
}
catch (LogicException $e)
{
Expand All @@ -130,8 +132,13 @@ protected function checkImages(ExecuteTaskEvent $event): int
$this->logTask('PLG_TASK_CHECK_FILES_LOG_IMAGE_SAVE_FAIL', 'error');
}

// We do at most a single resize per execution
break;
--$numImages;

// We do a limited number of resize per execution
if ($numImages == 0)
{
break;
}
}

return TaskStatus::OK;
Expand Down
14 changes: 13 additions & 1 deletion plugins/task/checkfiles/forms/image_size.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
type="list"
label="PLG_TASK_CHECK_FILES_LABEL_IMAGE_DIMENSION"
required="true"
default="width"
>
<option value="width">JFIELD_MEDIA_WIDTH_LABEL</option>
<option value="height">JFIELD_MEDIA_HEIGHT_LABEL</option>
Expand All @@ -29,10 +30,21 @@
label="PLG_TASK_CHECK_FILES_LABEL_DIMENSION_LIMIT"
required="true"
default="1080"
min="100"
min="1"
step="1"
filter="int"
/>
<field
name="numImages"
type="number"
label="PLG_TASK_CHECK_FILES_LABEL_MAXIMAGES"
description="PLG_TASK_CHECK_FILES_LABEL_MAXIMAGES_DESC"
required="true"
min="1"
step="1"
default="1"
filter="int"
/>
</fieldset>
</fields>
</form>