Некоторые события плагинов
Подготовка доп.полей (system):
public function onCustomFieldsPrepareDom($field, DOMElement $parent, Form $form)
{
$option = $this->app->input->getCmd('option', '');
$view = $this->app->input->getCmd('view', '');
$layout = $this->app->input->getCmd('layout', '');
$id = $this->app->input->getInt('id', 0);
$onContactEdit = $option == 'com_contact' && $view == 'contact' && $layout == 'edit';
$onContentEdit = $option == 'com_content' && $view == 'article' && $layout == 'edit';
$onEdit = $onContactEdit || $onContentEdit;
// Для полей media дополняем путь к изображению ИДом владельца
if($field->type == 'media' && $onEdit){
if(!$id){
$field->type = 'hidden';
return;
}
$dir = $field->fieldparams->get('directory', '');
$dir = empty($dir) ? $id : $dir.'/'.$id;
if(!empty($field->fieldname)) $dir .= '/'.$field->fieldname;
$path = JPATH_SITE.'/images/'.$dir;
if(!file_exists($path)) mkdir($path, 0755, true);
$field->fieldparams->set('directory', $dir);
//~ echo '
'.print_r($field->fieldname,1).'
'; } }
Фиксирование размеров картинки с добавлением полей заданного цвета (content):
use Joomla\CMS\Image\Image;
public function onContentBeforeSave($context, $item, $isNew, $data = array())
{
if ($context == 'com_media.file')
{
$path = $this->app->input->getString('path', '');
if(strpos($path, '/slide') === false) return;
$color = hexdec(str_replace('#', '', '#FF9933'));
$image = imagecreatefromstring($item->data);
imagefill($image, 0, 0, $color);
imagecolortransparent($image, $color);
//~ $imgObject = new Image(imagecreatefromstring($item->data));
$imgObject = new Image($image);
$imgObject->resize(
1200,
900,
false,
Image::SCALE_FIT
);
$type = IMAGETYPE_JPEG;
switch ($item->extension)
{
case 'gif':
$type = IMAGETYPE_GIF;
break;
case 'png':
$type = IMAGETYPE_PNG;
}
ob_start();
$imgObject->toFile(null, $type);
$item->data = ob_get_contents();
ob_end_clean();
}
Это решение является расширением функционала плагина /plugins/media-action/resize/
Простое преобразование изображения под размер с добавлением полей белого цвета
$imgObject = new JImage($src_file);
$imgObject->resize(
$width,
$height,
false,
JImage::SCALE_FIT
);
$imgObject->filter('backgroundfill', array('color' => '#FFFFFF'));
$imgObject->toFile($dst_file);
