Skip to content

Instantly share code, notes, and snippets.

@victorlapshev
Created October 24, 2017 10:10
Show Gist options
  • Select an option

  • Save victorlapshev/c7971da14f24641cee33f348a9763225 to your computer and use it in GitHub Desktop.

Select an option

Save victorlapshev/c7971da14f24641cee33f348a9763225 to your computer and use it in GitHub Desktop.
<?php
use Bitrix\Main\IO\FileNotFoundException;
use Gregwar\Image\Image;
class BaseComponent extends CBitrixComponent
{
const RESIZE_CACHE = '/upload/resize_cache';
const ORDER_BY_DATE = 'date';
const ORDER_BY_NAME = 'name';
const DEFAULT_ORDER_BY = self::ORDER_BY_DATE;
const ORDERS = [
self::ORDER_BY_NAME => [
'name' => 'По алфавиту',
'order' => ['NAME' => 'asc', 'ID' => 'desc']
],
self::ORDER_BY_DATE => [
'name' => 'По дате выпуска',
'order' => ['DATE_ACTIVE_FROM' => 'desc', 'ID' => 'desc']
],
];
public function getOrder() {
$order = static::DEFAULT_ORDER_BY;
if(!empty($_SESSION['ORDER_BY'][get_called_class()]) && array_key_exists($_SESSION['MOVIES']['ORDER_BY'], static::ORDERS)) {
$order = $_SESSION['MOVIES']['ORDER_BY'];
}
if($this->request->get('order') && array_key_exists($this->request->get('order'), static::ORDERS)) {
$order = $_SESSION['ORDER_BY'][get_called_class()] = $this->request->get('order');
}
return $order;
}
protected function getImages() {
if(!count($this->arResult['IMAGES'])) {
return;
}
$dbRes = \CFile::GetList([], ['@ID' => array_keys($this->arResult['IMAGES'])]);
while($arRes = $dbRes->Fetch()) {
$this->arResult['IMAGES'][ $arRes['ID'] ] = '/upload/' . $arRes['SUBDIR'] . '/' . $arRes['FILE_NAME'];
}
}
/**
* Resize, save to cache and return cached picture path
*
* @link https://github.com/Gregwar/Image
*
* @param int $imageID Image id, must present in $arResult[IMAGES]
* @param int $width
* @param int $height
* @param array $params
*
* Possible params
* -negate
*
* @return string
*/
public function resize($imageID, $width, $height, $params = []) {
if(empty($this->arResult['IMAGES'][$imageID])) {
$this->arResult['IMAGES'][$imageID] = \CFile::GetPath($imageID);
}
$path = $_SERVER['DOCUMENT_ROOT'] . '/' . $this->arResult['IMAGES'][$imageID];
$image = Image::open($path);
$image->setCacheDir(self::RESIZE_CACHE);
$image->setActualCacheDir($_SERVER['DOCUMENT_ROOT'] . self::RESIZE_CACHE);
$image->resize($width, $height, 'transparent', true, false, false);
if(!empty($params['negate'])) {
$image->negate();
}
return $image->png();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment