Skip to content

Instantly share code, notes, and snippets.

@Feiron
Last active July 20, 2021 11:39
Show Gist options
  • Save Feiron/dd4c2f2776f5855f85deeb7d7999e3f0 to your computer and use it in GitHub Desktop.
Save Feiron/dd4c2f2776f5855f85deeb7d7999e3f0 to your computer and use it in GitHub Desktop.
[Image resize] #bitrix #examples #image
<?php
/**
* Images resize on the fly
**/
$arParams['THUMB']['width'] = 48;
$arParams['THUMB']['height'] = 48;
$arParams['PREVIEW_PICTURE']['height'] = 275;
$arParams['PREVIEW_PICTURE']['width'] = 350;
$arFilter = array("name" => "sharpen", "precision" => 90);
foreach ($arResult['ITEMS'] as $key => &$arItem) {
if(!empty($arItem['DETAIL_PICTURE'])){
$arItem['PREVIEW_PICTURE'] = CFile::ResizeImageGet(
$arItem['DETAIL_PICTURE'],
$arParams['PREVIEW_PICTURE'],
BX_RESIZE_IMAGE_PROPORTIONAL,
true,
$arFilter
);
$arItem['THUMB'] = CFile::ResizeImageGet(
$arItem['DETAIL_PICTURE'],
$arParams['THUMB'],
BX_RESIZE_IMAGE_EXACT,
true,
$arFilter
);
}
}
/**
* Upadte user awatar
**/
AddEventHandler("main", "OnBeforeUserUpdate", "OnBeforeUserUpdateHandler");
function OnBeforeUserUpdateHandler(&$arFields)
{
if($arFields['PERSONAL_PHOTO']['size']>1024*1024*10)
{
GLOBAL $APPLICATION;
$APPLICATION->throwException('Максимальный размер фотографии 10 мегабайт');
unset($arFields['PERSONAL_PHOTO']);
return false;
}
elseif($arFields['PERSONAL_PHOTO'])
{
$arNewFile = CIBlock::ResizePicture($arFields['PERSONAL_PHOTO'], array(
"WIDTH" => 700,
"HEIGHT" => 700,
"METHOD" => "resample",
));
if($arNewFile)
$arFields['PERSONAL_PHOTO'] = $arNewFile;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment