<?
class CMyImageResizer
{
function ResizeImageGet($file, $arSize, $resizeType = BX_RESIZE_IMAGE_PROPORTIONAL, $bInitSizes = false, $arFilters = false, $arWaterMark = array())
{
if (!is_array($file) && IntVal($file) > 0)
{
$dbRes = CFile::GetByID(IntVal($file));
$file = $dbRes->Fetch();
}
if (!is_array($file) || !array_key_exists("FILE_NAME", $file) || StrLen($file["FILE_NAME"]) <= 0)
return false;
if ($resizeType != BX_RESIZE_IMAGE_EXACT && $resizeType != BX_RESIZE_IMAGE_PROPORTIONAL_ALT)
$resizeType != BX_RESIZE_IMAGE_PROPORTIONAL;
if (!is_array($arSize))
$arSize = array();
if (!array_key_exists("width", $arSize) || IntVal($arSize["width"]) <= 0)
$arSize["width"] = 0;
if (!array_key_exists("height", $arSize) || IntVal($arSize["height"]) <= 0)
$arSize["height"] = 0;
$arSize["width"] = IntVal($arSize["width"]);
$arSize["height"] = IntVal($arSize["height"]);
$uploadDirName = COption::GetOptionString("main", "upload_dir", "upload");
$imageFile = "/".$uploadDirName."/".$file["SUBDIR"]."/".$file["FILE_NAME"];
$bNeedCreatePicture = ($arSize['width'] > $file['WIDTH'] && $arSize['height'] > $file['HEIGHT']) ? false : true;
$cacheImageFile = "/".$uploadDirName."/resize_cache/".$file["SUBDIR"]."/".$arSize["width"]."_".$arSize["height"]."_".$resizeType.(is_array($arFilters)? md5(serialize($arFilters)): "").(is_array($arWaterMark)? md5(serialize($arWaterMark)): "")."/".$file["FILE_NAME"];
$cacheImageFileCheck = $cacheImageFile;
if ($file["CONTENT_TYPE"] == "image/bmp")
$cacheImageFileCheck .= ".jpg";
if (!file_exists($_SERVER["DOCUMENT_ROOT"].$cacheImageFileCheck))
{
/****************************** QUOTA ******************************/
$bDiskQuota = true;
if (COption::GetOptionInt("main", "disk_space") > 0)
{
$quota = new CDiskQuota();
$bDiskQuota = $quota->checkDiskQuota($file);
}
/****************************** QUOTA ******************************/
if ($bDiskQuota)
{
if(!is_array($arFilters))
$arFilters = array(
array("name" => "sharpen", "precision" => 15),
);
$cacheImageFileTmp = $_SERVER["DOCUMENT_ROOT"].$cacheImageFile;
if (CMyImageResizer::ResizeImageFile($_SERVER["DOCUMENT_ROOT"].$imageFile, $cacheImageFileTmp, $arSize, $resizeType, $arWaterMark, false, $arFilters))
{
$cacheImageFile = SubStr($cacheImageFileTmp, StrLen($_SERVER["DOCUMENT_ROOT"]));
/****************************** QUOTA ******************************/
if (COption::GetOptionInt("main", "disk_space") > 0)
CDiskQuota::updateDiskQuota("file", filesize($cacheImageFileTmp), "insert");
/****************************** QUOTA ******************************/
}
else
{
$cacheImageFile = $imageFile;
}
}
else
{
$cacheImageFile = $imageFile;
}
$cacheImageFileCheck = $cacheImageFile;
}
$arImageSize = array(0, 0);
if ($bInitSizes)
$arImageSize = getimagesize($_SERVER["DOCUMENT_ROOT"].$cacheImageFileCheck);
return array("src" => $cacheImageFileCheck, "width" => IntVal($arImageSize[0]), "height" => IntVal($arImageSize[1]));
}
function ResizeImageFile($sourceFile, &$destinationFile, $arSize, $resizeType = BX_RESIZE_IMAGE_PROPORTIONAL, $arWaterMark = array(), $jpgQuality=false, $arFilters=false)
{
static $bGD2 = false;
static $bGD2Initial = false;
if (!$bGD2Initial && function_exists("gd_info"))
{
$arGDInfo = gd_info();
$bGD2 = ((StrPos($arGDInfo['GD Version'], "2.") !== false) ? true : false);
$bGD2Initial = true;
}
$imageInput = false;
$bNeedCreatePicture = false;
$picture = false;
if ($resizeType != BX_RESIZE_IMAGE_EXACT && $resizeType != BX_RESIZE_IMAGE_PROPORTIONAL_ALT)
$resizeType = BX_RESIZE_IMAGE_PROPORTIONAL;
if (!is_array($arSize))
$arSize = array();
if (!array_key_exists("width", $arSize) || IntVal($arSize["width"]) <= 0)
$arSize["width"] = 0;
if (!array_key_exists("height", $arSize) || IntVal($arSize["height"]) <= 0)
$arSize["height"] = 0;
$arSize["width"] = IntVal($arSize["width"]);
$arSize["height"] = IntVal($arSize["height"]);
$arSourceSize = array("x" => 0, "y" => 0, "width" => 0, "height" => 0);
$arDestinationSize = array("x" => 0, "y" => 0, "width" => 0, "height" => 0);
$arSourceFileSizeTmp = getimagesize($sourceFile);
if (!in_array($arSourceFileSizeTmp[2], array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_BMP)))
return false;
if (!file_exists($sourceFile) || !is_file($sourceFile))
return false;
if (CopyDirFiles($sourceFile, $destinationFile))
{
if (!is_array($arWaterMark))
$arWaterMark = array();
$sourceImage = false;
switch ($arSourceFileSizeTmp[2])
{
case IMAGETYPE_GIF:
$sourceImage = imagecreatefromgif($sourceFile);
break;
case IMAGETYPE_PNG:
$sourceImage = imagecreatefrompng($sourceFile);
break;
case IMAGETYPE_BMP:
$sourceImage = CFile::ImageCreateFromBMP($sourceFile);
break;
default:
$sourceImage = imagecreatefromjpeg($sourceFile);
break;
}
$sourceImageWidth = IntVal(imagesx($sourceImage));
$sourceImageHeight = IntVal(imagesy($sourceImage));
if ($sourceImageWidth > 0 && $sourceImageHeight > 0)
{
if ($arSize["width"] > 0 && $arSize["height"] > 0)
{
switch ($resizeType)
{
case BX_RESIZE_IMAGE_EXACT:
$bNeedCreatePicture = true;
$width = Max($sourceImageWidth, $sourceImageHeight);
$height = Min($sourceImageWidth, $sourceImageHeight);
$iResizeCoeff = Max($arSize["width"] / $width, $arSize["height"] / $height);
$arDestinationSize["width"] = IntVal($arSize["width"]);
$arDestinationSize["height"] = IntVal($arSize["height"]);
if ($iResizeCoeff > 0)
{
$arSourceSize["x"] = ((($sourceImageWidth * $iResizeCoeff - $arSize["width"]) / 2) / $iResizeCoeff);
$arSourceSize["y"] = ((($sourceImageHeight * $iResizeCoeff - $arSize["height"]) / 2) / $iResizeCoeff);
$arSourceSize["width"] = $arSize["width"] / $iResizeCoeff;
$arSourceSize["height"] = $arSize["height"] / $iResizeCoeff;
}
break;
default:
if ($resizeType == BX_RESIZE_IMAGE_PROPORTIONAL_ALT)
{
$width = Max($sourceImageWidth, $sourceImageHeight);
$height = Min($sourceImageWidth, $sourceImageHeight);
}
else
{
$width = $sourceImageWidth;
$height = $sourceImageHeight;
}
$ResizeCoeff["width"] = $arSize["width"] / $width;
$ResizeCoeff["height"] = $arSize["height"] / $height;
$iResizeCoeff = Min($ResizeCoeff["width"], $ResizeCoeff["height"]);
$iResizeCoeff = ((0 < $iResizeCoeff) && ($iResizeCoeff < 1) ? $iResizeCoeff : 1);
$bNeedCreatePicture = ($iResizeCoeff != 1 ? true : false);
$arDestinationSize["width"] = intVal($iResizeCoeff * $sourceImageWidth);
$arDestinationSize["height"] = intVal($iResizeCoeff * $sourceImageHeight);
$arSourceSize["x"] = 0;
$arSourceSize["y"] = 0;
$arSourceSize["width"] = $sourceImageWidth;
$arSourceSize["height"] = $sourceImageHeight;
break;
}
}
else
{
$arSourceSize = array("x" => 0, "y" => 0, "width" => $sourceImageWidth, "height" => $sourceImageHeight);
$arDestinationSize = array("x" => 0, "y" => 0, "width" => $sourceImageWidth, "height" => $sourceImageHeight);
$arSize["width"] = $sourceImageWidth;
$arSize["height"] = $sourceImageHeight;
}
$bNeedCreatePicture = (!empty($arWaterMark['path_to_watermark']) ? true : $bNeedCreatePicture);
if ($bNeedCreatePicture)
{
if ($bGD2)
{
$picture = ImageCreateTrueColor($arDestinationSize["width"], $arDestinationSize["height"]);
if($arSourceFileSizeTmp[2] == IMAGETYPE_PNG)
{
$transparentcolor = imagecolorallocatealpha($picture, 0, 0, 0, 127);
imagefilledrectangle($picture, 0, 0, $arDestinationSize["width"], $arDestinationSize["height"], $transparentcolor);
$transparentcolor = imagecolortransparent($picture, $transparentcolor);
imagealphablending($picture, false);
imagecopyresampled($picture, $sourceImage,
0, 0, $arSourceSize["x"], $arSourceSize["y"],
$arDestinationSize["width"], $arDestinationSize["height"], $arSourceSize["width"], $arSourceSize["height"]);
imagealphablending($picture, true);
}
elseif($arSourceFileSizeTmp[2] == IMAGETYPE_GIF)
{
imagepalettecopy($picture, $sourceImage);
//Save transparency for GIFs
$transparentcolor = imagecolortransparent($sourceImage);
if($transparentcolor >= 0 && $transparentcolor < imagecolorstotal($sourceImage))
{
$transparentcolor = imagecolortransparent($picture, $transparentcolor);
imagefilledrectangle($picture, 0, 0, $arDestinationSize["width"], $arDestinationSize["height"], $transparentcolor);
}
imagecopyresampled($picture, $sourceImage,
0, 0, $arSourceSize["x"], $arSourceSize["y"],
$arDestinationSize["width"], $arDestinationSize["height"], $arSourceSize["width"], $arSourceSize["height"]);
}
else
{
imagecopyresampled($picture, $sourceImage,
0, 0, $arSourceSize["x"], $arSourceSize["y"],
$arDestinationSize["width"], $arDestinationSize["height"], $arSourceSize["width"], $arSourceSize["height"]);
}
}
else
{
$picture = ImageCreate($arDestinationSize["width"], $arDestinationSize["height"]);
imagecopyresized($picture, $sourceImage,
0, 0, $arSourceSize["x"], $arSourceSize["y"],
$arDestinationSize["width"], $arDestinationSize["height"], $arSourceSize["width"], $arSourceSize["height"]);
}
}
if(is_array($arFilters))
{
foreach($arFilters as $arFilter)
CFile::ApplyImageFilter($picture, $arFilter);
}
if (!empty($arWaterMark['path_to_watermark']) && file_exists($_SERVER['DOCUMENT_ROOT'].$arWaterMark['path_to_watermark'])) {
$pngWaterMarkImg = @imagecreatefrompng($_SERVER['DOCUMENT_ROOT'].$arWaterMark['path_to_watermark']);
$arWaterMarkImgSize['x'] = imagesx($pngWaterMarkImg);
$arWaterMarkImgSize['y'] = imagesy($pngWaterMarkImg);
$arWaterMarkDestPos['x'] = intval($arDestinationSize['width'] - $arWaterMarkImgSize['x'] - intval($arWaterMark['margin_x']));
$arWaterMarkDestPos['y'] = intval($arDestinationSize['height'] - $arWaterMarkImgSize['y'] - intval($arWaterMark['margin_y']));
imagecopyresampled($picture, $pngWaterMarkImg, $arWaterMarkDestPos['x'], $arWaterMarkDestPos['y'], 0, 0, $arWaterMarkImgSize['x'], $arWaterMarkImgSize['y'], $arWaterMarkImgSize['x'], $arWaterMarkImgSize['y']);
imagedestroy($pngWaterMarkImg);
}
if ($bNeedCreatePicture)
{
if(file_exists($destinationFile))
unlink($destinationFile);
switch ($arSourceFileSizeTmp[2])
{
case IMAGETYPE_GIF:
imagegif($picture, $destinationFile);
break;
case IMAGETYPE_PNG:
imagealphablending($picture, false );
imagesavealpha($picture, true);
imagepng($picture, $destinationFile);
break;
default:
if ($arSourceFileSizeTmp[2] == IMAGETYPE_BMP)
$destinationFile .= ".jpg";
if($jpgQuality === false)
$jpgQuality = intval(COption::GetOptionString('main', 'image_resize_quality', '95'));
if($jpgQuality <= 0 || $jpgQuality > 100)
$jpgQuality = 95;
imagejpeg($picture, $destinationFile, $jpgQuality);
break;
}
imagedestroy($picture);
}
}
return true;
}
return false;
}
}
|