Common image/crop validator check

@param int $width
@param int $height
@param array $configuration

@throws ValidationException
170720
protected function isValid($width, $height, array $configuration)
    {
        if (isset($configuration['MinWidth']) && $this->validateConfig('MinWidth', $configuration)) {
            if ($width < $configuration['MinWidth']) {
                throw new ValidationException('Minimum width must be '.$configuration['MinWidth'].'px');
            }
        }

        if (isset($configuration['MaxWidth']) && $this->validateConfig('MaxWidth', $configuration)) {
            if ($width > $configuration['MaxWidth']) {
                throw new ValidationException('Maximum width must be '.$configuration['MaxWidth'].'px');
            }
        }

        if (isset($configuration['MinHeight']) && $this->validateConfig('MinHeight', $configuration)) {
            if ($height < $configuration['MinHeight']) {
                throw new ValidationException('Minimum height must be '.$configuration['MinHeight'].'px');
            }
        }

        if (isset($configuration['MaxHeight']) && $this->validateConfig('MaxHeight', $configuration)) {
            if ($height > $configuration['MaxHeight']) {
                throw new ValidationException('Minimum height must be '.$configuration['MaxHeight'].'px');
            }
        }

        $ratio = round($width / $height, 2);

        if (isset($configuration['MinRatio']) && $this->validateConfig('MinRatio', $configuration, true)) {
            if ($ratio < $configuration['MinRatio']) {
                throw new ValidationException('Minimum ratio must be '.$configuration['MinRatio']);
            }
        }

        if (isset($configuration['MaxRatio']) && $this->validateConfig('MaxRatio', $configuration, true)) {
            if ($ratio > $configuration['MaxRatio']) {
                throw new ValidationException('Maximum ratio must be '.$configuration['MaxRatio']);
            }
        }
    }
564521
public function resize($width, $height) {
        if (!$width) $width = 1;
        if (!$height) $height = 1;
        return (
            (false !== ($img = new gd(array($width, $height)))) &&
            $img->imagecopyresampled($this) &&
            (false !== ($this->image = $img->get_image())) &&
            (false !== ($this->width = $img->get_width())) &&
            (false !== ($this->height = $img->get_height()))
        );
    }
452063
final protected function addImageSize($name, $width, $height, $crop = false)
    {
        $this->getWpBridge()->addImageSize($name, $width, $height, $crop);
    }
559998
public function crop($width, $height, $allow_enlarge = false, $position = self::CROPCENTER)
    {
        if (!$allow_enlarge) {
            // this logic is slightly different to resize(),
            // it will only reset dimensions to the original
            // if that particular dimenstion is larger

            if ($width > $this->getSourceWidth()) {
                $width  = $this->getSourceWidth();
            }

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

        $ratio_source = $this->getSourceWidth() / $this->getSourceHeight();
        $ratio_dest = $width / $height;

        if ($ratio_dest < $ratio_source) {
            $this->resizeToHeight($height, $allow_enlarge);

            $excess_width = ($this->getDestWidth() - $width) / $this->getDestWidth() * $this->getSourceWidth();

            $this->source_w = $this->getSourceWidth() - $excess_width;
            $this->source_x = $this->getCropPosition($excess_width, $position);

            $this->dest_w = $width;
        } else {
            $this->resizeToWidth($width, $allow_enlarge);

            $excess_height = ($this->getDestHeight() - $height) / $this->getDestHeight() * $this->getSourceHeight();

            $this->source_h = $this->getSourceHeight() - $excess_height;
            $this->source_y = $this->getCropPosition($excess_height, $position);

            $this->dest_h = $height;
        }

        return $this;
    }
232633
public function cropImage(Image $image, int $width, int $height): Image
    {
        if (true === $image->getEncrypted()) {
            $this->decryptImage($image);
            $encrypt = true;
        } else {
            $encrypt = false;
        }

        $crop = $this->getAdjustedImageCropDimensions($image, $width, $height);
        $centerX = ($image->getWidth() / 2) - ($crop['width'] / 2);
        $centerY = ($image->getHeight() / 2) - ($crop['height'] / 2);
        $tmp = new Image($this->secret, null, $image->getExtension(), $this->autorotate);
        $tmp->create($width, $height);
        imagecopyresampled(
            $tmp->getData(),
            $image->getData(),
            0,
            0,
            (int)$centerX,
            (int)$centerY,
            $width,
            $height,
            $crop['width'],
            $crop['height']
        );
        $image->destroy();
        $image = $tmp;

        if (true === $encrypt) {
            $image = $this->encryptImage($image);
        }
        return $image;
    }
1109089
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;
    }
1305938
public function withDefaultImage(
        $width = null,
        $height = null,
        $options = null)
    {
        $this->withRenamedImage(null, null, $width, $height, $options);

        return $this;
    }
313005
public function resize($width = null, $height = null)
    {
        $this->styleAttributes['width'] = $width . 'px';
        $this->styleAttributes['height'] = $height . 'px';

        return $this;
    }
1052805
public function resize($width, $height = NULL, $resizeMode = NULL)
    {
        if(!$height)
        {
            $height = $width;
        }

        $this->effect[] = new Resize($width, $height, $resizeMode);
    }
825501
public function fit($width, $height, $filename = null)
    {
        $this->fit[] = array('width'=>$width, 'height'=>$height,  'filename'=>$filename);

        return $this;
    }
1114067
public static function resizeImg($image, $width, $height = 0)
    {
        $newImg = self::getImg($image, $width, $height, false, ManipulatorInterface::THUMBNAIL_OUTBOUND);

        if(self::copyImg($newImg, $image)){
            File::delFile($newImg);
        }
    }
1299110
public static function add_resize_configuration($name, $width, $height, $mode = 'auto') {
		$configuration = [
			'width' => $width,
			'height' => $height,
			'mode' => $mode
		];
		self::$resize_configurations[$name] = $configuration;
	}
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);
    }
567513
protected function validateAspectRatio($width, $height)
    {
        /**
         * Allow aspect ratio given?
         */
        if ($this->allowAspectRatioX && $this->allowAspectRatioY) {
            $gcd = $this->gcd($width, $height);
            $x = $width / $gcd;
            $y = $height / $gcd;

            if ($x != $this->allowAspectRatioX || $y != $this->allowAspectRatioY) {
                $this->setErrorMessage(sprintf(
                    $this->messages['aspect_ratio'],
                    $x . ':' . $y,
                    $this->allowAspectRatioX . ':' . $this->allowAspectRatioY
                ));
                return false;
            }
        }

        /**
         * Deny aspect ratio given?
         */
        if ($this->denyAspectRatioX && $this->denyAspectRatioY) {
            $gcd = $this->gcd($width, $height);
            $x = $width / $gcd;
            $y = $height / $gcd;

            if ($x == $this->denyAspectRatioX && $y == $this->denyAspectRatioY) {
                $this->setErrorMessage(sprintf(
                    $this->messages['aspect_ratio_denied'],
                    $x . ':' . $y
                ));
                return false;
            }
        }

        return true;
    }
693954
public function resize($width, $height) {
		$image = imagecreatetruecolor($width, $height);
		imagecopyresampled($image, $this->_image, 0, 0, 0, 0, $width, $height, $this->width, $this->height);
		imagedestroy($this->_image);
		$this->_image = $image;
		return $this;
	}
530394
protected function validateMinWidth($width, Constraint $constraint)
    {
        if ($constraint->minWidth) {
            if (!ctype_digit((string) $constraint->minWidth)) {
                throw new ConstraintDefinitionException(
                    sprintf(
                        '"%s" is not a valid minimum width',
                        $constraint->minWidth
                    )
                );
            }

            if ($width < $constraint->minWidth) {
                $this->context->addViolation(
                    $constraint->minWidthMessage,
                    array(
                        '{{ width }}' => $width,
                        '{{ min_width }}' => $constraint->minWidth
                    )
                );

                return true;
            }
        }
    }
1206072
protected final boolean isValidSize (int width, int height)
    {
        if (width < 0 || height < 0) {
            log.warning("Attempt to add invalid dirty region?!",
                        "size", (width + "x" + height), new Exception());
            return false;

        } else if (width == 0 || height == 0) {
            // no need to complain about zero sized rectangles, just ignore them
            return false;

        } else {
            return true;
        }
    }