37 $server = Application::getInstance()->getContext()->getServer();
38 if (
$host = $server->getHttpHost())
40 $domain = strtok(
$host,
':');
44 $domain = strtok(
$name,
':');
48 $domain = strtok(SITE_SERVER_NAME,
':');
52 $protocol = \CMain::IsHTTPS() ?
"https://" :
"http://";
54 if (mb_substr($src, 0, 1) ==
"/")
56 $strFile =
$protocol . $domain . implode(
"/", array_map(
"rawurlencode", explode(
"/", $src)));
59 elseif (preg_match(
"/^(http|https):\\/\\/(.*?)\\/(.*)\$/", $src, $match))
61 $strFile =
$protocol . $match[2] .
'/' . implode(
"/", array_map(
"rawurlencode", explode(
"/", $match[3])));
74 $sortedPhotos =
array();
92 $photosSizes =
array();
93 foreach($photos as $photo)
98 $sizes = \CFile::GetFileArray($photo);
99 $photosSizes[$sizes[
'HEIGHT'] + $sizes[
'WIDTH']] =
array(
100 'PHOTO_ID' => $photo,
101 'SIZES_SUM' => $sizes[
'HEIGHT'] + $sizes[
'WIDTH']
105 krsort($photosSizes);
110 $biggestPhoto = current($photosSizes);
111 $sortedPhotos[$biggestPhoto[
'PHOTO_ID']] = $biggestPhoto[
'PHOTO_ID'];
112 unset($photosSizes[$biggestPhoto[
'SIZES_SUM']]);
115 return $sortedPhotos;
137 $needMainPhoto =
true;
138 $sizesLimits =
array(
150 $needMainPhoto =
false;
151 $sizesLimits =
array(
160 if (!is_array($photos))
161 $photos =
array($photos =>
array(
"PHOTO_BX_ID" => $photos));
170 foreach ($photos as $photoId => $photo)
172 if ($photoChecked = self::checkPhoto($photoId, $sizesLimits))
175 if (
$i == 1 && $needMainPhoto)
177 $result[
"PHOTO_MAIN_BX_ID"] = $photoChecked[
'ID'];
178 $result[
"PHOTO_MAIN_URL"] = $photoChecked[
'URL'];
186 $result[
"PHOTOS"][$photoChecked[
'ID']][
"PHOTO_BX_ID"] = $photoChecked[
'ID'];
187 $result[
"PHOTOS"][$photoChecked[
'ID']][
"PHOTO_URL"] = $photoChecked[
'URL'];
196 if ($photoChecked[
'RESIZE'])
199 $result[
"RESIZE_TYPE"] = $photoChecked[
'RESIZE'];
215 private static function checkPhoto($photoId, $sizesLimits)
217 $photoParams = \CFile::GetFileArray($photoId);
221 $photoSrc = $photoParams[
"SRC"];
223 $needResize = self::RESIZE_NO;
228 $photoParams[
'HEIGHT'] < $sizesLimits[
'MIN_HEIGHT'] ||
229 $photoParams[
'WIDTH'] < $sizesLimits[
'MIN_WIDTH']
232 $needResize = self::RESIZE_UP;
237 ($photoParams[
'HEIGHT'] + $photoParams[
'WIDTH']) > $sizesLimits[
'MAX_SIZES_SUM'] ||
238 $photoParams[
'FILE_SIZE'] > $sizesLimits[
'MAX_SIZE']
241 $needResize = self::RESIZE_DOWN;
246 (isset($sizesLimits[
'RATIO_V']) && $photoParams[
'WIDTH'] / $photoParams[
'HEIGHT'] <= $sizesLimits[
'RATIO_V']) ||
247 (isset($sizesLimits[
'RATIO_H']) && $photoParams[
'WIDTH'] / $photoParams[
'HEIGHT'] >= $sizesLimits[
'RATIO_H'])
251 if ($needResize == self::RESIZE_UP || $needResize == self::RESIZE_NO)
253 $needResize = self::RESIZE_UP_CROP;
255 elseif ($needResize == self::RESIZE_DOWN)
257 $needResize = self::RESIZE_DOWN_CROP;
266 case self::RESIZE_UP:
267 $multiplier = max($sizesLimits[
'MIN_WIDTH'] / $photoParams[
'WIDTH'], $sizesLimits[
'MIN_HEIGHT'] / $photoParams[
'HEIGHT']);
268 $newWidth = ceil($photoParams[
'WIDTH'] * $multiplier);
269 $newHeight = ceil($photoParams[
'HEIGHT'] * $multiplier);
272 case self::RESIZE_DOWN:
273 $ratio = $sizesLimits[
'MIN_WIDTH'] / $sizesLimits[
'MIN_HEIGHT'];
274 $newHeight = floor($sizesLimits[
'MAX_SIZES_SUM'] / ($ratio + 1));
275 $newWidth = floor($ratio * $newHeight);
278 case self::RESIZE_UP_CROP:
279 if(($sizesLimits[
'MIN_WIDTH'] / $photoParams[
'WIDTH']) < ($sizesLimits[
'MIN_HEIGHT'] / $photoParams[
'HEIGHT']))
281 $ratio = $sizesLimits[
"RATIO_H"];
282 $newHeight = $sizesLimits[
'MIN_HEIGHT'];
283 $newWidth = floor($sizesLimits[
'MIN_HEIGHT'] * $ratio) - 1;
287 $ratio = $sizesLimits[
"RATIO_V"];
288 $newWidth = $sizesLimits[
'MIN_WIDTH'];
289 $newHeight = floor($sizesLimits[
'MIN_WIDTH'] / $ratio) - 1;
294 case self::RESIZE_DOWN_CROP:
295 if(($sizesLimits[
'MIN_WIDTH'] / $photoParams[
'WIDTH']) < ($sizesLimits[
'MIN_HEIGHT'] / $photoParams[
'HEIGHT']))
297 $ratio = $sizesLimits[
"RATIO_H"];
298 $newHeight = floor($sizesLimits[
'MAX_SIZES_SUM'] / ($ratio + 1));
299 $newWidth = floor($ratio * $newHeight) - 1;
303 $ratio = $sizesLimits[
"RATIO_V"];
304 $newHeight = floor($sizesLimits[
'MAX_SIZES_SUM'] / ($ratio + 1));
305 $newWidth = floor($ratio * $newHeight--);
314 $resizeFilters =
false;
315 if ($needResize == self::RESIZE_UP || $needResize == self::RESIZE_UP_CROP)
323 array(
'width' => $newWidth,
'height' => $newHeight),
330 $photoId = \CFile::SaveFile(
331 \CFile::MakeFileArray($resizedPhoto[
'SRC']),
332 "resize_cache/vk_export_resize_img"
334 $resizedFile = \CFile::GetFileArray($photoId);
336 $photoSrc = $resizedFile[
'SRC'];
340 'RESIZE' => $needResize,
361 if (!is_array($file) && intval($file) > 0)
363 $file = \CFile::GetFileArray($file);
366 if (!is_array($file) || !array_key_exists(
"FILE_NAME", $file) || $file[
"FILE_NAME"] ==
'')
372 if (!is_array($arSize))
374 if (!array_key_exists(
"width", $arSize) || intval($arSize[
"width"]) <= 0)
375 $arSize[
"width"] = 0;
376 if (!array_key_exists(
"height", $arSize) || intval($arSize[
"height"]) <= 0)
377 $arSize[
"height"] = 0;
378 $arSize[
"width"] = intval($arSize[
"width"]);
379 $arSize[
"height"] = intval($arSize[
"height"]);
381 $uploadDirName = \COption::GetOptionString(
"main",
"upload_dir",
"upload");
383 $imageFile =
"/" . $uploadDirName .
"/" . $file[
"SUBDIR"] .
"/" . $file[
"FILE_NAME"];
384 $arImageSize =
false;
385 $bFilters = is_array($arFilters) && !empty($arFilters);
388 ($arSize[
"width"] <= 0 )
389 && ($arSize[
"height"] <= 0 )
395 $arSize[
"width"] = $file[
"WIDTH"];
396 $arSize[
"height"] = $file[
"HEIGHT"];
405 "SRC" => $file[
"SRC"],
406 "width" => intval($file[
"WIDTH"]),
407 "height" => intval($file[
"HEIGHT"]),
408 "size" => $file[
"FILE_SIZE"],
414 $cacheImageFile =
"/" . $uploadDirName .
"/resize_cache/" . $file[
"SUBDIR"] .
"/" . $arSize[
"width"] .
"_" . $arSize[
"height"] .
"_" . $resizeType . (is_array($arFilters) ? md5(serialize($arFilters)) :
"") .
"/" . $file[
"FILE_NAME"];
416 $cacheImageFileCheck = $cacheImageFile;
417 if ($file[
"CONTENT_TYPE"] ==
"image/bmp")
418 $cacheImageFileCheck .=
".jpg";
420 static $cache =
array();
421 $cache_id = $cacheImageFileCheck;
422 if (isset($cache[$cache_id]))
424 return $cache[$cache_id];
426 elseif (!file_exists(
$io->GetPhysicalName(
$_SERVER[
"DOCUMENT_ROOT"] . $cacheImageFileCheck)))
430 if (\COption::GetOptionInt(
"main",
"disk_space") > 0)
432 $quota = new \CDiskQuota();
433 $bDiskQuota = $quota->CheckDiskQuota($file);
439 if (!is_array($arFilters))
441 array(
"name" =>
"sharpen",
"precision" => 15),
444 $sourceImageFile =
$_SERVER[
"DOCUMENT_ROOT"] . $imageFile;
445 $cacheImageFileTmp =
$_SERVER[
"DOCUMENT_ROOT"] . $cacheImageFile;
447 $callbackData = NULL;
449 foreach (
GetModuleEvents(
"main",
"OnBeforeResizeImage",
true) as $arEvent)
453 array($arSize, $resizeType,
array(),
false, $arFilters, $bImmediate),
462 if ($bNeedResize && self::ResizeImageFile($sourceImageFile, $cacheImageFileTmp, $arSize, $resizeType,
array(), $jpgQuality, $arFilters))
464 $cacheImageFile = mb_substr($cacheImageFileTmp, mb_strlen(
$_SERVER[
"DOCUMENT_ROOT"]));
467 if (\COption::GetOptionInt(
"main",
"disk_space") > 0)
473 $cacheImageFile = $imageFile;
476 foreach (
GetModuleEvents(
"main",
"OnAfterResizeImage",
true) as $arEvent)
480 array($arSize, $resizeType,
array(),
false, $arFilters),
491 $cacheImageFile = $imageFile;
494 $cacheImageFileCheck = $cacheImageFile;
497 if ($bInitSizes && !is_array($arImageSize))
499 $arImageSize = \CFile::GetImageSize(
$_SERVER[
"DOCUMENT_ROOT"] . $cacheImageFileCheck);
501 $f =
$io->GetFile(
$_SERVER[
"DOCUMENT_ROOT"] . $cacheImageFileCheck);
502 $arImageSize[2] =
$f->GetFileSize();
505 $cache[$cache_id] =
array(
506 "SRC" => $cacheImageFileCheck,
507 "width" => intval($arImageSize[0]),
508 "height" => intval($arImageSize[1]),
509 "size" => $arImageSize[2],
512 return $cache[$cache_id];
527 private static function ResizeImageFile($sourceFile, &$destinationFile, $arSize, $resizeType =
BX_RESIZE_IMAGE_PROPORTIONAL, $arWaterMark =
array(), $jpgQuality =
false, $arFilters =
false)
531 if (!
$io->FileExists($sourceFile))
534 $bNeedCreatePicture =
false;
539 if (!is_array($arSize))
541 if (!array_key_exists(
"width", $arSize) || intval($arSize[
"width"]) <= 0)
542 $arSize[
"width"] = 0;
543 if (!array_key_exists(
"height", $arSize) || intval($arSize[
"height"]) <= 0)
544 $arSize[
"height"] = 0;
545 $arSize[
"width"] = intval($arSize[
"width"]);
546 $arSize[
"height"] = intval($arSize[
"height"]);
548 $arSourceSize =
array(
"x" => 0,
"y" => 0,
"width" => 0,
"height" => 0);
549 $arDestinationSize =
array(
"x" => 0,
"y" => 0,
"width" => 0,
"height" => 0);
551 $arSourceFileSizeTmp = \CFile::GetImageSize($sourceFile);
552 if (!in_array($arSourceFileSizeTmp[2],
array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_BMP)))
556 if ($arSourceFileSizeTmp[2] == IMAGETYPE_JPEG)
558 $exifData = \CFile::ExtractImageExif(
$io->GetPhysicalName($sourceFile));
559 if ($exifData && isset($exifData[
'Orientation']))
561 $orientation = $exifData[
'Orientation'];
563 if ($orientation >= 5 && $orientation <= 8)
565 $tmp = $arSourceFileSizeTmp[1];
566 $arSourceFileSizeTmp[1] = $arSourceFileSizeTmp[0];
567 $arSourceFileSizeTmp[0] =
$tmp;
574 if (
$io->Copy($sourceFile, $destinationFile))
576 switch ($arSourceFileSizeTmp[2])
579 $sourceImage = imagecreatefromgif(
$io->GetPhysicalName($sourceFile));
583 $sourceImage = imagecreatefrompng(
$io->GetPhysicalName($sourceFile));
587 $sourceImage = \CFile::ImageCreateFromBMP(
$io->GetPhysicalName($sourceFile));
591 $sourceImage = imagecreatefromjpeg(
$io->GetPhysicalName($sourceFile));
592 if ($sourceImage ===
false)
594 ini_set(
'gd.jpeg_ignore_warning', 1);
595 $sourceImage = imagecreatefromjpeg(
$io->GetPhysicalName($sourceFile));
598 if ($orientation > 1)
600 $properlyOriented = \CFile::ImageHandleOrientation($orientation, $sourceImage);
602 if ($jpgQuality ===
false)
603 $jpgQuality = intval(\COption::GetOptionString(
'main',
'image_resize_quality',
'95'));
604 if ($jpgQuality <= 0 || $jpgQuality > 100)
607 if ($properlyOriented)
609 imagejpeg($properlyOriented,
$io->GetPhysicalName($destinationFile), $jpgQuality);
610 $sourceImage = $properlyOriented;
617 $sourceImageWidth = intval(imagesx($sourceImage));
618 $sourceImageHeight = intval(imagesy($sourceImage));
620 if ($sourceImageWidth > 0 && $sourceImageHeight > 0)
622 if ($arSize[
"width"] <= 0 || $arSize[
"height"] <= 0)
624 $arSize[
"width"] = $sourceImageWidth;
625 $arSize[
"height"] = $sourceImageHeight;
628 self::ScaleImage($sourceImageWidth, $sourceImageHeight, $arSize, $resizeType, $bNeedCreatePicture, $arSourceSize, $arDestinationSize);
630 if ($bNeedCreatePicture)
634 $picture = imagecreatetruecolor($arDestinationSize[
"width"], $arDestinationSize[
"height"]);
635 if ($arSourceFileSizeTmp[2] == IMAGETYPE_PNG)
637 $transparentcolor = imagecolorallocatealpha($picture, 0, 0, 0, 127);
638 imagefilledrectangle($picture, 0, 0, $arDestinationSize[
"width"], $arDestinationSize[
"height"], $transparentcolor);
640 imagealphablending($picture,
false);
641 imagecopyresampled($picture, $sourceImage,
642 0, 0, $arSourceSize[
"x"], $arSourceSize[
"y"],
643 $arDestinationSize[
"width"], $arDestinationSize[
"height"], $arSourceSize[
"width"], $arSourceSize[
"height"]);
644 imagealphablending($picture,
true);
646 elseif ($arSourceFileSizeTmp[2] == IMAGETYPE_GIF)
648 imagepalettecopy($picture, $sourceImage);
651 $transparentcolor = imagecolortransparent($sourceImage);
652 if ($transparentcolor >= 0 && $transparentcolor < imagecolorstotal($sourceImage))
654 $RGB = imagecolorsforindex($sourceImage, $transparentcolor);
655 $transparentcolor = imagecolorallocate($picture, $RGB[
"red"], $RGB[
"green"], $RGB[
"blue"]);
656 imagecolortransparent($picture, $transparentcolor);
657 imagefilledrectangle($picture, 0, 0, $arDestinationSize[
"width"], $arDestinationSize[
"height"], $transparentcolor);
660 imagecopyresampled($picture, $sourceImage,
661 0, 0, $arSourceSize[
"x"], $arSourceSize[
"y"],
662 $arDestinationSize[
"width"], $arDestinationSize[
"height"], $arSourceSize[
"width"], $arSourceSize[
"height"]);
666 imagecopyresampled($picture, $sourceImage,
667 0, 0, $arSourceSize[
"x"], $arSourceSize[
"y"],
668 $arDestinationSize[
"width"], $arDestinationSize[
"height"], $arSourceSize[
"width"], $arSourceSize[
"height"]);
673 $picture = imagecreate($arDestinationSize[
"width"], $arDestinationSize[
"height"]);
674 imagecopyresized($picture, $sourceImage,
675 0, 0, $arSourceSize[
"x"], $arSourceSize[
"y"],
676 $arDestinationSize[
"width"], $arDestinationSize[
"height"], $arSourceSize[
"width"], $arSourceSize[
"height"]);
681 $picture = $sourceImage;
684 if (is_array($arFilters))
687 $bNeedCreatePicture |= \CFile::ApplyImageFilter($picture,
$arFilter, $bHasAlpha);
690 if (is_array($arWaterMark))
692 $arWaterMark[
"name"] =
"watermark";
693 $bNeedCreatePicture |= \CFile::ApplyImageFilter($picture, $arWaterMark, $bHasAlpha);
696 if ($bNeedCreatePicture)
698 if (
$io->FileExists($destinationFile))
699 $io->Delete($destinationFile);
700 switch ($arSourceFileSizeTmp[2])
703 imagegif($picture,
$io->GetPhysicalName($destinationFile));
706 imagealphablending($picture,
false);
707 imagesavealpha($picture,
true);
708 imagepng($picture,
$io->GetPhysicalName($destinationFile));
711 if ($arSourceFileSizeTmp[2] == IMAGETYPE_BMP)
712 $destinationFile .=
".jpg";
713 if ($jpgQuality ===
false)
714 $jpgQuality = intval(\COption::GetOptionString(
'main',
'image_resize_quality',
'95'));
715 if ($jpgQuality <= 0 || $jpgQuality > 100)
717 imagejpeg($picture,
$io->GetPhysicalName($destinationFile), $jpgQuality);
720 imagedestroy($picture);
741 private static function ScaleImage($sourceImageWidth, $sourceImageHeight, $arSize, $resizeType, &$bNeedCreatePicture, &$arSourceSize, &$arDestinationSize)
743 if (!is_array($arSize))
745 if (!array_key_exists(
"width", $arSize) || intval($arSize[
"width"]) <= 0)
746 $arSize[
"width"] = 0;
747 if (!array_key_exists(
"height", $arSize) || intval($arSize[
"height"]) <= 0)
748 $arSize[
"height"] = 0;
749 $arSize[
"width"] = intval($arSize[
"width"]);
750 $arSize[
"height"] = intval($arSize[
"height"]);
752 $bNeedCreatePicture =
false;
753 $arSourceSize =
array(
"x" => 0,
"y" => 0,
"width" => 0,
"height" => 0);
754 $arDestinationSize =
array(
"x" => 0,
"y" => 0,
"width" => 0,
"height" => 0);
756 if ($sourceImageWidth > 0 && $sourceImageHeight > 0)
758 if ($arSize[
"width"] > 0 && $arSize[
"height"] > 0)
763 $bNeedCreatePicture =
true;
765 $ratio = (($sourceImageWidth / $sourceImageHeight) < ($arSize[
"width"] / $arSize[
"height"])) ?
766 $arSize[
"width"] / $sourceImageWidth : $arSize[
"height"] / $sourceImageHeight;
768 $x = max(0, ceil($sourceImageWidth / 2 - ($arSize[
"width"] / 2) / $ratio));
769 $y = max(0, ceil($sourceImageHeight / 2 - ($arSize[
"height"] / 2) / $ratio));
771 $arDestinationSize[
"width"] = $arSize[
"width"];
772 $arDestinationSize[
"height"] = $arSize[
"height"];
774 $arSourceSize[
"x"] = $x;
775 $arSourceSize[
"y"] = $y;
776 $arSourceSize[
"width"] = ceil($arSize[
"width"] / $ratio);
777 $arSourceSize[
"height"] = ceil($arSize[
"height"] / $ratio);
783 $width = max($sourceImageWidth, $sourceImageHeight);
784 $height = min($sourceImageWidth, $sourceImageHeight);
788 $width = $sourceImageWidth;
789 $height = $sourceImageHeight;
791 $ResizeCoeff[
"width"] = $arSize[
"width"] /
$width;
792 $ResizeCoeff[
"height"] = $arSize[
"height"] / $height;
794 $iResizeCoeff = min($ResizeCoeff[
"width"], $ResizeCoeff[
"height"]);
796 $bNeedCreatePicture = ($iResizeCoeff > 0 ?
true :
false);
798 $arDestinationSize[
"width"] = max(1, intval(ceil($iResizeCoeff * $sourceImageWidth)));
799 $arDestinationSize[
"height"] = max(1, intval(ceil($iResizeCoeff * $sourceImageHeight)));
801 $arSourceSize[
"x"] = 0;
802 $arSourceSize[
"y"] = 0;
803 $arSourceSize[
"width"] = $sourceImageWidth;
804 $arSourceSize[
"height"] = $sourceImageHeight;
810 $arSourceSize =
array(
"x" => 0,
"y" => 0,
"width" => $sourceImageWidth,
"height" => $sourceImageHeight);
811 $arDestinationSize =
array(
"x" => 0,
"y" => 0,
"width" => $sourceImageWidth,
"height" => $sourceImageHeight);