- Для работы нужен сниппет imgToWebp
- Включить событие
OnBeforeDocFormSave
Last active
July 26, 2020 22:20
-
-
Save TrywaR/b530d92a2dfdd0816a0514c98f447389 to your computer and use it in GitHub Desktop.
MODX Revo плагин для конвертации изображений ресурса в webp, при его сохранении
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
switch ($modx->event->name) { | |
// После сохранения ресурса | |
case 'OnBeforeDocFormSave': | |
// MINISHOP2 | |
// - Если это товар, обработка картинок галлереи | |
if ( 'msProduct' == $resource->get('class_key') ) { | |
// - Получаем товар картинки товара | |
if ($oProduct = $modx->getObject('msProductData', $resource->get('id'))) { | |
$files = $oProduct->getMany('Files'); | |
// - Перебираем картинки | |
foreach ($files as $file) | |
$modx->runSnippet('imgToWebp', array( | |
'sImgUrl' => $file->get('url') | |
)); | |
} | |
} | |
// Картинки из доп свойств | |
// MIGX | |
$intMIGXTvId = 1; # id доп свойства | |
$sMIGXImgName = 'image'; # Параметр с картинкой | |
$sMIGXImgDir = ''; # Путь до картинок | |
// - Вытаскиваем адреса картинок из MIGX в массив | |
$oMIGXImgs = $modx->getObject('modTemplateVarResource', array( | |
'tmplvarid' => $intMIGXTvId, | |
'contentid' => $resource->get('id') | |
)); | |
if ( $oMIGXImgs ) { | |
$sMIGXImgs = $oMIGXImgs->get('value'); | |
$arrMIGXImgs = json_decode($sMIGXImgs, true); | |
if ( is_array($arrMIGXImgs) ) { | |
foreach ($arrMIGXImgs as $arrMIGXImg) { | |
$modx->runSnippet('imgToWebp', array( | |
'sImgUrl' => $sMIGXImgDir.$arrMIGXImg[$sMIGXImgName] | |
)); | |
} | |
} | |
} | |
// Из обычной TV с картиной | |
$sTvId = 2; # id доп свойства | |
$oImg = $modx->getObject('modTemplateVarResource', array( | |
'tmplvarid' => $sTvId, | |
'contentid' => $resource->get('id') | |
)); | |
if ( $oImg ) { | |
$sImgUrl = $oImg->get('value'); | |
if ( $sImgUrl ) { | |
$modx->runSnippet('imgToWebp', array( | |
'sImgUrl' => $sImgUrl | |
)); | |
} | |
} | |
// GALLERY | |
// ??? | |
break; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment