Created
July 14, 2021 05:24
-
-
Save dnikonov/0f263b658a52f35f7067bd511458549e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// обрезка изображения под пропорции | |
if (!function_exists('cropImage')) { | |
/** | |
* cropImage обрезаем картинку до нужных пропорций (черные поля у превью с ютюба) | |
* @param [file] $file временный файл | |
* @param [float] $aspect отношение сторон | |
* @param [boolean] $youtube полосы ютюба | |
* @return [string] ответ | |
*/ | |
function cropImage(&$file, $aspect=1.7777, $youtube=true){ | |
if ($aspect) { | |
if (!empty($file["name"])) { | |
list($width, $height) = getimagesize($file["tmp_name"]); | |
if (round($width / $height, 4) < $aspect) { | |
$width_new = $width; | |
$height_new = round($width / $aspect * ($youtube ? 0.97 : 1)); // чтобы полностью убрать черные полосы ютюба | |
CFile::ResizeImage($file, array("width" => $width_new, "height" => $height_new), BX_RESIZE_IMAGE_EXACT); | |
} | |
} elseif (!empty($file["VALUE"]["name"])) { | |
list($width, $height) = getimagesize($file["VALUE"]["tmp_name"]); | |
if (round($width / $height, 4) < $aspect) { | |
$width_new = $width; | |
$height_new = round($width / $aspect * ($youtube ? 0.97 : 1)); // чтобы полностью убрать черные полосы ютюба | |
CFile::ResizeImage($file["VALUE"], array("width" => $width_new, "height" => $height_new), BX_RESIZE_IMAGE_EXACT); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment