Created
January 12, 2014 17:13
-
-
Save marvell/8387555 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* Функция генерации превью изображений с помощью phpthumb 3.x | |
* | |
* @param str|int $src относительный или абсолютный путь к картинке или id файла Битрикса | |
* @param int $width ширина превью (0 - авто) | |
* @param int $heigth высота превью (0 - авто) | |
* @param bool $adaptive метод резайза adaptive/resize | |
* @param bool $clear_cache разрешить пересоздавать превью при обновлении кеша страницы Битрикса ($_GET['clear_cache']) | |
* @param int $lifetime время перегенерации превью, в милисекундах | |
* @return string|bool возвращает путь к превью и false при ошибке | |
*/ | |
if ( ! function_exists('thumbnail')) { | |
function thumbnail($src, $width = 0, $height = 0, $adaptive = TRUE, $clear_cache = TRUE, $lifetime = 2592000) { | |
global $APPLICATION, $USER; | |
if(!file_exists($_SERVER['DOCUMENT_ROOT'].'/bitrix/php_interface/include/phpthumb/ThumbLib.inc.php')) { | |
$APPLICATION->ThrowException('Can`t find phpthumb library.'); | |
return FALSE; | |
} | |
if(!$src) { | |
$APPLICATION->ThrowException('Wrong filename.'); | |
return FALSE; | |
} | |
$width = intval($width); | |
$height = intval($height); | |
if ($USER->IsAdmin() && $_GET['clear_cache']) { | |
$lifetime = 0; | |
} | |
if (is_numeric($src) && $src > 0) { | |
$src = $_SERVER['DOCUMENT_ROOT'].CFile::GetPath($src); | |
} elseif (!strstr($src, $_SERVER['DOCUMENT_ROOT'])) { | |
$src = $_SERVER['DOCUMENT_ROOT'].$src; | |
} | |
$info = pathinfo($src); | |
$thumb = $info['dirname'].'/'.$info['filename'].'_thumb_'.$width.'x'.$height.'.'.$info['extension']; | |
if (!file_exists($thumb) OR (filemtime($thumb) + $lifetime) < time()) { | |
require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/php_interface/include/phpthumb/ThumbLib.inc.php'); | |
@unlink($thumb); | |
$phpthumb = PhpThumbFactory::create($src); | |
if($adaptive) { | |
$phpthumb->adaptiveResize($width, $height); | |
} else { | |
$phpthumb->resize($width, $height); | |
} | |
RewriteFile($thumb, $phpthumb->getImageAsString()); | |
} | |
return substr($thumb, strlen($_SERVER['DOCUMENT_ROOT'])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment