static function ResizeImageFile($sourceFile, &$destinationFile, $arSize, $resizeType = BX_RESIZE_IMAGE_PROPORTIONAL, $arWaterMark = array(), $jpgQuality = false, $arFilters = false)
{
$io = CBXVirtualIo::GetInstance();
if (!$io->FileExists($sourceFile))
return false;
$bNeedCreatePicture = 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 = CFile::GetImageSize($sourceFile);
if (!in_array($arSourceFileSizeTmp[2], array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_BMP)))
return false;
$orientation = 0;
if ($arSourceFileSizeTmp[2] == IMAGETYPE_JPEG)
{
$exifData = CFile::ExtractImageExif($io->GetPhysicalName($sourceFile));
if ($exifData && isset($exifData['Orientation']))
{
$orientation = $exifData['Orientation'];
//swap width and height
if ($orientation >= 5 && $orientation <= 8)
{
$tmp = $arSourceFileSizeTmp[1];
$arSourceFileSizeTmp[1] = $arSourceFileSizeTmp[0];
$arSourceFileSizeTmp[0] = $tmp;
}
}
}
// imagemagick was be here. I delete them to simplification
if ($io->Copy($sourceFile, $destinationFile))
{
switch ($arSourceFileSizeTmp[2])
{
case IMAGETYPE_GIF:
$sourceImage = imagecreatefromgif($io->GetPhysicalName($sourceFile));
$bHasAlpha = true;
break;
case IMAGETYPE_PNG:
$sourceImage = imagecreatefrompng($io->GetPhysicalName($sourceFile));
$bHasAlpha = true;
break;
case IMAGETYPE_BMP:
$sourceImage = CFile::ImageCreateFromBMP($io->GetPhysicalName($sourceFile));
$bHasAlpha = false;
break;
default:
$sourceImage = imagecreatefromjpeg($io->GetPhysicalName($sourceFile));
if ($sourceImage === false)
{
ini_set('gd.jpeg_ignore_warning', 1);
$sourceImage = imagecreatefromjpeg($io->GetPhysicalName($sourceFile));
}
if ($orientation > 1)
{
$properlyOriented = CFile::ImageHandleOrientation($orientation, $sourceImage);
if ($jpgQuality === false)
$jpgQuality = intval(COption::GetOptionString('main', 'image_resize_quality', '95'));
if ($jpgQuality <= 0 || $jpgQuality > 100)
$jpgQuality = 95;
if ($properlyOriented)
{
imagejpeg($properlyOriented, $io->GetPhysicalName($destinationFile), $jpgQuality);
$sourceImage = $properlyOriented;
}
}
$bHasAlpha = false;
break;
}
$sourceImageWidth = intval(imagesx($sourceImage));
$sourceImageHeight = intval(imagesy($sourceImage));
if ($sourceImageWidth > 0 && $sourceImageHeight > 0)
{
if ($arSize["width"] <= 0 || $arSize["height"] <= 0)
{
$arSize["width"] = $sourceImageWidth;
$arSize["height"] = $sourceImageHeight;
}
self::ScaleImage($sourceImageWidth, $sourceImageHeight, $arSize, $resizeType, $bNeedCreatePicture, $arSourceSize, $arDestinationSize);
if ($bNeedCreatePicture)
{
if (CFile::IsGD2())
{
$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);
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))
{
$RGB = imagecolorsforindex($sourceImage, $transparentcolor);
$transparentcolor = imagecolorallocate($picture, $RGB["red"], $RGB["green"], $RGB["blue"]);
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"]);
}
}
else
{
$picture = $sourceImage;
}
if (is_array($arFilters))
{
foreach ($arFilters as $arFilter)
$bNeedCreatePicture |= CFile::ApplyImageFilter($picture, $arFilter, $bHasAlpha);
}
if (is_array($arWaterMark))
{
$arWaterMark["name"] = "watermark";
$bNeedCreatePicture |= CFile::ApplyImageFilter($picture, $arWaterMark, $bHasAlpha);
}
if ($bNeedCreatePicture)
{
if ($io->FileExists($destinationFile))
$io->Delete($destinationFile);
switch ($arSourceFileSizeTmp[2])
{
case IMAGETYPE_GIF:
imagegif($picture, $io->GetPhysicalName($destinationFile));
break;
case IMAGETYPE_PNG:
imagealphablending($picture, false);
imagesavealpha($picture, true);
imagepng($picture, $io->GetPhysicalName($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, $io->GetPhysicalName($destinationFile), $jpgQuality);
break;
}
imagedestroy($picture);
}
}
return true;
}
return false;
}