Skip to content

Commit

Permalink
Добавление превью картинок и исправление путей
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriitux committed Jan 14, 2019
1 parent e8ab834 commit e4ccbe4
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 27 deletions.
3 changes: 3 additions & 0 deletions language/en-GB/en-GB.plg_fields_radicalmultifield.ini
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTRESIZE_LABEL="Enable image resiz
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTRESIZE_DESC="When the resize is enabled, the images will resize under the specified maximum width and height"
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTRESIZEMAXWIDTH_LABEL="Maximum width <br /> in pixels"
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTRESIZEMAXHEIGHT_LABEL="Maximum height <br /> in pixels"
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTPREVIEW_LABEL="Generate preview images"
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTPREVIEWMAXWIDTH_LABEL="Max width preview <br /> in pixels"
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTPREVIEWMAXHEIGHT_LABEL="Max height preview <br /> in pixels"
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTRESIZEOVERLAY_LABEL="Apply a watermark"
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTRESIZEOVERLAYFILE_LABEL="Watermark File"
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTRESIZEOVERLAYPOS_LABEL="Select a position for the sign"
Expand Down
3 changes: 3 additions & 0 deletions language/ru-RU/ru-RU.plg_fields_radicalmultifield.ini
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTRESIZE_LABEL="Подгонять
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTRESIZE_DESC="При включеном параметре, изображения будут изменять размер под указанные максимальную ширину и высоту в пикселях"
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTRESIZEMAXWIDTH_LABEL="Максимальная ширина <br /> в пикселях"
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTRESIZEMAXHEIGHT_LABEL="Максимальная высота <br /> в пикселях"
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTPREVIEW_LABEL="Генерация превью картинок"
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTPREVIEWMAXWIDTH_LABEL="Максимальная ширина превью <br /> в пикселях"
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTPREVIEWMAXHEIGHT_LABEL="Максимальная высота превью <br /> в пикселях"
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTRESIZEOVERLAY_LABEL="Накладывать водяной знак"
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTRESIZEOVERLAYFILE_LABEL="Файл водяного знака"
PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTRESIZEOVERLAYPOS_LABEL="Выберите позицию для знака"
Expand Down
30 changes: 30 additions & 0 deletions params/radicalmultifield.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,36 @@
showon="filesimportresize:1[AND]filesimport:1"
/>

<field name="filesimportpreview"
type="radio"
label="PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTPREVIEW_LABEL"
description="PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTPREVIEW_DESC"
required="true"
class="btn-group"
default="0"
showon="filesimport:1">
<option value="1">JYES</option>
<option value="0">JNO</option>
</field>

<field name="filesimportpreviewmaxwidth"
type="text"
label="PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTPREVIEWMAXWIDTH_LABEL"
required="true"
default="360"
class="input-xlarge"
showon="filesimportpreview:1[AND]filesimport:1"
/>

<field name="filesimportpreviewmaxheight"
type="text"
label="PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTPREVIEWMAXHEIGHT_LABEL"
required="true"
default="360"
class="input-xlarge"
showon="filesimportpreview:1[AND]filesimport:1"
/>

<field name="filesimportrezizeoverlay"
type="radio"
label="PLG_RADICAL_MULTI_FIELD_FIELD_PARAMS_FILESIMPORTRESIZEOVERLAY_LABEL"
Expand Down
48 changes: 26 additions & 22 deletions radicalmultifield.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,30 +249,30 @@ public function onContentPrepareForm(JForm $form, $data)
$tmp = explode('_', $type);
$type = $tmp[0];
$pluginType = $tmp[1];
$pathType = JPATH_PLUGINS . '/radicalmultifield/' . $pluginType . '/params';
$pathType = JPATH_PLUGINS . DIRECTORY_SEPARATOR . 'radicalmultifield' . DIRECTORY_SEPARATOR . $pluginType . DIRECTORY_SEPARATOR . 'params';
}
else
{
$pluginType = '';
$pathType = '';
}

$path = JPATH_PLUGINS . '/' . $this->_type . '/' . $this->_name . '/params/' . $type . '.xml';
$path = JPATH_PLUGINS . DIRECTORY_SEPARATOR . $this->_type . DIRECTORY_SEPARATOR . $this->_name . DIRECTORY_SEPARATOR . 'params' . DIRECTORY_SEPARATOR . $type . '.xml';

// Check if params file exists
if (!file_exists($path))
{
return;
}

JLoader::import('radicalmultifieldhelper', JPATH_ROOT . '/plugins/fields/radicalmultifield');
JLoader::register('RadicalmultifieldHelper', JPATH_ROOT . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, ['plugins', 'fields', 'radicalmultifield', 'radicalmultifieldhelper']) . '.php');

$paramsfield = file_get_contents($path);

if(!empty($pluginType)) {

if(file_exists($pathType . '/newparams.xml')) {
$newParams = file_get_contents($pathType . '/newparams.xml');
if(file_exists($pathType . DIRECTORY_SEPARATOR . 'newparams.xml')) {
$newParams = file_get_contents($pathType . DIRECTORY_SEPARATOR . 'newparams.xml');
$paramsfield = str_replace('</fieldset>', $newParams . '</fieldset>', $paramsfield);
}

Expand Down Expand Up @@ -325,7 +325,7 @@ public function onContentPrepareForm(JForm $form, $data)

if(file_exists($pathType))
{
$paramsfieldValues = file_get_contents($pathType . '/default.xml');
$paramsfieldValues = file_get_contents($pathType . DIRECTORY_SEPARATOR . 'default.xml');
$paramsfieldValuesXML = new SimpleXMLElement($paramsfieldValues);

for ($i = 0; $i < count($paramsfieldValuesXML->fields->fieldset->field); $i++)
Expand Down Expand Up @@ -392,7 +392,7 @@ public function onAjaxRadicalmultifield()
$admin = $app->isAdmin();
$allow = true;

JLoader::register('RadicalmultifieldHelper', JPATH_SITE . '/plugins/fields/radicalmultifield/radicalmultifieldhelper.php');
JLoader::register('RadicalmultifieldHelper', JPATH_ROOT . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, ['plugins', 'fields', 'radicalmultifield', 'radicalmultifieldhelper']) . '.php');

$params = RadicalmultifieldHelper::getParams($data['importfield']);

Expand Down Expand Up @@ -432,10 +432,10 @@ public function onAjaxRadicalmultifield()
if($data['type'] === 'get_files')
{
$exs = explode(',', $params['filesimportexc']);
$directory = ($data['importfieldpath'] ? $data['importfieldpath'] . '/' : '') . preg_replace('/^root/isu', '', $data['directory']);
$directory = str_replace("//", '/', $directory);
$directory = ($data['importfieldpath'] ? $data['importfieldpath'] . DIRECTORY_SEPARATOR : '') . preg_replace('/^root/isu', '', $data['directory']);
$directory = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $directory);
$filesOutput = [];
$files = Folder::files(JPATH_ROOT . '/' .(string) $directory);
$files = Folder::files(JPATH_ROOT . DIRECTORY_SEPARATOR .(string) $directory);
foreach ($files as $file)
{
$tmpExs = explode('.', $file);
Expand Down Expand Up @@ -503,25 +503,25 @@ public function onAjaxRadicalmultifield()

$data['name'] = isset($data['name']) ? $data['name'] : '';

$path = JPATH_ROOT . '/' . ($data['importfieldpath'] ? $data['importfieldpath'] . '/' : '') . preg_replace('/^root/isu', '', $data['path']) . '/' . $data['name'];
$path = str_replace("//", '/', $path);
$path = JPATH_ROOT . DIRECTORY_SEPARATOR . ($data['importfieldpath'] ? $data['importfieldpath'] . DIRECTORY_SEPARATOR : '') . preg_replace('/^root/isu', '', $data['path']) . DIRECTORY_SEPARATOR . $data['name'];
$path = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $path);

if (!file_exists($path))
{
Folder::create($path);
}

if (File::upload($file['tmp_name'], $path . "/" . $uploadedFileName))
if (File::upload($file['tmp_name'], $path . DIRECTORY_SEPARATOR . $uploadedFileName))
{

if(in_array($type, ['jpg', 'gif', 'png', 'jpeg', 'jpg', 'webp']))
{
if((int)$params['filesimportresize'])
{

JLoader::registerNamespace('Gumlet', JPATH_SITE . '/' . implode(DIRECTORY_SEPARATOR , ['plugins', 'fields', 'radicalmultifield', 'libs', 'gumlet', 'lib']));
JLoader::registerNamespace('Gumlet', JPATH_SITE . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR , ['plugins', 'fields', 'radicalmultifield', 'libs', 'gumlet', 'lib']));

$image = new ImageResize($path . "/" . $uploadedFileName);
$image = new ImageResize($path . DIRECTORY_SEPARATOR . $uploadedFileName);

$maxWidth = (int)$params['filesimportrezizemaxwidth'];
$maxHeight = (int)$params['filesimportrezizemaxheight'];
Expand All @@ -533,7 +533,7 @@ public function onAjaxRadicalmultifield()
if((int)$params['filesimportrezizeoverlay'])
{

$file = JPATH_SITE . '/' . $params['filesimportrezizeoverlayfile'];
$file = JPATH_SITE . DIRECTORY_SEPARATOR . $params['filesimportrezizeoverlayfile'];
$position = $params['filesimportrezizeoverlaypos'];
$padding = $params['filesimportrezizeoverlaypadding'];

Expand Down Expand Up @@ -618,7 +618,12 @@ public function onAjaxRadicalmultifield()

}

$image->save($path . "/" . $uploadedFileName);
$image->save($path . DIRECTORY_SEPARATOR . $uploadedFileName);

if((int)$params['filesimportpreview'])
{
RadicalmultifieldHelper::generateThumb($params, $path . DIRECTORY_SEPARATOR . $uploadedFileName);
}

}
}
Expand All @@ -640,11 +645,11 @@ public function onAjaxRadicalmultifield()
{
$lang = Factory::getLanguage();
$data['name'] = JFILE::makeSafe($lang->transliterate($data['name']));
$path = JPATH_ROOT . '/' . ($data['importfieldpath'] ? $data['importfieldpath'] . '/' : '') . preg_replace('/^root/isu', '', $data['path']) . '/' . $data['name'];
$path = str_replace("//", '/', $path);
$path = JPATH_ROOT . DIRECTORY_SEPARATOR . ($data['importfieldpath'] ? $data['importfieldpath'] . DIRECTORY_SEPARATOR : '') . preg_replace('/^root/isu', '', $data['path']) . DIRECTORY_SEPARATOR . $data['name'];
$path = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $path);
Folder::create($path);

JLoader::register('FormFieldRadicalmultifieldtreecatalog', JPATH_ROOT . '/plugins/fields/radicalmultifield/elements/radicalmultifieldtreecatalog.php');
JLoader::register('FormFieldRadicalmultifieldtreecatalog', JPATH_ROOT . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, ['plugins', 'fields', 'radicalmultifield', 'elements', 'radicalmultifieldtreecatalog']) . '.php');
$treeCatalog = new FormFieldRadicalmultifieldtreecatalog;
$paramsForField = [
'name' => 'select-directory',
Expand All @@ -656,8 +661,7 @@ public function onAjaxRadicalmultifield()
'showroot' => 'true',
];

$dataAttributes = array_map(function($value, $key)
{
$dataAttributes = array_map(function($value, $key) {
return $key . '="' . $value . '"';
}, array_values($paramsForField), array_keys($paramsForField));

Expand Down
2 changes: 1 addition & 1 deletion radicalmultifield.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<authorUrl>https://delo-design.ru</authorUrl>
<copyright>Copyright (C) 2018 "Delo Design". All rights reserved.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<version>2.0</version>
<version>2.1</version>
<description>PLG_RADICAL_MULTI_FIELD_XML_DESCRIPTION</description>

<scriptfile>script.php</scriptfile>
Expand Down
92 changes: 90 additions & 2 deletions radicalmultifieldhelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Joomla\CMS\Factory;
use Joomla\Filesystem\Folder;
use Gumlet\ImageResize;


defined('_JEXEC') or die;
Expand Down Expand Up @@ -277,15 +278,18 @@ public static function check($field_id, $item_id, $data = [], $column_find_all =
foreach ($item as $keyRow => $row)
{

if($column_find_all) {
if($column_find_all)
{

if(!isset($data[$keyRow]) || ((string)$row !== (string)$data[$keyRow]))
{
$findTmp = false;
break;
}

} else {
}
else
{

if(isset($data[$keyRow]) && ((string)$row == (string)$data[$keyRow]))
{
Expand Down Expand Up @@ -380,4 +384,88 @@ public static function loadClassExtendField($path)
return $fileListsName;
}


/**
* @param $fieldOrParams
* @param $source
*
* @return mixed|string
*/
public static function generateThumb(&$fieldOrParams, $source)
{
$source = str_replace(JPATH_ROOT . DIRECTORY_SEPARATOR, '', $source);
$paths = explode(DIRECTORY_SEPARATOR, $source);
$file = array_pop($paths);
$fileSplit = explode('.', $file);
$fileExt = array_pop($fileSplit);
$extAccept = ['jpg', 'jpeg', 'png', 'gif'];

if(!in_array($fileExt, $extAccept))
{
return $file;
}

$pathThumb = implode(DIRECTORY_SEPARATOR, array_merge($paths, ['_thumb']));
$pathFileThumb = implode(DIRECTORY_SEPARATOR, array_merge($paths, ['_thumb'])) . DIRECTORY_SEPARATOR . $file;
$fullPathThumb = JPATH_ROOT . DIRECTORY_SEPARATOR . $pathThumb . DIRECTORY_SEPARATOR . $file;

//если есть проевью, то отдаем ссылку на файл
if(file_exists($fullPathThumb))
{
return $pathFileThumb;
}

//если нет, генерируем превью

//проверяем создан ли каталог для превью
if(!file_exists(JPATH_ROOT . DIRECTORY_SEPARATOR . $pathThumb))
{
//создаем каталог
Folder::create(JPATH_ROOT . DIRECTORY_SEPARATOR . $pathThumb);
}

//подгружаем библиотеку для фото
JLoader::registerNamespace('Gumlet', JPATH_SITE . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR , ['plugins', 'fields', 'radicalmultifield', 'libs', 'gumlet', 'lib']));

$params = [];

if(is_object($fieldOrParams))
{
if(!isset($fieldOrParams->name))
{
return $source;
}

//подгружаем параметры поля
$params = self::getParams($fieldOrParams->name);

}

if(is_array($fieldOrParams))
{
$params = $fieldOrParams;
}


if(!isset($params['filesimportpreviewmaxwidth']) || !isset($params['filesimportpreviewmaxheight']))
{
return $source;
}

copy(JPATH_ROOT . DIRECTORY_SEPARATOR . $source, $fullPathThumb);
$image = new ImageResize($fullPathThumb);

$maxWidth = (int)$params['filesimportpreviewmaxwidth'];
$maxHeight = (int)$params['filesimportpreviewmaxheight'];

$image->resizeToBestFit($maxWidth, $maxHeight);
$image->save($fullPathThumb);
unset($image);

return $pathFileThumb;

}



}
4 changes: 3 additions & 1 deletion tmpl/galleryforuikit2.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
$values = json_decode($field->value, JSON_OBJECT_AS_ARRAY);
$listtype = $this->getListTypeFromField($field);

jimport('radicalmultifieldhelper', JPATH_ROOT . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, ['plugins', 'fields', 'radicalmultifield'])); //подключаем хелпер

?>


Expand All @@ -26,7 +28,7 @@

<div>
<a href="<?= $row['image']?>" data-uk-lightbox="{group:'group-fields-<?= $field->id ?>'}" title="<?= $row['alt'] ?>">
<img src="<?= $row['image']?>" alt="<?= $row['alt'] ?>"/>
<img src="<?= RadicalmultifieldHelper::generateThumb($field, $row['image'])?>" alt="<?= $row['alt'] ?>"/>
</a>
</div>

Expand Down
4 changes: 3 additions & 1 deletion tmpl/galleryforuikit3.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
$values = json_decode($field->value, JSON_OBJECT_AS_ARRAY);
$listtype = $this->getListTypeFromField($field);

jimport('radicalmultifieldhelper', JPATH_ROOT . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, ['plugins', 'fields', 'radicalmultifield'])); //подключаем хелпер

?>

<div class="uk-child-width-1-5@m" uk-grid uk-height-match="img" uk-lightbox="animation: slide">
<?php foreach ($values as $key => $row): ?>

<div>
<a class="uk-inline" href="<?= $row['image'] ?>" data-caption="<?= $row['alt'] ?>">
<img src="<?= $row['image']?>" alt="<?= $row['alt'] ?>"/>
<img src="<?= RadicalmultifieldHelper::generateThumb($field, $row['image'])?>" alt="<?= $row['alt'] ?>"/>
</a>
</div>

Expand Down

0 comments on commit e4ccbe4

Please sign in to comment.