init
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<extension type="plugin" group="media-action" method="upgrade">
|
||||
<name>plg_media-action_crop</name>
|
||||
<author>Joomla! Project</author>
|
||||
<creationDate>2017-01</creationDate>
|
||||
<copyright>(C) 2017 Open Source Matters, Inc.</copyright>
|
||||
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
|
||||
<authorEmail>admin@joomla.org</authorEmail>
|
||||
<authorUrl>www.joomla.org</authorUrl>
|
||||
<version>4.0.0</version>
|
||||
<description>PLG_MEDIA-ACTION_CROP_XML_DESCRIPTION</description>
|
||||
<namespace path="src">Joomla\Plugin\MediaAction\Crop</namespace>
|
||||
<files>
|
||||
<folder>form</folder>
|
||||
<folder plugin="crop">services</folder>
|
||||
<folder>src</folder>
|
||||
</files>
|
||||
<languages>
|
||||
<language tag="en-GB">language/en-GB/plg_media-action_crop.ini</language>
|
||||
<language tag="en-GB">language/en-GB/plg_media-action_crop.sys.ini</language>
|
||||
</languages>
|
||||
<config>
|
||||
<fields name="params">
|
||||
<fieldset name="basic">
|
||||
<field
|
||||
name="aspect_ratios"
|
||||
type="subform"
|
||||
label="PLG_MEDIA-ACTION_CROP_ASPECT_RATIOS_LABEL"
|
||||
description="PLG_MEDIA-ACTION_CROP_ASPECT_RATIOS_DESC"
|
||||
multiple="true"
|
||||
layout="joomla.form.field.subform.repeatable-table"
|
||||
default='{"aspect_ratios0":{"label":"1:1","value":"1","group":""},"aspect_ratios1":{"label":"5:4","value":"5/4","group":"landscape"},"aspect_ratios2":{"label":"4:3","value":"4/3","group":"landscape"},"aspect_ratios3":{"label":"3:2","value":"3/2","group":"landscape"},"aspect_ratios4":{"label":"16:9","value":"16/9","group":"landscape"},"aspect_ratios5":{"label":"4:5","value":"4/5","group":"portrait"},"aspect_ratios6":{"label":"3:4","value":"3/4","group":"portrait"},"aspect_ratios7":{"label":"2:3","value":"2/3","group":"portrait"},"aspect_ratios8":{"label":"9:16","value":"9/16","group":"portrait"}}'
|
||||
>
|
||||
<form>
|
||||
<field
|
||||
name="label"
|
||||
type="text"
|
||||
label="PLG_MEDIA-ACTION_CROP_RATIO_LABEL_LABEL"
|
||||
description="PLG_MEDIA-ACTION_CROP_RATIO_LABEL_DESC"
|
||||
required="true"
|
||||
filter="string"
|
||||
/>
|
||||
<field
|
||||
name="value"
|
||||
type="text"
|
||||
label="PLG_MEDIA-ACTION_CROP_RATIO_VALUE_LABEL"
|
||||
description="PLG_MEDIA-ACTION_CROP_RATIO_VALUE_DESC"
|
||||
required="true"
|
||||
pattern="([1-9]\d*/[1-9]\d*|[1-9]\d*)"
|
||||
filter="string"
|
||||
hint="9/16"
|
||||
class="input-small"
|
||||
size="10"
|
||||
maxlength="10"
|
||||
/>
|
||||
<field
|
||||
name="group"
|
||||
type="list"
|
||||
label="PLG_MEDIA-ACTION_CROP_RATIO_GROUP_LABEL"
|
||||
description="PLG_MEDIA-ACTION_CROP_RATIO_GROUP_DESC"
|
||||
default=""
|
||||
>
|
||||
<option value="">PLG_MEDIA-ACTION_CROP_RATIO_GROUP_NONE</option>
|
||||
<option value="landscape">PLG_MEDIA-ACTION_CROP_PARAM_LANDSCAPE</option>
|
||||
<option value="portrait">PLG_MEDIA-ACTION_CROP_PARAM_PORTRAIT</option>
|
||||
</field>
|
||||
</form>
|
||||
</field>
|
||||
</fieldset>
|
||||
</fields>
|
||||
</config>
|
||||
</extension>
|
||||
@@ -0,0 +1,86 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form>
|
||||
<fieldset name="crop" label="PLG_MEDIA-ACTION_CROP_LABEL">
|
||||
<field
|
||||
name="crop_quality"
|
||||
type="number"
|
||||
label="PLG_MEDIA-ACTION_CROP_QUALITY"
|
||||
addonBefore="PLG_MEDIA-ACTION_CROP_QUALITY"
|
||||
min="1"
|
||||
max="100"
|
||||
step="1"
|
||||
default="80"
|
||||
filter="integer"
|
||||
/>
|
||||
|
||||
<field
|
||||
type="spacer"
|
||||
hr="true"
|
||||
/>
|
||||
|
||||
<field
|
||||
name="crop_x"
|
||||
type="text"
|
||||
label="PLG_MEDIA-ACTION_CROP_PARAM_X"
|
||||
hiddenLabel="true"
|
||||
addonBefore="PLG_MEDIA-ACTION_CROP_PARAM_X"
|
||||
addonAfter="px"
|
||||
pattern="\d*\.?\d*"
|
||||
/>
|
||||
|
||||
<field
|
||||
name="crop_y"
|
||||
type="text"
|
||||
label="PLG_MEDIA-ACTION_CROP_PARAM_Y"
|
||||
hiddenLabel="true"
|
||||
addonBefore="PLG_MEDIA-ACTION_CROP_PARAM_Y"
|
||||
addonAfter="px"
|
||||
pattern="\d*\.?\d*"
|
||||
/>
|
||||
|
||||
<field
|
||||
name="crop_width"
|
||||
type="text"
|
||||
label="PLG_MEDIA-ACTION_CROP_PARAM_WIDTH"
|
||||
hiddenLabel="true"
|
||||
addonBefore="PLG_MEDIA-ACTION_CROP_PARAM_WIDTH"
|
||||
addonAfter="px"
|
||||
pattern="\d*\.?\d*"
|
||||
/>
|
||||
|
||||
<field
|
||||
name="crop_height"
|
||||
type="text"
|
||||
label="PLG_MEDIA-ACTION_CROP_PARAM_HEIGHT"
|
||||
hiddenLabel="true"
|
||||
addonBefore="PLG_MEDIA-ACTION_CROP_PARAM_HEIGHT"
|
||||
addonAfter="px"
|
||||
pattern="\d*\.?\d*"
|
||||
/>
|
||||
|
||||
<field
|
||||
name="aspectRatio"
|
||||
type="groupedlist"
|
||||
label="PLG_MEDIA-ACTION_CROP_PARAM_ASPECT"
|
||||
hiddenLabel="true"
|
||||
class="crop-aspect-ratio-options"
|
||||
default="10/9"
|
||||
>
|
||||
<option class="crop-aspect-ratio-option" value="10/9">PLG_MEDIA-ACTION_CROP_PARAM_DEFAULT_RATIO</option>
|
||||
<option class="crop-aspect-ratio-option" value="">PLG_MEDIA-ACTION_CROP_PARAM_NO_RATIO</option>
|
||||
<option class="crop-aspect-ratio-option" value="1">1:1</option>
|
||||
<group label="PLG_MEDIA-ACTION_CROP_PARAM_LANDSCAPE">
|
||||
<option class="crop-aspect-ratio-option" value="5/4">5:4</option>
|
||||
<option class="crop-aspect-ratio-option" value="4/3">4:3</option>
|
||||
<option class="crop-aspect-ratio-option" value="3/2">3:2</option>
|
||||
<option class="crop-aspect-ratio-option" value="16/9">16:9</option>
|
||||
</group>
|
||||
<group label="PLG_MEDIA-ACTION_CROP_PARAM_PORTRAIT">
|
||||
<option class="crop-aspect-ratio-option" value="4/5">4:5</option>
|
||||
<option class="crop-aspect-ratio-option" value="3/4">3:4</option>
|
||||
<option class="crop-aspect-ratio-option" value="2/3">2:3</option>
|
||||
<option class="crop-aspect-ratio-option" value="9/16">9:16</option>
|
||||
</group>
|
||||
</field>
|
||||
</fieldset>
|
||||
</form>
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Plugin
|
||||
* @subpackage Media-Action.crop
|
||||
*
|
||||
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
\defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Extension\PluginInterface;
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Plugin\PluginHelper;
|
||||
use Joomla\DI\Container;
|
||||
use Joomla\DI\ServiceProviderInterface;
|
||||
use Joomla\Plugin\MediaAction\Crop\Extension\Crop;
|
||||
|
||||
return new class () implements ServiceProviderInterface {
|
||||
/**
|
||||
* Registers the service provider with a DI container.
|
||||
*
|
||||
* @param Container $container The DI container.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 4.4.0
|
||||
*/
|
||||
public function register(Container $container): void
|
||||
{
|
||||
$container->set(
|
||||
PluginInterface::class,
|
||||
$container->lazy(Crop::class, function (Container $container) {
|
||||
$plugin = new Crop(
|
||||
(array) PluginHelper::getPlugin('media-action', 'crop')
|
||||
);
|
||||
$plugin->setApplication(Factory::getApplication());
|
||||
|
||||
return $plugin;
|
||||
})
|
||||
);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,217 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Plugin
|
||||
* @subpackage Media-Action.crop
|
||||
*
|
||||
* @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Plugin\MediaAction\Crop\Extension;
|
||||
|
||||
use Joomla\CMS\Application\CMSWebApplicationInterface;
|
||||
use Joomla\CMS\Form\Form;
|
||||
use Joomla\Component\Media\Administrator\Plugin\MediaActionPlugin;
|
||||
use Joomla\Event\SubscriberInterface;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Media Manager Crop Action
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
final class Crop extends MediaActionPlugin implements SubscriberInterface
|
||||
{
|
||||
/**
|
||||
* The form event. Load additional parameters when available into the field form.
|
||||
* Override to dynamically inject aspect ratios from plugin settings.
|
||||
*
|
||||
* @param Form $form The form
|
||||
* @param \stdClass $data The data (required by the event interface, not used in this implementation)
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 6.1.0
|
||||
* @note The $data parameter is required by the Joomla event interface but is not used in this method.
|
||||
*/
|
||||
public function onContentPrepareForm(Form $form, $data): void
|
||||
{
|
||||
// Check if it is the right form
|
||||
if ($form->getName() !== 'com_media.file') {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->loadCss();
|
||||
$this->loadJs();
|
||||
|
||||
// The file with the params for the edit view
|
||||
$paramsFile = JPATH_PLUGINS . '/media-action/' . $this->_name . '/form/' . $this->_name . '.xml';
|
||||
|
||||
// When the file exists, load it into the form
|
||||
if (file_exists($paramsFile)) {
|
||||
$form->loadFile($paramsFile);
|
||||
|
||||
// Get the aspect ratios from plugin parameters
|
||||
$aspectRatios = $this->params->get('aspect_ratios');
|
||||
|
||||
// Convert to array if needed (handles stdClass from Registry)
|
||||
if (\is_object($aspectRatios)) {
|
||||
$aspectRatios = (array) $aspectRatios;
|
||||
}
|
||||
|
||||
// If we have custom aspect ratios, modify the form field
|
||||
if (!empty($aspectRatios) && \is_array($aspectRatios)) {
|
||||
$this->injectAspectRatios($form, $aspectRatios);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject custom aspect ratios into the crop form
|
||||
*
|
||||
* @param Form $form The form object
|
||||
* @param array $aspectRatios The aspect ratios from plugin settings
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 6.1.0
|
||||
*/
|
||||
private function injectAspectRatios(Form $form, array $aspectRatios): void
|
||||
{
|
||||
// Get the aspectRatio field (try without group first, then with 'crop' group)
|
||||
$field = $form->getField('aspectRatio');
|
||||
|
||||
if (!$field) {
|
||||
$field = $form->getField('aspectRatio', 'crop');
|
||||
}
|
||||
|
||||
if (!$field) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Build new XML for the field with custom options
|
||||
$xml = new \SimpleXMLElement('<field/>');
|
||||
$xml->addAttribute('name', 'aspectRatio');
|
||||
$xml->addAttribute('type', 'groupedlist');
|
||||
$xml->addAttribute('label', 'PLG_MEDIA-ACTION_CROP_PARAM_ASPECT');
|
||||
$xml->addAttribute('hiddenLabel', 'true');
|
||||
$xml->addAttribute('class', 'crop-aspect-ratio-options');
|
||||
$xml->addAttribute('default', '10/9');
|
||||
|
||||
// Add default options
|
||||
$option = $xml->addChild('option', 'PLG_MEDIA-ACTION_CROP_PARAM_DEFAULT_RATIO');
|
||||
$option->addAttribute('class', 'crop-aspect-ratio-option');
|
||||
$option->addAttribute('value', '10/9');
|
||||
|
||||
$option = $xml->addChild('option', 'PLG_MEDIA-ACTION_CROP_PARAM_NO_RATIO');
|
||||
$option->addAttribute('class', 'crop-aspect-ratio-option');
|
||||
$option->addAttribute('value', '');
|
||||
|
||||
// Group ratios by landscape/portrait
|
||||
$grouped = [
|
||||
'' => [],
|
||||
'landscape' => [],
|
||||
'portrait' => [],
|
||||
];
|
||||
|
||||
foreach ($aspectRatios as $ratio) {
|
||||
// Convert individual ratio to array if it's an object
|
||||
$ratio = (array) $ratio;
|
||||
|
||||
$label = $ratio['label'] ?? '';
|
||||
$value = $ratio['value'] ?? '';
|
||||
$group = strtolower(trim($ratio['group'] ?? ''));
|
||||
|
||||
if (empty($label) || $value === '' || $value === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isset($grouped[$group])) {
|
||||
$grouped[$group] = [];
|
||||
}
|
||||
|
||||
$grouped[$group][] = [
|
||||
'label' => $label,
|
||||
'value' => $value,
|
||||
'group' => $group,
|
||||
];
|
||||
}
|
||||
|
||||
// Add ungrouped ratios
|
||||
foreach ($grouped[''] as $ratio) {
|
||||
$option = $xml->addChild('option', htmlspecialchars($ratio['label'], ENT_XML1, 'UTF-8'));
|
||||
$option->addAttribute('class', 'crop-aspect-ratio-option');
|
||||
$option->addAttribute('value', htmlspecialchars($ratio['value'], ENT_XML1, 'UTF-8'));
|
||||
}
|
||||
|
||||
// Add landscape group
|
||||
if (!empty($grouped['landscape'])) {
|
||||
$group = $xml->addChild('group');
|
||||
$group->addAttribute('label', 'PLG_MEDIA-ACTION_CROP_PARAM_LANDSCAPE');
|
||||
|
||||
foreach ($grouped['landscape'] as $ratio) {
|
||||
$option = $group->addChild('option', htmlspecialchars($ratio['label'], ENT_XML1, 'UTF-8'));
|
||||
$option->addAttribute('class', 'crop-aspect-ratio-option');
|
||||
$option->addAttribute('value', htmlspecialchars($ratio['value'], ENT_XML1, 'UTF-8'));
|
||||
}
|
||||
}
|
||||
|
||||
// Add portrait group
|
||||
if (!empty($grouped['portrait'])) {
|
||||
$group = $xml->addChild('group');
|
||||
$group->addAttribute('label', 'PLG_MEDIA-ACTION_CROP_PARAM_PORTRAIT');
|
||||
|
||||
foreach ($grouped['portrait'] as $ratio) {
|
||||
$option = $group->addChild('option', htmlspecialchars($ratio['label'], ENT_XML1, 'UTF-8'));
|
||||
$option->addAttribute('class', 'crop-aspect-ratio-option');
|
||||
$option->addAttribute('value', htmlspecialchars($ratio['value'], ENT_XML1, 'UTF-8'));
|
||||
}
|
||||
}
|
||||
|
||||
// Replace the field in the form
|
||||
// Try setting in default group first, then 'crop' group
|
||||
if (!$form->setField($xml, null, true)) {
|
||||
$form->setField($xml, 'crop', true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the javascript files of the plugin.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected function loadJs(): void
|
||||
{
|
||||
parent::loadJs();
|
||||
|
||||
if (!$this->getApplication() instanceof CMSWebApplicationInterface) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->getApplication()->getDocument()->getWebAssetManager()->useScript('cropper-module');
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the CSS files of the plugin.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected function loadCss(): void
|
||||
{
|
||||
parent::loadCss();
|
||||
|
||||
if (!$this->getApplication() instanceof CMSWebApplicationInterface) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->getApplication()->getDocument()->getWebAssetManager()->useStyle('cropperjs');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user