static function ResizePicture($arFile, $arResize)
{
if($arFile["tmp_name"] == '')
return $arFile;
if(array_key_exists("error", $arFile) && $arFile["error"] !== 0)
return GetMessage("IBLOCK_BAD_FILE_ERROR");
$file = $arFile["tmp_name"];
if(!file_exists($file) && !is_file($file))
return GetMessage("IBLOCK_BAD_FILE_NOT_FOUND");
$width = (int)$arResize["WIDTH"];
$height = (int)$arResize["HEIGHT"];
if($width <= 0 && $height <= 0)
return $arFile;
$image = new Image($file);
$imageInfo = $image->getInfo(false);
if (empty($imageInfo))
{
return GetMessage("IBLOCK_BAD_FILE_NOT_PICTURE");
}
$orig = [
0 => $imageInfo->getWidth(),
1 => $imageInfo->getHeight(),
2 => $imageInfo->getFormat(),
3 => $imageInfo->getAttributes(),
"mime" => $imageInfo->getMime(),
];
$width_orig = $orig[0];
$height_orig = $orig[1];
$orientation = 0;
$exifData = [];
$image_type = $orig[2];
if($image_type == Image::FORMAT_JPEG)
{
$exifData = $image->getExifData();
if (isset($exifData['Orientation']))
{
$orientation = $exifData['Orientation'];
if ($orientation >= 5 && $orientation <= 8)
{
$width_orig = $orig[1];
$height_orig = $orig[0];
}
}
}
if(($width > 0 && $orig[0] > $width) || ($height > 0 && $orig[1] > $height))
{
if($arFile["COPY_FILE"] == "Y")
{
$new_file = CTempFile::GetFileName(basename($file));
CheckDirPath($new_file);
$arFile["copy"] = true;
if(copy($file, $new_file))
$file = $new_file;
else
return GetMessage("IBLOCK_BAD_FILE_NOT_FOUND");
}
if($width <= 0)
$width = $width_orig;
if($height <= 0)
$height = $height_orig;
$height_new = $height_orig;
if($width_orig > $width)
$height_new = $width * $height_orig / $width_orig;
if($height_new > $height)
$width = $height * $width_orig / $height_orig;
else
$height = $height_new;
$image_type = $orig[2];
if ($image_type == Image::FORMAT_JPEG)
{
$image = imagecreatefromjpeg($file);
if ($image === false)
{
ini_set('gd.jpeg_ignore_warning', 1);
$image = imagecreatefromjpeg($file);
}
if ($orientation > 1)
{
if ($orientation == 7 || $orientation == 8)
$image = imagerotate($image, 90, null);
elseif ($orientation == 3 || $orientation == 4)
$image = imagerotate($image, 180, null);
elseif ($orientation == 5 || $orientation == 6)
$image = imagerotate($image, 270, null);
if (
$orientation == 2 || $orientation == 7
|| $orientation == 4 || $orientation == 5
)
{
$engine = new ImageGd();
$engine->setResource($image);
$engine->flipHorizontal();
}
}
}
elseif ($image_type == Image::FORMAT_GIF)
{
$image = imagecreatefromgif($file);
}
elseif ($image_type == Image::FORMAT_PNG)
{
$image = imagecreatefrompng($file);
}
elseif ($image_type == Image::FORMAT_WEBP)
{
$image = imagecreatefromwebp($file);
}
else
{
return GetMessage("IBLOCK_ERR_BAD_FILE_UNSUPPORTED");
}
$image_p = imagecreatetruecolor($width, $height);
if($image_type == Image::FORMAT_JPEG)
{
if($arResize["METHOD"] === "resample")
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
else
imagecopyresized($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
if($arResize["COMPRESSION"] > 0)
imagejpeg($image_p, $file, $arResize["COMPRESSION"]);
else
imagejpeg($image_p, $file);
}
elseif($image_type == Image::FORMAT_GIF && function_exists("imagegif"))
{
imagetruecolortopalette($image_p, true, imagecolorstotal($image));
imagepalettecopy($image_p, $image);
//Save transparency for GIFs
$transparentColor = imagecolortransparent($image);
if($transparentColor >= 0 && $transparentColor < imagecolorstotal($image))
{
$transparentColor = imagecolortransparent($image_p, $transparentColor);
imagefilledrectangle($image_p, 0, 0, $width, $height, $transparentColor);
}
if($arResize["METHOD"] === "resample")
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
else
imagecopyresized($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagegif($image_p, $file);
}
else
{
//Save transparency for PNG
$transparentColor = imagecolorallocatealpha($image_p, 0, 0, 0, 127);
imagefilledrectangle($image_p, 0, 0, $width, $height, $transparentColor);
$transparentColor = imagecolortransparent($image_p, $transparentColor);
imagealphablending($image_p, false);
if($arResize["METHOD"] === "resample")
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
else
imagecopyresized($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagesavealpha($image_p, true);
imagepng($image_p, $file);
}
imagedestroy($image);
imagedestroy($image_p);
$arFile["size"] = filesize($file);
$arFile["tmp_name"] = $file;
return $arFile;
}
else
{
return $arFile;
}
}