Crops the image

@param int|null $width  The width; If null will use the original width
@param int|null $height The height; If null will use the original height

@return ImageHandler
345545
public function crop($width = null, $height = null)
    {
        //get the source width and height
        $orgWidth = imagesx($this->resource);
        $orgHeight = imagesy($this->resource);

        //set the width if none is defined
        if (is_null($width)) {
            $width = $orgWidth;
        }

        //set the height if none is defined
        if (is_null($height)) {
            $height = $orgHeight;
        }

        //if the width and height are the same as the originals
        if ($width == $orgWidth && $height == $orgHeight) {
            //there's no need to process
            return $this;
        }

        //if we are here then we do need to crop
        //create the new resource with the width and height
        $crop = imagecreatetruecolor($width, $height);

        //set some defaults
        $xPosition = 0;
        $yPosition = 0;

        //if the width is greater than the original width
        //or if the height is greater than the original height
        if ($width > $orgWidth || $height > $orgHeight) {
            //save the destination width and height
            //because they will change here
            $newWidth = $width;
            $newHeight = $height;

            //if the desired height is larger than the desired width
            if ($height > $width) {
                //and adjust the height instead
                $height = $this->getHeightAspectRatio($orgWidth, $orgHeight, $width);
                //if the aspect height is bigger than the desired height
                if ($newHeight > $height) {
                    //set it back to the desired height
                    $height = $newHeight;
                    //and adjust the width instead
                    $width = $this->getWidthAspectRatio($orgWidth, $orgHeight, $height);
                    //now because of the way GD renders we need to find the ratio of desired
                    //height if it was brought down to the original height
                    $rWidth = $this->getWidthAspectRatio($newWidth, $newHeight, $orgHeight);
                    //set the x Position of the source to the center of the
                    //original width image width minus half the rWidth width
                    $xPosition = ($orgWidth / 2) - ($rWidth / 2);
                } else {
                    //now because of the way GD renders we need to find the ratio of desired
                    //height if it was brought down to the original height
                    $rHeight = $this->getHeightAspectRatio($newWidth, $newHeight, $orgWidth);
                    //set the y Position of the source to the center of the
                    //new sized image height minus half the desired height
                    $yPosition = ($orgHeight / 2) - ($rHeight / 2) ;
                }
            //if the desired height is smaller than the desired width
            } else {
                //get the width aspect ratio
                $width = $this->getWidthAspectRatio($orgWidth, $orgHeight, $height);
                //if the aspect height is bigger than the desired height
                if ($newWidth > $width) {
                    //set it back to the desired height
                    $width = $newWidth;
                    //and adjust the width instead
                    $height = $this->getHeightAspectRatio($orgWidth, $orgHeight, $width);
                    //now because of the way GD renders we need to find the ratio of desired
                    //height if it was brought down to the original height
                    $rHeight = $this->getHeightAspectRatio($newWidth, $newHeight, $orgWidth);
                    //set the y Position of the source to the center of the
                    //new sized image height minus half the desired height
                    $yPosition = ($orgHeight / 2) - ($rHeight / 2) ;
                } else {
                    //now because of the way GD renders we need to find the ratio of desired
                    //height if it was brought down to the original height
                    $rWidth = $this->getWidthAspectRatio($newWidth, $newHeight, $orgHeight);
                    //set the x Position of the source to the center of the
                    //original width image width minus half the rWidth width
                    $xPosition = ($orgWidth / 2) - ($rWidth / 2);
                }
            }
        } else {
            //if the width is less than the original width
            if ($width < $orgWidth) {
                //set the x Position of the source to the center of the
                //original image width minus half the desired width
                $xPosition = ($orgWidth / 2) - ($width / 2);
                //set the destination width to be the original width
                $width = $orgWidth;
            }

            //if the height is less than the original height
            if ($height < $orgHeight) {
                //set the y Position of the source to the center of the
                //original image height minus half the desired height
                $yPosition = ($orgHeight / 2) - ($height / 2);
                //set the destination height to be the original height
                $height = $orgHeight;
            }
        }

        //render the image
        imagecopyresampled($crop, $this->resource, 0, 0, $xPosition, $yPosition, $width, $height, $orgWidth, $orgHeight);

        //destroy the original resource
        imagedestroy($this->resource);

        //assign the new resource
        $this->resource = $crop;

        return $this;
    }
16699
public function resize(int $width = 0, int $height = 0) : ImageInterface
    {
        $this->operations[] = [ __FUNCTION__, func_get_args() ];
        return $this;
    }
906876
final public function resizeImage($width = null, $height = null)
	{
		if (is_int($width) and is_int($height)) {
			$new_image = imagecreatetruecolor($width, $height);
			imagecopyresampled(
				$new_image,
				$this->_image_handle,
				0,
				0,
				0,
				0,
				$width,
				$height,
				$this->imageWidth(),
				$this->imageHeight()
			);
			if (is_resource($new_image)) {
				$this->_image_handle = $new_image;
			}
		}
		return $this;
	}
946516
protected function resizeCrop($file, $width, $height)
  	{
		$image = $this->imagine->open($file->getRealPath());
		list($optimalWidth, $optimalHeight) = $this->getOptimalCrop($image->getSize(), $width, $height);

    	// Find center - this will be used for the crop
		$centerX = ($optimalWidth / 2) - ($width / 2);
    	$centerY = ($optimalHeight / 2) - ($height / 2);

		return $image->resize(new Box($optimalWidth, $optimalHeight))
			->crop(new Point($centerX, $centerY), new Box($width, $height));
	}
82107
public function crop($width, $height, $mode = 'tl')
    {
        if ( !is_numeric($width) ) {
            throw new \InvalidArgumentException('$width must be numeric');
        }

        if ( !is_numeric($height) ) {
            throw new \InvalidArgumentException('$height must be numeric');
        }

        if ( $this->newImage )
        {
            $this->sourceImage = $this->newImage;
        }

        $oldWidth   = ($this->newImage) ? imagesx($this->newImage) : $this->source['width'];
        $oldHeight  = ($this->newImage) ? imagesy($this->newImage) : $this->source['height'];

        $this->create($width, $height);

        $startX     = $startY = 0;
        $cropWidth  = $sourceWidth  = $width;
        $cropHeight = $sourceHeight = $height;

        if ( is_array($mode) )
        {
            $startX = $mode[0];
            $startY = $mode[1];
        }

        if ($mode === self::CROP_TOP_CENTER)
        {
            $startX = ($oldWidth - $cropWidth) / 2;
        }
        else if ($mode === self::CROP_TOP_RIGHT)
        {
            $startX = $oldWidth - $cropWidth;
        }
        else if ($mode === self::CROP_CENTER_LEFT)
        {
            $startY = ($oldHeight - $cropHeight) / 2;
        }
        else if ($mode === self::CROP_CENTER_CENTER)
        {
            $startX = ($oldWidth - $cropWidth)   / 2;
            $startY = ($oldHeight - $cropHeight) / 2;
        }
        else if ($mode === self::CROP_CENTER_RIGHT)
        {
            $startX = $oldWidth - $cropWidth;
            $startY = ($oldHeight - $cropHeight) / 2;
        }
        else if ($mode === self::CROP_BOTTOM_LEFT)
        {
            $startY = $oldHeight - $cropHeight;
        }
        else if ($mode === self::CROP_BOTTOM_CENTER)
        {
            $startX = ($oldWidth - $cropWidth)   / 2;
            $startY = $oldHeight - $cropHeight;
        }
        else if ($mode === self::CROP_BOTTOM_RIGHT)
        {
            $startX = $oldWidth - $cropWidth;
            $startY = $oldHeight - $cropHeight;
        }
        else
        {
        }

        imagecopyresampled(
            $this->newImage,
            $this->sourceImage,
            0, 0, $startX, $startY,
            $cropWidth, $cropHeight, $sourceWidth, $sourceHeight);

        return $this;
    }
1209668
protected function imageResize($width, $height)
    {
        $width = (int) $width;
        $height = (int) $height;
        if ($width <= 0) {
            throw new BadMethodCallException('Invalid parameter width for command "resize"');
        }
        if ($height <= 0) {
            throw new BadMethodCallException('Invalid parameter height for command "resize"');
        }
        $this->image->resize(new Box($width, $height));
    }
1171047
public function freecrop($width, $height, $x = false, $y = false)
    {
        if ($x === false || $y === false) {
            return $this->crop($width, $height);
        }
        $this->source_x = $x;
        $this->source_y = $y;
        if ($width > $this->getSourceWidth() - $x) {
            $this->source_w = $this->getSourceWidth() - $x;
        } else {
            $this->source_w = $width;
        }

        if ($height > $this->getSourceHeight() - $y) {
            $this->source_h = $this->getSourceHeight() - $y;
        } else {
            $this->source_h = $height;
        }

        $this->dest_w = $width;
        $this->dest_h = $height;

        return $this;
    }
1349386
public function crop(string $cropMethod, int $width, int $height)
    {
        if (! $this->validateManipulation($cropMethod, 'crop')) {
            throw InvalidManipulation::invalidParameter(
                'cropmethod',
                $cropMethod,
                $this->getValidManipulationOptions('crop')
            );
        }

        $this->width($width);
        $this->height($height);

        return $this->addManipulation('crop', $cropMethod);
    }
919512
public function resizeCropCenter($width, $height)
    {
        $_width = $this->do_getWidth();
        $_height = $this->do_getHeight();

        if ($_width / $_height > $width / $height) {
            $crop_height = $_height;
            $crop_width = $width * $crop_height / $height;
            $offsetX = ($_width - $crop_width) / 2;
            $offsetY = 0;
        } else {
            $crop_width = $_width;
            $crop_height = $height * $crop_width / $width;
            $offsetY = ($_height - $crop_height) / 2;
            $offsetX = 0;
        }

        $this->crop($crop_width, $crop_height, $offsetX, $offsetY);
        $this->scale($width / $crop_width);

        return $this;
    }
699395
public function Fit($width, $height)
    {
        $width = $this->castDimension($width, 'Width');
        $height = $this->castDimension($height, 'Height');
        // Item must be regenerated
        $variant = $this->variantName(__FUNCTION__, $width, $height);
        return $this->manipulateImage($variant, function (Image_Backend $backend) use ($width, $height) {
            // Check if image is already sized to the correct dimension
            $currentWidth = $backend->getWidth();
            $currentHeight = $backend->getHeight();
            if (!$currentWidth || !$currentHeight) {
                return null;
            }
            $widthRatio = $width / $currentWidth;
            $heightRatio = $height / $currentHeight;

            if ($widthRatio < $heightRatio) {
                // Target is higher aspect ratio than image, so check width
                if ($currentWidth === $width) {
                    return $this;
                }
            } else {
                // Target is wider or same aspect ratio as image, so check height
                if ($currentHeight === $height) {
                    return $this;
                }
            }

            return $backend->resizeRatio($width, $height);
        });
    }
1220509
public ImageHandlerBuilder crop(int x, int y, int width, int height) {
         height = Math.min(height, image.getHeight() - y); 
         width = Math.min(width, image.getWidth() - x);

         BufferedImage crop = Scalr.crop(image, x, y, width, height);
         image.flush();// cannot throw
         image = crop;
         return this;
     }
448950
public function lightbox($width = null, $height = null, $reset = true)
    {
        if ($this->mode !== 'source') {
            $this->display('source');
        }

        if ($width && $height) {
            $this->__call('cropResize', [$width, $height]);
        }

        return parent::lightbox($width, $height, $reset);
    }
1196295
public function resizeAndOrCrop($width, $height, $options) {
        if (!$width && !$height) return $this;
        if (isset($options['quadrant'])) return $this->cropQuadrant($width, $height, $options);
        if (array_key_exists('pad', $options)) $this->pad($width, $height, $options);
        if (array_key_exists('resize', $options) || !$width || !$height) return $this->resize($width, $height);
        return $this->crop($width, $height);
    }
501575
public function resize(int $width = 0, int $height = 0)
    {
        $this->instance->scaleImage($width, $height);
    }
1237175
public function resize( $width = null, $height = null )
    {
        // Get the source width and height
        $sourceWidth = imagesx( $this->image );
        $sourceHeight = imagesy( $this->image );

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

        if ( !$width && !$height ) {
            $width = $sourceWidth;
            $height = $sourceHeight;
        }
        else if ( ! $width ) {
            $width = $height * $sourceAspectRatio;
        }
        else {
            $height = $width / $sourceAspectRatio;
        }

        $maxCanvasWidth = $width;
        $maxCanvasHeight = $height;

        $thumbnailAspectRatio = $maxCanvasWidth / $maxCanvasHeight;

        // Create canvas for the new image
        $canvas = $this->createCanvas( $width, $height );

        // Do the resizing of the image
        imagecopyresampled($canvas, $this->image, 0, 0, 0, 0, $width, $height, $sourceWidth, $sourceHeight);

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

        // Return this for linking
        return $this;
    }
993218
public function centerCrop($width, $height)
    {
        $pwidth = $this->getPropWidth($height);
        $pheight = $this->getPropHeight($width);

        if ($pwidth == $width && $pheight == $height) {
            $this->resizeToWidth($width);
        } else {
            if ($pheight > $height) {
                $this->resizeToWidth($width);
            } else {
                $pheight += $height - $pheight;
                $this->resizeToHeight($pheight);
            }

            $x = ($this->width - $width) / 2;
            $y = ($this->height - $height) / 2;
            $this->crop($width, $height, $x, $y);
        }

        return $this;
    }
1251185
public function resize($width, $height)
    {
        $new = imagecreatetruecolor($width, $height);

        if (empty($new)) {
            throw new UnexpectedValueException('Failed to create an image identifier');
        }

        if ($this->format === 'gif') {

            $transparent_index = imagecolortransparent($this->image);
            $palletsize = imagecolorstotal($this->image);

            if ($transparent_index >= 0 && $transparent_index < $palletsize) {
                $transparent_color = imagecolorsforindex($this->image, $transparent_index);
                $transparent_index = imagecolorallocate($new, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
                imagefill($new, 0, 0, $transparent_index);
                imagecolortransparent($new, $transparent_index);
            }
        } else {
            imagealphablending($new, false);
            imagesavealpha($new, true);
        }

        if (!imagecopyresampled($new, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height)) {
            throw new UnexpectedValueException('Failed to copy and resize part of an image with resampling');
        }

        $this->image = $new;
        $this->width = $width;
        $this->height = $height;

        return $this;
    }