src/Modifiers/ImageModifier.php
public function cropToFit($width, $height, $focusPointX = 0, $focusPointY = 0)
    {

        $debug = $width == 100 && $height == 100;

        // Check if the image is a valid resource
        if (!is_resource($this->image)) {
            throw new RuntimeException('No image set');
        }

        // Get the width and height of element
        $originalWidth = imagesx( $this->image );
        $originalHeight = imagesy( $this->image );

        // Check if any of the width and height is null, and set it to the $source size
        $width = $width ?: $originalWidth;
        $height = $height ?: $originalHeight;

        // Calculate the aspect ratio of the existing image
        $originalAspectRatio = $originalWidth / $originalHeight;

        // Calculate the aspect ratio of the desired image
        $desiredAspectRatio = $width / $height;

        // Check what value we should use with the aspect ratio
        if ( $originalAspectRatio > $desiredAspectRatio ) {
            $temp_height = $height;
            $temp_width = ( int ) ($height * $originalAspectRatio);
        }
        else {
            $temp_width = $width;
            $temp_height = ( int ) ($width / $originalAspectRatio);
        }

        // Get the center of image based on the focusPoint
        $x0 = ( $temp_width - $width ) * ( ( $focusPointX + 1 ) * .5 );
        $y0 = ( $temp_height - $height ) * ( ( $focusPointY + 1 ) * .5 );

        // If $x0 or $y0 is not a full number increase with one pixel
        // to fill black line and fill canvas
        if ( ! ctype_digit( $x0 ) || ! ctype_digit( $y0 ) ) {
            $temp_width++;
            $temp_height++;
        }

        // Resize the image into the desired image
        $this->resize( $temp_width, $temp_height );

        // Create canvas for the new image
        $canvas = $this->createCanvas( $width, $height );
        // Do the cropping of image
        imagecopy( $canvas, $this->image, 0, 0, $x0, $y0, $width, $height );

        // Replace the image
        $this->replace( $canvas );

        // Return $this for chaining
        return $this;
    }
public function cropToFit($width, $height, $focusPointX = 0, $focusPointY = 0)
    {

        $debug = $width == 100 && $height == 100;

        // Check if the image is a valid resource
        if (!is_resource($this->image)) {
            throw new RuntimeException('No image set');
        }

        // Get the width and height of element
        $originalWidth = imagesx( $this->image );
        $originalHeight = imagesy( $this->image );

        // Check if any of the width and height is null, and set it to the $source size
        $width = $width ?: $originalWidth;
        $height = $height ?: $originalHeight;

        // Calculate the aspect ratio of the existing image
        $originalAspectRatio = $originalWidth / $originalHeight;

        // Calculate the aspect ratio of the desired image
        $desiredAspectRatio = $width / $height;

        // Check what value we should use with the aspect ratio
        if ( $originalAspectRatio > $desiredAspectRatio ) {
            $temp_height = $height;
            $temp_width = ( int ) ($height * $originalAspectRatio);
        }
        else {
            $temp_width = $width;
            $temp_height = ( int ) ($width / $originalAspectRatio);
        }

        // Get the center of image based on the focusPoint
        $x0 = ( $temp_width - $width ) * ( ( $focusPointX + 1 ) * .5 );
        $y0 = ( $temp_height - $height ) * ( ( $focusPointY + 1 ) * .5 );

        // If $x0 or $y0 is not a full number increase with one pixel
        // to fill black line and fill canvas
        if ( ! ctype_digit( $x0 ) || ! ctype_digit( $y0 ) ) {
            $temp_width++;
            $temp_height++;
        }

        // Resize the image into the desired image
        $this->resize( $temp_width, $temp_height );

        // Create canvas for the new image
        $canvas = $this->createCanvas( $width, $height );
        // Do the cropping of image
        imagecopy( $canvas, $this->image, 0, 0, $x0, $y0, $width, $height );

        // Replace the image
        $this->replace( $canvas );

        // Return $this for chaining
        return $this;
    }
public function cropToFit ( $ width , $ height , $ focusPointX = 0 , $ focusPointY = 0 ) { $ debug = $ width == 100 && $ height == 100 ; // Check if the image is a valid resource if ( ! is_resource ( $ this -> image ) ) { throw new RuntimeException ( 'No image set' ) ; } // Get the width and height of element $ originalWidth = imagesx ( $ this -> image ) ; $ originalHeight = imagesy ( $ this -> image ) ; // Check if any of the width and height is null, and set it to the $source size $ width = $ width ? : $ originalWidth ; $ height = $ height ? : $ originalHeight ; // Calculate the aspect ratio of the existing image $ originalAspectRatio = $ originalWidth / $ originalHeight ; // Calculate the aspect ratio of the desired image $ desiredAspectRatio = $ width / $ height ; // Check what value we should use with the aspect ratio if ( $ originalAspectRatio > $ desiredAspectRatio ) { $ temp_height = $ height ; $ temp_width = ( int ) ( $ height * $ originalAspectRatio ) ; } else { $ temp_width = $ width ; $ temp_height = ( int ) ( $ width / $ originalAspectRatio ) ; } // Get the center of image based on the focusPoint $ x0 = ( $ temp_width - $ width ) * ( ( $ focusPointX + 1 ) * .5 ) ; $ y0 = ( $ temp_height - $ height ) * ( ( $ focusPointY + 1 ) * .5 ) ; // If $x0 or $y0 is not a full number increase with one pixel // to fill black line and fill canvas if ( ! ctype_digit ( $ x0 ) || ! ctype_digit ( $ y0 ) ) { $ temp_width ++ ; $ temp_height ++ ; } // Resize the image into the desired image $ this -> resize ( $ temp_width , $ temp_height ) ; // Create canvas for the new image $ canvas = $ this -> createCanvas ( $ width , $ height ) ; // Do the cropping of image imagecopy ( $ canvas , $ this -> image , 0 , 0 , $ x0 , $ y0 , $ width , $ height ) ; // Replace the image $ this -> replace ( $ canvas ) ; // Return $this for chaining return $ this ; }
Crop image To the width and height of the image

@return $this
Crop image To the width and height of the image