Skip to content

Instantly share code, notes, and snippets.

@vozhukh
Last active December 13, 2016 13:05
Show Gist options
  • Save vozhukh/b7ca9171e59fde20f9d615f1ad767e53 to your computer and use it in GitHub Desktop.
Save vozhukh/b7ca9171e59fde20f9d615f1ad767e53 to your computer and use it in GitHub Desktop.
bitrix. Resize and replace images form b_file , when HEIGHT || WIDTH> 1600. Scripn in js step/
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_before.php'); // include bitrix prolog (clasess)
define("STOP_STATISTICS", true); // disable statistic
CModule::IncludeModule("iblock");
global $DB;
$LastID=0; // start ID
$obRes = Bitrix\Main\FileTable::GetList(array(
'select' => array('ID', 'HEIGHT', 'WIDTH', 'FILE_SIZE','SUBDIR','FILE_NAME'),
'filter' => array('>ID'=>$LastID, 'CONTENT_TYPE'=>'image/jpeg'),
'order' => array('ID'=>'asc'),
'limit' => 5 // filesby step
));
$ptime = getmicrotime();
$io = CBXVirtualIo::GetInstance();
$CountR =0;
while ( $arRes = $obRes->fetch() )
{
// size filter
if($arRes['HEIGHT']>1600 || $arRes['WIDTH']>1600){
//only big
$ToResizeImg = $_SERVER["DOCUMENT_ROOT"].'/upload/'.$arRes['SUBDIR'].'/'.$arRes['FILE_NAME'];
$tempFile = $_SERVER['DOCUMENT_ROOT']."/upload/1.jpeg";
$rif = CFile::ResizeImageFile( // resize image
$sourceFile = $ToResizeImg,
$destinationFile = $tempFile,
$arSize = array('width'=>1600,'height'=>1600),
$resizeType = BX_RESIZE_IMAGE_PROPORTIONAL,
$arWaterMark = array(),
$jpgQuality=false,
$arFilters =false
);
if ($rif) { /// if set resized
$arImageSize = CFile::GetImageSize( $tempFile);
$f = $io->GetFile($tempFile);
$arImageSize[2] = $f->GetFileSize();
unlink($ToResizeImg);
rename($tempFile, $ToResizeImg); // replace image
//undate info in b_file
$DB->Query("UPDATE b_file SET FILE_SIZE='".round(floatval($arImageSize[2]))."', HEIGHT='".round(floatval($arImageSize[1]))."', WIDTH='".round(floatval($arImageSize[0]))."' WHERE ID=".intval($arRes['ID']));
$CountR++;
}
}
$LastID = intval($arRes['ID']);
}
echo "Loop work ".round(getmicrotime()-$ptime, 3)." seconds";
echo '<pre>$LastID';
print_r($LastID);
echo '</pre>';
if($LastID>0){
?>
<script>
setTimeout(function(){
window.location.reload(1);
}, 700);
</script>
<?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment