Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
photoresizer.php
1<?php
2
4
8
9Loc::loadMessages(__FILE__);
10
18{
19 const RESIZE_NO = 0;
20 const RESIZE_UP = 10;
21 const RESIZE_UP_CROP = 20;
22 const RESIZE_DOWN = 30;
23 const RESIZE_DOWN_CROP = 40;
24
32 protected static function buildPictureUrl($src, $domain = '')
33 {
34 if ($domain == '')
35 {
36// get different variants of domain URL, because in diff site some variants not work
37 $server = Application::getInstance()->getContext()->getServer();
38 if ($host = $server->getHttpHost())
39 {
40 $domain = strtok($host, ':');
41 }
42 elseif ($name = $server->getServerName())
43 {
44 $domain = strtok($name, ':');
45 }
46 else
47 {
48 $domain = strtok(SITE_SERVER_NAME, ':');
49 }
50 }
51
52 $protocol = \CMain::IsHTTPS() ? "https://" : "http://";
53 // relative path by '/'
54 if (mb_substr($src, 0, 1) == "/")
55 {
56 $strFile = $protocol . $domain . implode("/", array_map("rawurlencode", explode("/", $src)));
57 }
58 // full path
59 elseif (preg_match("/^(http|https):\\/\\/(.*?)\\/(.*)\$/", $src, $match))
60 {
61 $strFile = $protocol . $match[2] . '/' . implode("/", array_map("rawurlencode", explode("/", $match[3])));
62 }
63 // default
64 else
65 {
66 $strFile = $src;
67 }
68
69 return $strFile;
70 }
71
72 public static function sortPhotoArray($photos, $type)
73 {
74 $sortedPhotos = array();
75
76 switch ($type)
77 {
78 case 'PRODUCT':
80// todo: for photos we must ALWAYS set VK-field in first places
81 break;
82
83 case 'ALBUM':
85 break;
86
87 default:
88 return $photos;
89 }
90
91// match sizes of photos. Key of array - sum of width and height
92 $photosSizes = array();
93 foreach($photos as $photo)
94 {
95// photo may be not set
96 if($photo)
97 {
98 $sizes = \CFile::GetFileArray($photo);
99 $photosSizes[$sizes['HEIGHT'] + $sizes['WIDTH']] = array(
100 'PHOTO_ID' => $photo,
101 'SIZES_SUM' => $sizes['HEIGHT'] + $sizes['WIDTH']
102 );
103 }
104 }
105 krsort($photosSizes);
106
107// get elements from sorted sizes until not catch count
108 while(count($sortedPhotos) < $count)
109 {
110 $biggestPhoto = current($photosSizes);
111 $sortedPhotos[$biggestPhoto['PHOTO_ID']] = $biggestPhoto['PHOTO_ID'];
112 unset($photosSizes[$biggestPhoto['SIZES_SUM']]);
113 }
114
115 return $sortedPhotos;
116 }
117
125 public static function checkPhotos($photos, $type)
126 {
127// check empty photos
128 if(!$photos)
129 return NULL;
130
131 $result = array();
132
133 switch ($type)
134 {
135 case 'PRODUCT':
137 $needMainPhoto = true;
138 $sizesLimits = array(
139 'MIN_WIDTH' => Vk::MIN_PRODUCT_PHOTO_WIDTH,
140 'MIN_HEIGHT' => Vk::MIN_PRODUCT_PHOTO_HEIGHT,
141 'MAX_SIZES_SUM' => Vk::MAX_PRODUCT_PHOTO_SIZES_SUM,
142 'MAX_SIZE' => Vk::MAX_PRODUCT_PHOTO_SIZE,
143 'RATIO_V' => Vk::MAX_PRODUCT_RATIO_V, // width / height
144 'RATIO_H' => Vk::MAX_PRODUCT_RATIO_H,
145 );
146 break;
147
148 case 'ALBUM':
150 $needMainPhoto = false;
151 $sizesLimits = array(
152 'MIN_WIDTH' => Vk::MIN_ALBUM_PHOTO_WIDTH,
153 'MIN_HEIGHT' => Vk::MIN_ALBUM_PHOTO_HEIGHT,
154 'MAX_SIZES_SUM' => Vk::MAX_ALBUM_PHOTO_SIZES_SUM,
155 'MAX_SIZE' => Vk::MAX_ALBUM_PHOTO_SIZE,
156 'RATIO_V' => Vk::MAX_ALBUM_RATIO_V, // width / height
157 'RATIO_H' => Vk::MAX_ALBUM_RATIO_H,
158 );
159// CONVERT photo-id format if needed
160 if (!is_array($photos))
161 $photos = array($photos => array("PHOTO_BX_ID" => $photos));
162 break;
163
164 default:
165 return $photos;
166 }
167
168// PROCESSED
169 $i = 1;
170 foreach ($photos as $photoId => $photo)
171 {
172 if ($photoChecked = self::checkPhoto($photoId, $sizesLimits))
173 {
174// MAIN photo is first
175 if ($i == 1 && $needMainPhoto)
176 {
177 $result["PHOTO_MAIN_BX_ID"] = $photoChecked['ID'];
178 $result["PHOTO_MAIN_URL"] = $photoChecked['URL'];
179 $count++; //increase limit for other photos
180 $i++;
181 }
182
183// other PHOTOS
184 elseif ($i++ <= $count)
185 {
186 $result["PHOTOS"][$photoChecked['ID']]["PHOTO_BX_ID"] = $photoChecked['ID'];
187 $result["PHOTOS"][$photoChecked['ID']]["PHOTO_URL"] = $photoChecked['URL'];
188 }
189
190 else
191 {
192 break;
193 }
194
195// set flag if image was be resized
196 if ($photoChecked['RESIZE'])
197 {
198 $result["RESIZE"] = true;
199 $result["RESIZE_TYPE"] = $photoChecked['RESIZE'];
200 }
201 }
202 }
203
204 return $result;
205 }
206
215 private static function checkPhoto($photoId, $sizesLimits)
216 {
217 $photoParams = \CFile::GetFileArray($photoId);
218// check bad files
219 if(!$photoParams)
220 return false;
221 $photoSrc = $photoParams["SRC"];
222 $photoUrl = self::buildPictureUrl($photoParams["SRC"]);
223 $needResize = self::RESIZE_NO;
224 $resizeType = BX_RESIZE_IMAGE_PROPORTIONAL; // default not crop
225
226// need 'RESIZE_UP';
227 if (
228 $photoParams['HEIGHT'] < $sizesLimits['MIN_HEIGHT'] ||
229 $photoParams['WIDTH'] < $sizesLimits['MIN_WIDTH']
230 )
231 {
232 $needResize = self::RESIZE_UP;
233 }
234
235// need 'RESIZE_DOWN';
236 if (
237 ($photoParams['HEIGHT'] + $photoParams['WIDTH']) > $sizesLimits['MAX_SIZES_SUM'] ||
238 $photoParams['FILE_SIZE'] > $sizesLimits['MAX_SIZE']
239 )
240 {
241 $needResize = self::RESIZE_DOWN;
242 }
243
244// for big RATIO - need a crop. If need resize - use $needResize flag, else - always resize down
245 if (
246 (isset($sizesLimits['RATIO_V']) && $photoParams['WIDTH'] / $photoParams['HEIGHT'] <= $sizesLimits['RATIO_V']) ||
247 (isset($sizesLimits['RATIO_H']) && $photoParams['WIDTH'] / $photoParams['HEIGHT'] >= $sizesLimits['RATIO_H'])
248 )
249 {
250 // UP, but we reduce image (need for a get correct sizes)
251 if ($needResize == self::RESIZE_UP || $needResize == self::RESIZE_NO)
252 {
253 $needResize = self::RESIZE_UP_CROP;
254 }
255 elseif ($needResize == self::RESIZE_DOWN)
256 {
257 $needResize = self::RESIZE_DOWN_CROP;
258 }
259 }
260
261// calculate new sizes
262 if ($needResize)
263 {
264 switch ($needResize)
265 {
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);
270 break;
271
273 $ratio = $sizesLimits['MIN_WIDTH'] / $sizesLimits['MIN_HEIGHT'];
274 $newHeight = floor($sizesLimits['MAX_SIZES_SUM'] / ($ratio + 1));
275 $newWidth = floor($ratio * $newHeight);
276 break;
277
279 if(($sizesLimits['MIN_WIDTH'] / $photoParams['WIDTH']) < ($sizesLimits['MIN_HEIGHT'] / $photoParams['HEIGHT']))
280 {
281 $ratio = $sizesLimits["RATIO_H"];
282 $newHeight = $sizesLimits['MIN_HEIGHT'];
283 $newWidth = floor($sizesLimits['MIN_HEIGHT'] * $ratio) - 1; // minus one for preserve overratio.
284 }
285 else
286 {
287 $ratio = $sizesLimits["RATIO_V"];
288 $newWidth = $sizesLimits['MIN_WIDTH'];
289 $newHeight = floor($sizesLimits['MIN_WIDTH'] / $ratio) - 1; // minus one for preserve overratio.
290 }
291 $resizeType = BX_RESIZE_IMAGE_EXACT; // resize with crop
292 break;
293
295 if(($sizesLimits['MIN_WIDTH'] / $photoParams['WIDTH']) < ($sizesLimits['MIN_HEIGHT'] / $photoParams['HEIGHT']))
296 {
297 $ratio = $sizesLimits["RATIO_H"];
298 $newHeight = floor($sizesLimits['MAX_SIZES_SUM'] / ($ratio + 1));
299 $newWidth = floor($ratio * $newHeight) - 1; // need -1, because sizes must be <= ratio
300 }
301 else
302 {
303 $ratio = $sizesLimits["RATIO_V"];
304 $newHeight = floor($sizesLimits['MAX_SIZES_SUM'] / ($ratio + 1));
305 $newWidth = floor($ratio * $newHeight--); // need -1, because sizes must be <= ratio
306 }
307 $resizeType = BX_RESIZE_IMAGE_EXACT; // resize with crop
308 break;
309
310 default:
311 return false;
312 }
313
314 $resizeFilters = false;
315 if ($needResize == self::RESIZE_UP || $needResize == self::RESIZE_UP_CROP)
316 {
317// set empty array for UP resizing - PNG may be failed by timeout
318 $resizeFilters = [];
319 }
320
321 $resizedPhoto = self::ResizeImageGet(
322 $photoId,
323 array('width' => $newWidth, 'height' => $newHeight),
324 $resizeType,
325 true,
326 $resizeFilters
327 );
328
329// need save new photo
330 $photoId = \CFile::SaveFile(
331 \CFile::MakeFileArray($resizedPhoto['SRC']),
332 "resize_cache/vk_export_resize_img"
333 );
334 $resizedFile = \CFile::GetFileArray($photoId);
335 $photoUrl = self::buildPictureUrl($resizedFile['SRC']);
336 $photoSrc = $resizedFile['SRC'];
337 }
338
339 return array(
340 'RESIZE' => $needResize,
341 'SRC' => $photoSrc,
342 'URL' => $photoUrl,
343 'ID' => $photoId,
344 );
345 }
346
359 public static function ResizeImageGet($file, $arSize, $resizeType = BX_RESIZE_IMAGE_PROPORTIONAL, $bInitSizes = false, $arFilters = false, $bImmediate = false, $jpgQuality = false)
360 {
361 if (!is_array($file) && intval($file) > 0)
362 {
363 $file = \CFile::GetFileArray($file);
364 }
365
366 if (!is_array($file) || !array_key_exists("FILE_NAME", $file) || $file["FILE_NAME"] == '')
367 return false;
368
369 if ($resizeType !== BX_RESIZE_IMAGE_EXACT && $resizeType !== BX_RESIZE_IMAGE_PROPORTIONAL_ALT)
370 $resizeType = BX_RESIZE_IMAGE_PROPORTIONAL;
371
372 if (!is_array($arSize))
373 $arSize = array();
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"]);
380
381 $uploadDirName = \COption::GetOptionString("main", "upload_dir", "upload");
382
383 $imageFile = "/" . $uploadDirName . "/" . $file["SUBDIR"] . "/" . $file["FILE_NAME"];
384 $arImageSize = false;
385 $bFilters = is_array($arFilters) && !empty($arFilters);
386
387 if (
388 ($arSize["width"] <= 0 /*|| $arSize["width"] >= $file["WIDTH"]*/)
389 && ($arSize["height"] <= 0 /*|| $arSize["height"] >= $file["HEIGHT"]*/)
390 )
391 {
392 if ($bFilters)
393 {
394 //Only filters. Leave size unchanged
395 $arSize["width"] = $file["WIDTH"];
396 $arSize["height"] = $file["HEIGHT"];
397 $resizeType = BX_RESIZE_IMAGE_PROPORTIONAL;
398 }
399 else
400 {
401 global $arCloudImageSizeCache;
402 $arCloudImageSizeCache[$file["SRC"]] = array($file["WIDTH"], $file["HEIGHT"]);
403
404 return array(
405 "SRC" => $file["SRC"],
406 "width" => intval($file["WIDTH"]),
407 "height" => intval($file["HEIGHT"]),
408 "size" => $file["FILE_SIZE"],
409 );
410 }
411 }
412
413 $io = \CBXVirtualIo::GetInstance();
414 $cacheImageFile = "/" . $uploadDirName . "/resize_cache/" . $file["SUBDIR"] . "/" . $arSize["width"] . "_" . $arSize["height"] . "_" . $resizeType . (is_array($arFilters) ? md5(serialize($arFilters)) : "") . "/" . $file["FILE_NAME"];
415
416 $cacheImageFileCheck = $cacheImageFile;
417 if ($file["CONTENT_TYPE"] == "image/bmp")
418 $cacheImageFileCheck .= ".jpg";
419
420 static $cache = array();
421 $cache_id = $cacheImageFileCheck;
422 if (isset($cache[$cache_id]))
423 {
424 return $cache[$cache_id];
425 }
426 elseif (!file_exists($io->GetPhysicalName($_SERVER["DOCUMENT_ROOT"] . $cacheImageFileCheck)))
427 {
428 /****************************** QUOTA ******************************/
429 $bDiskQuota = true;
430 if (\COption::GetOptionInt("main", "disk_space") > 0)
431 {
432 $quota = new \CDiskQuota();
433 $bDiskQuota = $quota->CheckDiskQuota($file);
434 }
435 /****************************** QUOTA ******************************/
436
437 if ($bDiskQuota)
438 {
439 if (!is_array($arFilters))
440 $arFilters = array(
441 array("name" => "sharpen", "precision" => 15),
442 );
443
444 $sourceImageFile = $_SERVER["DOCUMENT_ROOT"] . $imageFile;
445 $cacheImageFileTmp = $_SERVER["DOCUMENT_ROOT"] . $cacheImageFile;
446 $bNeedResize = true;
447 $callbackData = NULL;
448
449 foreach (GetModuleEvents("main", "OnBeforeResizeImage", true) as $arEvent)
450 {
451 if (ExecuteModuleEventEx($arEvent, array(
452 $file,
453 array($arSize, $resizeType, array(), false, $arFilters, $bImmediate),
454 &$callbackData,
455 &$bNeedResize,
456 &$sourceImageFile,
457 &$cacheImageFileTmp,
458 )))
459 break;
460 }
461
462 if ($bNeedResize && self::ResizeImageFile($sourceImageFile, $cacheImageFileTmp, $arSize, $resizeType, array(), $jpgQuality, $arFilters))
463 {
464 $cacheImageFile = mb_substr($cacheImageFileTmp, mb_strlen($_SERVER["DOCUMENT_ROOT"]));
465
466 /****************************** QUOTA ******************************/
467 if (\COption::GetOptionInt("main", "disk_space") > 0)
468 \CDiskQuota::UpdateDiskQuota("file", filesize($io->GetPhysicalName($cacheImageFileTmp)), "insert");
469 /****************************** QUOTA ******************************/
470 }
471 else
472 {
473 $cacheImageFile = $imageFile;
474 }
475
476 foreach (GetModuleEvents("main", "OnAfterResizeImage", true) as $arEvent)
477 {
478 if (ExecuteModuleEventEx($arEvent, array(
479 $file,
480 array($arSize, $resizeType, array(), false, $arFilters),
481 &$callbackData,
482 &$cacheImageFile,
483 &$cacheImageFileTmp,
484 &$arImageSize,
485 )))
486 break;
487 }
488 }
489 else
490 {
491 $cacheImageFile = $imageFile;
492 }
493
494 $cacheImageFileCheck = $cacheImageFile;
495 }
496
497 if ($bInitSizes && !is_array($arImageSize))
498 {
499 $arImageSize = \CFile::GetImageSize($_SERVER["DOCUMENT_ROOT"] . $cacheImageFileCheck);
500
501 $f = $io->GetFile($_SERVER["DOCUMENT_ROOT"] . $cacheImageFileCheck);
502 $arImageSize[2] = $f->GetFileSize();
503 }
504
505 $cache[$cache_id] = array(
506 "SRC" => $cacheImageFileCheck,
507 "width" => intval($arImageSize[0]),
508 "height" => intval($arImageSize[1]),
509 "size" => $arImageSize[2],
510 );
511
512 return $cache[$cache_id];
513 }
514
527 private static function ResizeImageFile($sourceFile, &$destinationFile, $arSize, $resizeType = BX_RESIZE_IMAGE_PROPORTIONAL, $arWaterMark = array(), $jpgQuality = false, $arFilters = false)
528 {
529 $io = \CBXVirtualIo::GetInstance();
530
531 if (!$io->FileExists($sourceFile))
532 return false;
533
534 $bNeedCreatePicture = false;
535
536 if ($resizeType !== BX_RESIZE_IMAGE_EXACT && $resizeType !== BX_RESIZE_IMAGE_PROPORTIONAL_ALT)
537 $resizeType = BX_RESIZE_IMAGE_PROPORTIONAL;
538
539 if (!is_array($arSize))
540 $arSize = array();
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"]);
547
548 $arSourceSize = array("x" => 0, "y" => 0, "width" => 0, "height" => 0);
549 $arDestinationSize = array("x" => 0, "y" => 0, "width" => 0, "height" => 0);
550
551 $arSourceFileSizeTmp = \CFile::GetImageSize($sourceFile);
552 if (!in_array($arSourceFileSizeTmp[2], array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_BMP)))
553 return false;
554
555 $orientation = 0;
556 if ($arSourceFileSizeTmp[2] == IMAGETYPE_JPEG)
557 {
558 $exifData = \CFile::ExtractImageExif($io->GetPhysicalName($sourceFile));
559 if ($exifData && isset($exifData['Orientation']))
560 {
561 $orientation = $exifData['Orientation'];
562 //swap width and height
563 if ($orientation >= 5 && $orientation <= 8)
564 {
565 $tmp = $arSourceFileSizeTmp[1];
566 $arSourceFileSizeTmp[1] = $arSourceFileSizeTmp[0];
567 $arSourceFileSizeTmp[0] = $tmp;
568 }
569 }
570 }
571
572// imagemagick was be here. I delete them to simplification
573
574 if ($io->Copy($sourceFile, $destinationFile))
575 {
576 switch ($arSourceFileSizeTmp[2])
577 {
578 case IMAGETYPE_GIF:
579 $sourceImage = imagecreatefromgif($io->GetPhysicalName($sourceFile));
580 $bHasAlpha = true;
581 break;
582 case IMAGETYPE_PNG:
583 $sourceImage = imagecreatefrompng($io->GetPhysicalName($sourceFile));
584 $bHasAlpha = true;
585 break;
586 case IMAGETYPE_BMP:
587 $sourceImage = \CFile::ImageCreateFromBMP($io->GetPhysicalName($sourceFile));
588 $bHasAlpha = false;
589 break;
590 default:
591 $sourceImage = imagecreatefromjpeg($io->GetPhysicalName($sourceFile));
592 if ($sourceImage === false)
593 {
594 ini_set('gd.jpeg_ignore_warning', 1);
595 $sourceImage = imagecreatefromjpeg($io->GetPhysicalName($sourceFile));
596 }
597
598 if ($orientation > 1)
599 {
600 $properlyOriented = \CFile::ImageHandleOrientation($orientation, $sourceImage);
601
602 if ($jpgQuality === false)
603 $jpgQuality = intval(\COption::GetOptionString('main', 'image_resize_quality', '95'));
604 if ($jpgQuality <= 0 || $jpgQuality > 100)
605 $jpgQuality = 95;
606
607 if ($properlyOriented)
608 {
609 imagejpeg($properlyOriented, $io->GetPhysicalName($destinationFile), $jpgQuality);
610 $sourceImage = $properlyOriented;
611 }
612 }
613 $bHasAlpha = false;
614 break;
615 }
616
617 $sourceImageWidth = intval(imagesx($sourceImage));
618 $sourceImageHeight = intval(imagesy($sourceImage));
619
620 if ($sourceImageWidth > 0 && $sourceImageHeight > 0)
621 {
622 if ($arSize["width"] <= 0 || $arSize["height"] <= 0)
623 {
624 $arSize["width"] = $sourceImageWidth;
625 $arSize["height"] = $sourceImageHeight;
626 }
627
628 self::ScaleImage($sourceImageWidth, $sourceImageHeight, $arSize, $resizeType, $bNeedCreatePicture, $arSourceSize, $arDestinationSize);
629
630 if ($bNeedCreatePicture)
631 {
632 if (\CFile::IsGD2())
633 {
634 $picture = imagecreatetruecolor($arDestinationSize["width"], $arDestinationSize["height"]);
635 if ($arSourceFileSizeTmp[2] == IMAGETYPE_PNG)
636 {
637 $transparentcolor = imagecolorallocatealpha($picture, 0, 0, 0, 127);
638 imagefilledrectangle($picture, 0, 0, $arDestinationSize["width"], $arDestinationSize["height"], $transparentcolor);
639
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);
645 }
646 elseif ($arSourceFileSizeTmp[2] == IMAGETYPE_GIF)
647 {
648 imagepalettecopy($picture, $sourceImage);
649
650 //Save transparency for GIFs
651 $transparentcolor = imagecolortransparent($sourceImage);
652 if ($transparentcolor >= 0 && $transparentcolor < imagecolorstotal($sourceImage))
653 {
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);
658 }
659
660 imagecopyresampled($picture, $sourceImage,
661 0, 0, $arSourceSize["x"], $arSourceSize["y"],
662 $arDestinationSize["width"], $arDestinationSize["height"], $arSourceSize["width"], $arSourceSize["height"]);
663 }
664 else
665 {
666 imagecopyresampled($picture, $sourceImage,
667 0, 0, $arSourceSize["x"], $arSourceSize["y"],
668 $arDestinationSize["width"], $arDestinationSize["height"], $arSourceSize["width"], $arSourceSize["height"]);
669 }
670 }
671 else
672 {
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"]);
677 }
678 }
679 else
680 {
681 $picture = $sourceImage;
682 }
683
684 if (is_array($arFilters))
685 {
686 foreach ($arFilters as $arFilter)
687 $bNeedCreatePicture |= \CFile::ApplyImageFilter($picture, $arFilter, $bHasAlpha);
688 }
689
690 if (is_array($arWaterMark))
691 {
692 $arWaterMark["name"] = "watermark";
693 $bNeedCreatePicture |= \CFile::ApplyImageFilter($picture, $arWaterMark, $bHasAlpha);
694 }
695
696 if ($bNeedCreatePicture)
697 {
698 if ($io->FileExists($destinationFile))
699 $io->Delete($destinationFile);
700 switch ($arSourceFileSizeTmp[2])
701 {
702 case IMAGETYPE_GIF:
703 imagegif($picture, $io->GetPhysicalName($destinationFile));
704 break;
705 case IMAGETYPE_PNG:
706 imagealphablending($picture, false);
707 imagesavealpha($picture, true);
708 imagepng($picture, $io->GetPhysicalName($destinationFile));
709 break;
710 default:
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)
716 $jpgQuality = 95;
717 imagejpeg($picture, $io->GetPhysicalName($destinationFile), $jpgQuality);
718 break;
719 }
720 imagedestroy($picture);
721 }
722 }
723
724 return true;
725 }
726
727 return false;
728 }
729
741 private static function ScaleImage($sourceImageWidth, $sourceImageHeight, $arSize, $resizeType, &$bNeedCreatePicture, &$arSourceSize, &$arDestinationSize)
742 {
743 if (!is_array($arSize))
744 $arSize = array();
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"]);
751
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);
755
756 if ($sourceImageWidth > 0 && $sourceImageHeight > 0)
757 {
758 if ($arSize["width"] > 0 && $arSize["height"] > 0)
759 {
760 switch ($resizeType)
761 {
762 case BX_RESIZE_IMAGE_EXACT:
763 $bNeedCreatePicture = true;
764
765 $ratio = (($sourceImageWidth / $sourceImageHeight) < ($arSize["width"] / $arSize["height"])) ?
766 $arSize["width"] / $sourceImageWidth : $arSize["height"] / $sourceImageHeight;
767
768 $x = max(0, ceil($sourceImageWidth / 2 - ($arSize["width"] / 2) / $ratio));
769 $y = max(0, ceil($sourceImageHeight / 2 - ($arSize["height"] / 2) / $ratio));
770
771 $arDestinationSize["width"] = $arSize["width"];
772 $arDestinationSize["height"] = $arSize["height"];
773
774 $arSourceSize["x"] = $x;
775 $arSourceSize["y"] = $y;
776 $arSourceSize["width"] = ceil($arSize["width"] / $ratio);
777 $arSourceSize["height"] = ceil($arSize["height"] / $ratio);
778 break;
779
780 default:
781 if ($resizeType == BX_RESIZE_IMAGE_PROPORTIONAL_ALT)
782 {
783 $width = max($sourceImageWidth, $sourceImageHeight);
784 $height = min($sourceImageWidth, $sourceImageHeight);
785 }
786 else
787 {
788 $width = $sourceImageWidth;
789 $height = $sourceImageHeight;
790 }
791 $ResizeCoeff["width"] = $arSize["width"] / $width;
792 $ResizeCoeff["height"] = $arSize["height"] / $height;
793
794 $iResizeCoeff = min($ResizeCoeff["width"], $ResizeCoeff["height"]);
795// $iResizeCoeff = ((0 < $iResizeCoeff) && ($iResizeCoeff < 1) ? $iResizeCoeff : 1);
796 $bNeedCreatePicture = ($iResizeCoeff > 0 ? true : false);
797
798 $arDestinationSize["width"] = max(1, intval(ceil($iResizeCoeff * $sourceImageWidth)));
799 $arDestinationSize["height"] = max(1, intval(ceil($iResizeCoeff * $sourceImageHeight)));
800
801 $arSourceSize["x"] = 0;
802 $arSourceSize["y"] = 0;
803 $arSourceSize["width"] = $sourceImageWidth;
804 $arSourceSize["height"] = $sourceImageHeight;
805 break;
806 }
807 }
808 else
809 {
810 $arSourceSize = array("x" => 0, "y" => 0, "width" => $sourceImageWidth, "height" => $sourceImageHeight);
811 $arDestinationSize = array("x" => 0, "y" => 0, "width" => $sourceImageWidth, "height" => $sourceImageHeight);
812 }
813 }
814 }
815}
static loadMessages($file)
Definition loc.php:64
static ResizeImageGet($file, $arSize, $resizeType=BX_RESIZE_IMAGE_PROPORTIONAL, $bInitSizes=false, $arFilters=false, $bImmediate=false, $jpgQuality=false)