public function calculateCrop($width, $height, $originalWidth, $originalHeight)
    {
        // Work out how to crop the image and provide new focus coordinates
        $cropData = [
            'CropAxis' => 0,
            'CropOffset' => 0,
        ];

        $cropData['x'] = [
            'FocusPoint' => $this->getX(),
            'OriginalLength' => $originalWidth,
            'TargetLength' => round($width),
        ];

        $cropData['y'] = [
            'FocusPoint' => $this->getY(),
            'OriginalLength' => $originalHeight,
            'TargetLength' => round($height),
        ];

        // Avoid divide by zero error
        if (!($cropData['x']['OriginalLength'] > 0 && $cropData['y']['OriginalLength'] > 0)) {
            return false;
        }

        // Work out which axis to crop on
        $cropAxis = false;
        $cropData['x']['ScaleRatio'] = $cropData['x']['OriginalLength'] / $cropData['x']['TargetLength'];
        $cropData['y']['ScaleRatio'] = $cropData['y']['OriginalLength'] / $cropData['y']['TargetLength'];
        if ($cropData['x']['ScaleRatio'] < $cropData['y']['ScaleRatio']) {
            // Top and/or bottom of image will be lost
            $cropAxis = 'y';
            $scaleRatio = $cropData['x']['ScaleRatio'];
        } elseif ($cropData['x']['ScaleRatio'] > $cropData['y']['ScaleRatio']) {
            // Left and/or right of image will be lost
            $cropAxis = 'x';
            $scaleRatio = $cropData['y']['ScaleRatio'];
        }
        $cropData['CropAxis'] = $cropAxis;

        // Adjust dimensions for cropping
        if ($cropAxis) {
            // Focus point offset
            $focusOffset = $this->focusCoordToOffset($cropData[$cropAxis]['FocusPoint']);
            // Length after scaling but before cropping
            $scaledImageLength = floor($cropData[$cropAxis]['OriginalLength'] / $scaleRatio);
            // Focus point position in pixels
            $focusPos = floor($focusOffset * $scaledImageLength);
            // Container center in pixels
            $frameCenter = floor($cropData[$cropAxis]['TargetLength'] / 2);
            // Difference beetween focus point and center
            $focusShift = $focusPos - $frameCenter;
            // Limit offset so image remains filled
            $remainder = $scaledImageLength - $focusPos;
            $croppedRemainder = $cropData[$cropAxis]['TargetLength'] - $frameCenter;
            if ($remainder < $croppedRemainder) {
                $focusShift -= $croppedRemainder - $remainder;
            }
            if ($focusShift < 0) {
                $focusShift = 0;
            }
            // Set cropping start point
            $cropData['CropOffset'] = $focusShift;
            // Update Focus point location for cropped image
            $newFocusOffset = ($focusPos - $focusShift) / $cropData[$cropAxis]['TargetLength'];
            $cropData[$cropAxis]['FocusPoint'] = $this->focusOffsetToCoord($newFocusOffset);
        }

        return $cropData;
    }
public function calculateCrop($width, $height, $originalWidth, $originalHeight)
    {
        // Work out how to crop the image and provide new focus coordinates
        $cropData = [
            'CropAxis' => 0,
            'CropOffset' => 0,
        ];

        $cropData['x'] = [
            'FocusPoint' => $this->getX(),
            'OriginalLength' => $originalWidth,
            'TargetLength' => round($width),
        ];

        $cropData['y'] = [
            'FocusPoint' => $this->getY(),
            'OriginalLength' => $originalHeight,
            'TargetLength' => round($height),
        ];

        // Avoid divide by zero error
        if (!($cropData['x']['OriginalLength'] > 0 && $cropData['y']['OriginalLength'] > 0)) {
            return false;
        }

        // Work out which axis to crop on
        $cropAxis = false;
        $cropData['x']['ScaleRatio'] = $cropData['x']['OriginalLength'] / $cropData['x']['TargetLength'];
        $cropData['y']['ScaleRatio'] = $cropData['y']['OriginalLength'] / $cropData['y']['TargetLength'];
        if ($cropData['x']['ScaleRatio'] < $cropData['y']['ScaleRatio']) {
            // Top and/or bottom of image will be lost
            $cropAxis = 'y';
            $scaleRatio = $cropData['x']['ScaleRatio'];
        } elseif ($cropData['x']['ScaleRatio'] > $cropData['y']['ScaleRatio']) {
            // Left and/or right of image will be lost
            $cropAxis = 'x';
            $scaleRatio = $cropData['y']['ScaleRatio'];
        }
        $cropData['CropAxis'] = $cropAxis;

        // Adjust dimensions for cropping
        if ($cropAxis) {
            // Focus point offset
            $focusOffset = $this->focusCoordToOffset($cropData[$cropAxis]['FocusPoint']);
            // Length after scaling but before cropping
            $scaledImageLength = floor($cropData[$cropAxis]['OriginalLength'] / $scaleRatio);
            // Focus point position in pixels
            $focusPos = floor($focusOffset * $scaledImageLength);
            // Container center in pixels
            $frameCenter = floor($cropData[$cropAxis]['TargetLength'] / 2);
            // Difference beetween focus point and center
            $focusShift = $focusPos - $frameCenter;
            // Limit offset so image remains filled
            $remainder = $scaledImageLength - $focusPos;
            $croppedRemainder = $cropData[$cropAxis]['TargetLength'] - $frameCenter;
            if ($remainder < $croppedRemainder) {
                $focusShift -= $croppedRemainder - $remainder;
            }
            if ($focusShift < 0) {
                $focusShift = 0;
            }
            // Set cropping start point
            $cropData['CropOffset'] = $focusShift;
            // Update Focus point location for cropped image
            $newFocusOffset = ($focusPos - $focusShift) / $cropData[$cropAxis]['TargetLength'];
            $cropData[$cropAxis]['FocusPoint'] = $this->focusOffsetToCoord($newFocusOffset);
        }

        return $cropData;
    }
public function calculateCrop ( $ width , $ height , $ originalWidth , $ originalHeight ) { // Work out how to crop the image and provide new focus coordinates $ cropData = [ 'CropAxis' => 0 , 'CropOffset' => 0 , ] ; $ cropData [ 'x' ] = [ 'FocusPoint' => $ this -> getX ( ) , 'OriginalLength' => $ originalWidth , 'TargetLength' => round ( $ width ) , ] ; $ cropData [ 'y' ] = [ 'FocusPoint' => $ this -> getY ( ) , 'OriginalLength' => $ originalHeight , 'TargetLength' => round ( $ height ) , ] ; // Avoid divide by zero error if ( ! ( $ cropData [ 'x' ] [ 'OriginalLength' ] > 0 && $ cropData [ 'y' ] [ 'OriginalLength' ] > 0 ) ) { return false ; } // Work out which axis to crop on $ cropAxis = false ; $ cropData [ 'x' ] [ 'ScaleRatio' ] = $ cropData [ 'x' ] [ 'OriginalLength' ] / $ cropData [ 'x' ] [ 'TargetLength' ] ; $ cropData [ 'y' ] [ 'ScaleRatio' ] = $ cropData [ 'y' ] [ 'OriginalLength' ] / $ cropData [ 'y' ] [ 'TargetLength' ] ; if ( $ cropData [ 'x' ] [ 'ScaleRatio' ] < $ cropData [ 'y' ] [ 'ScaleRatio' ] ) { // Top and/or bottom of image will be lost $ cropAxis = 'y' ; $ scaleRatio = $ cropData [ 'x' ] [ 'ScaleRatio' ] ; } elseif ( $ cropData [ 'x' ] [ 'ScaleRatio' ] > $ cropData [ 'y' ] [ 'ScaleRatio' ] ) { // Left and/or right of image will be lost $ cropAxis = 'x' ; $ scaleRatio = $ cropData [ 'y' ] [ 'ScaleRatio' ] ; } $ cropData [ 'CropAxis' ] = $ cropAxis ; // Adjust dimensions for cropping if ( $ cropAxis ) { // Focus point offset $ focusOffset = $ this -> focusCoordToOffset ( $ cropData [ $ cropAxis ] [ 'FocusPoint' ] ) ; // Length after scaling but before cropping $ scaledImageLength = floor ( $ cropData [ $ cropAxis ] [ 'OriginalLength' ] / $ scaleRatio ) ; // Focus point position in pixels $ focusPos = floor ( $ focusOffset * $ scaledImageLength ) ; // Container center in pixels $ frameCenter = floor ( $ cropData [ $ cropAxis ] [ 'TargetLength' ] / 2 ) ; // Difference beetween focus point and center $ focusShift = $ focusPos - $ frameCenter ; // Limit offset so image remains filled $ remainder = $ scaledImageLength - $ focusPos ; $ croppedRemainder = $ cropData [ $ cropAxis ] [ 'TargetLength' ] - $ frameCenter ; if ( $ remainder < $ croppedRemainder ) { $ focusShift -= $ croppedRemainder - $ remainder ; } if ( $ focusShift < 0 ) { $ focusShift = 0 ; } // Set cropping start point $ cropData [ 'CropOffset' ] = $ focusShift ; // Update Focus point location for cropped image $ newFocusOffset = ( $ focusPos - $ focusShift ) / $ cropData [ $ cropAxis ] [ 'TargetLength' ] ; $ cropData [ $ cropAxis ] [ 'FocusPoint' ] = $ this -> focusOffsetToCoord ( $ newFocusOffset ) ; } return $ cropData ; }
Caluclate crop data given the desired width and height, as well as original width and height.
Calculates required crop coordinates using current FocusX and FocusY
@param int $width desired width
@param int $height desired height
@param int $originalWidth original image width
@param int $originalHeight original image height
@return array|bool
Caluclate crop data given the desired width and height as well as original width and height . Calculates required crop coordinates using current FocusX and FocusY