Skip to content

Instantly share code, notes, and snippets.

@di7spider
Last active May 5, 2019 22:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save di7spider/1f4320c5e29681f72737b6508b7e06a2 to your computer and use it in GitHub Desktop.
Save di7spider/1f4320c5e29681f72737b6508b7e06a2 to your computer and use it in GitHub Desktop.
1C Bitrix (Google PageSpeed) :: CFile::ResizeImageGet + optipng + jpegtran
<?php
$em = \Bitrix\Main\EventManager::getInstance();
$em->addEventHandler("main", "OnAfterResizeImage", function ($file, $options, &$cacheImageFile, &$cacheImageFileTmp, &$arImageSize){
$path = escapeshellarg($arImageSize);
if( !empty($path) ){
try{
if($file['CONTENT_TYPE'] == 'image/png'){
exec('/usr/bin/optipng -o7 -strip all '.$path);
}else if($file['CONTENT_TYPE'] == 'image/jpeg'){
exec('/usr/bin/jpegtran -copy none -optimize -outfile '.$path.' '.$path);
}
}catch (\Exception $e){}
}
});
?>
@di7spider
Copy link
Author

di7spider commented Oct 31, 2016

Обработчик для Bitrix метода CFile::ResizeImageGet - сжимает итоговое изображение при помощи библиотек optipng и jpegtran

Разместить в /local/php_interface/init.php или /bitrix/php_interface/init.php

На сервере должны быть установлены библиотеки:
http://optipng.sourceforge.net/
http://jpegclub.org/

Дополнительная инфа:
https://developers.google.com/speed/docs/insights/OptimizeImages
http://bxapi.ru/src/?module_id=main&name=CFile::ResizeImageGet
http://dev.1c-bitrix.ru/api_help/main/reference/cfile/resizeimageget.php

@111ypuk
Copy link

111ypuk commented Dec 4, 2017

параметра $callbackData не хватает

 foreach(GetModuleEvents("main", "OnAfterResizeImage", true) as $arEvent)
                {
                    if(ExecuteModuleEventEx($arEvent, array(
                        $file,
                        array($arSize, $resizeType, array(), false, $arFilters),
                        &$callbackData,
                        &$cacheImageFile,
                        &$cacheImageFileTmp,
                        &$arImageSize,
                    )))
                        break;
                }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment