public function FocusFill($width, $height, AssetContainer $image, $upscale = true)
    {
        if (!$image && $this->record instanceof Image) {
            $image = $this->record;
        }
        $width = intval($width);
        $height = intval($height);
        $imgW = $image->getWidth();
        $imgH = $image->getHeight();

        // Don't enlarge
        if (!$upscale) {
            $widthRatio = $imgW / $width;
            $heightRatio = $imgH / $height;
            if ($widthRatio < 1 && $widthRatio <= $heightRatio) {
                $width = $imgW;
                $height = intval(round($height * $widthRatio));
            } elseif ($heightRatio < 1) {
                $height = $imgH;
                $width = intval(round($width * $heightRatio));
            }
        }

        //Only resize if necessary
        if ($image->isSize($width, $height) && !Config::inst()->get(DBFile::class, 'force_resample')) {
            return $image;
        } elseif ($cropData = $this->calculateCrop($width, $height, $imgW, $imgH)) {
            $variant = $image->variantName(__FUNCTION__, $width, $height, $cropData['CropAxis'], $cropData['CropOffset']);
            $cropped = $image->manipulateImage($variant, function (Image_Backend $backend) use ($width, $height, $cropData) {
                $img = null;
                $cropAxis = $cropData['CropAxis'];
                $cropOffset = $cropData['CropOffset'];

                if ($cropAxis == 'x') {
                    //Generate image
                    $img = $backend
                        ->resizeByHeight($height)
                        ->crop(0, $cropOffset, $width, $height);
                } elseif ($cropAxis == 'y') {
                    //Generate image
                    $img = $backend
                        ->resizeByWidth($width)
                        ->crop($cropOffset, 0, $width, $height);
                } else {
                    //Generate image without cropping
                    $img = $backend->resize($width, $height);
                }

                if (!$img) {
                    return null;
                }

                return $img;
            });

            // Update FocusPoint
            $cropped->FocusPoint = DBField::create_field(static::class, [
                'X' => $cropData['x']['FocusPoint'],
                'Y' => $cropData['y']['FocusPoint']
            ]);

            return $cropped;
        }
        return null;
    }
public function FocusFill($width, $height, AssetContainer $image, $upscale = true)
    {
        if (!$image && $this->record instanceof Image) {
            $image = $this->record;
        }
        $width = intval($width);
        $height = intval($height);
        $imgW = $image->getWidth();
        $imgH = $image->getHeight();

        // Don't enlarge
        if (!$upscale) {
            $widthRatio = $imgW / $width;
            $heightRatio = $imgH / $height;
            if ($widthRatio < 1 && $widthRatio <= $heightRatio) {
                $width = $imgW;
                $height = intval(round($height * $widthRatio));
            } elseif ($heightRatio < 1) {
                $height = $imgH;
                $width = intval(round($width * $heightRatio));
            }
        }

        //Only resize if necessary
        if ($image->isSize($width, $height) && !Config::inst()->get(DBFile::class, 'force_resample')) {
            return $image;
        } elseif ($cropData = $this->calculateCrop($width, $height, $imgW, $imgH)) {
            $variant = $image->variantName(__FUNCTION__, $width, $height, $cropData['CropAxis'], $cropData['CropOffset']);
            $cropped = $image->manipulateImage($variant, function (Image_Backend $backend) use ($width, $height, $cropData) {
                $img = null;
                $cropAxis = $cropData['CropAxis'];
                $cropOffset = $cropData['CropOffset'];

                if ($cropAxis == 'x') {
                    //Generate image
                    $img = $backend
                        ->resizeByHeight($height)
                        ->crop(0, $cropOffset, $width, $height);
                } elseif ($cropAxis == 'y') {
                    //Generate image
                    $img = $backend
                        ->resizeByWidth($width)
                        ->crop($cropOffset, 0, $width, $height);
                } else {
                    //Generate image without cropping
                    $img = $backend->resize($width, $height);
                }

                if (!$img) {
                    return null;
                }

                return $img;
            });

            // Update FocusPoint
            $cropped->FocusPoint = DBField::create_field(static::class, [
                'X' => $cropData['x']['FocusPoint'],
                'Y' => $cropData['y']['FocusPoint']
            ]);

            return $cropped;
        }
        return null;
    }
public function FocusFill ( $ width , $ height , AssetContainer $ image , $ upscale = true ) { if ( ! $ image && $ this -> record instanceof Image ) { $ image = $ this -> record ; } $ width = intval ( $ width ) ; $ height = intval ( $ height ) ; $ imgW = $ image -> getWidth ( ) ; $ imgH = $ image -> getHeight ( ) ; // Don't enlarge if ( ! $ upscale ) { $ widthRatio = $ imgW / $ width ; $ heightRatio = $ imgH / $ height ; if ( $ widthRatio < 1 && $ widthRatio <= $ heightRatio ) { $ width = $ imgW ; $ height = intval ( round ( $ height * $ widthRatio ) ) ; } elseif ( $ heightRatio < 1 ) { $ height = $ imgH ; $ width = intval ( round ( $ width * $ heightRatio ) ) ; } } //Only resize if necessary if ( $ image -> isSize ( $ width , $ height ) && ! Config :: inst ( ) -> get ( DBFile :: class , 'force_resample' ) ) { return $ image ; } elseif ( $ cropData = $ this -> calculateCrop ( $ width , $ height , $ imgW , $ imgH ) ) { $ variant = $ image -> variantName ( __FUNCTION__ , $ width , $ height , $ cropData [ 'CropAxis' ] , $ cropData [ 'CropOffset' ] ) ; $ cropped = $ image -> manipulateImage ( $ variant , function ( Image_Backend $ backend ) use ( $ width , $ height , $ cropData ) { $ img = null ; $ cropAxis = $ cropData [ 'CropAxis' ] ; $ cropOffset = $ cropData [ 'CropOffset' ] ; if ( $ cropAxis == 'x' ) { //Generate image $ img = $ backend -> resizeByHeight ( $ height ) -> crop ( 0 , $ cropOffset , $ width , $ height ) ; } elseif ( $ cropAxis == 'y' ) { //Generate image $ img = $ backend -> resizeByWidth ( $ width ) -> crop ( $ cropOffset , 0 , $ width , $ height ) ; } else { //Generate image without cropping $ img = $ backend -> resize ( $ width , $ height ) ; } if ( ! $ img ) { return null ; } return $ img ; } ) ; // Update FocusPoint $ cropped -> FocusPoint = DBField :: create_field ( static :: class , [ 'X' => $ cropData [ 'x' ] [ 'FocusPoint' ] , 'Y' => $ cropData [ 'y' ] [ 'FocusPoint' ] ] ) ; return $ cropped ; } return null ; }
Generate a cropped version of the given image
@param int $width desired width
@param int $height desired height
@param Image $image the image to crop. If not set, the current record will be used
@param bool $upscale whether or not upscaling is allowed
@return AssetContainer|null
Generate a cropped version of the given image