Elgg\ImageService::normalizeResizeParameters PHP Метод

normalizeResizeParameters() публичный Метод

Calculate the parameters for resizing an image
public normalizeResizeParameters ( integer $width, integer $height, array $params = [] ) : array
$width integer Natural width of the image
$height integer Natural height of the image
$params array Resize parameters - 'w' maximum width of the resized image - 'h' maximum height of the resized image - 'upscale' allow upscaling - 'square' constrain to a square - 'x1', 'y1', 'x2', 'y2' cropping coordinates
Результат array
    public function normalizeResizeParameters($width, $height, array $params = [])
    {
        $max_width = (int) elgg_extract('w', $params, 100, false);
        $max_height = (int) elgg_extract('h', $params, 100, false);
        if (!$max_height || !$max_width) {
            throw new \LogicException("Resize width and height parameters are required");
        }
        $square = elgg_extract('square', $params, false);
        $upscale = elgg_extract('upscale', $params, false);
        $x1 = (int) elgg_extract('x1', $params, 0);
        $y1 = (int) elgg_extract('y1', $params, 0);
        $x2 = (int) elgg_extract('x2', $params, 0);
        $y2 = (int) elgg_extract('y2', $params, 0);
        $cropping_mode = $x1 || $y1 || $x2 || $y2;
        if ($cropping_mode) {
            $crop_width = $x2 - $x1;
            $crop_height = $y2 - $y1;
            if ($crop_width <= 0 || $crop_height <= 0 || $crop_width > $width || $crop_height > $height) {
                throw new \LogicException("Coordinates [{$x1}, {$y1}], [{$x2}, {$y2}] are invalid for image cropping");
            }
        } else {
            // everything selected if no crop parameters
            $crop_width = $width;
            $crop_height = $height;
        }
        // determine cropping offsets
        if ($square) {
            // asking for a square image back
            // detect case where someone is passing crop parameters that are not for a square
            if ($cropping_mode == true && $crop_width != $crop_height) {
                throw new \LogicException("Coordinates [{$x1}, {$y1}], [{$x2}, {$y2}] are invalid for a squared image cropping");
            }
            // size of the new square image
            $max_width = $max_height = min($max_width, $max_height);
            // find largest square that fits within the selected region
            $crop_width = $crop_height = min($crop_width, $crop_height);
            if (!$cropping_mode) {
                // place square region in the center
                $x1 = floor(($width - $crop_width) / 2);
                $y1 = floor(($height - $crop_height) / 2);
            }
        } else {
            // maintain aspect ratio of original image/crop
            if ($crop_height / $max_height > $crop_width / $max_width) {
                $max_width = floor($max_height * $crop_width / $crop_height);
            } else {
                $max_height = floor($max_width * $crop_height / $crop_width);
            }
        }
        if (!$upscale && ($crop_height < $max_height || $crop_width < $max_width)) {
            // we cannot upscale and selected area is too small so we decrease size of returned image
            $max_height = $crop_height;
            $max_width = $crop_width;
        }
        return ['w' => $max_width, 'h' => $max_height, 'x1' => $x1, 'y1' => $y1, 'x2' => $x1 + $crop_width, 'y2' => $y1 + $crop_height, 'square' => $square, 'upscale' => $upscale];
    }