Create resized version for the image
@param ImageSize $sourceImageSize
@param integer $targetWidth
@param integer $targetHeight
@param boolean $cropped
@param integer $quality
@param boolean $force
@return string
769513
public function createCroppedImageVariant(ImageSize $sourceImageSize, $targetWidth, $targetHeight, $cropped = false, $quality = 95, $force = false)
	{
		// validate params
		if ( ! $sourceImageSize instanceof ImageSize) {
			throw new Exception\RuntimeException('ImageSize entity expected');
		}
		if (($targetWidth <= 0) || ($targetHeight <= 0)) {
			throw new Exception\RuntimeException('Dimensions are invalid');
		}
		
		if ( ! $sourceImageSize->isCropped()) {
			//throw new Exception\RuntimeException('Cannot resize non-cropped images with this metod');
		}
		
		$image = $sourceImageSize->getMaster();

		$sizeName = $this->getImageSizeNameForResizedCrop($targetWidth, $targetHeight, $sourceImageSize->getTargetWidth(), $sourceImageSize->getTargetHeight(), $sourceImageSize->getCropLeft(), $sourceImageSize->getCropTop(), $sourceImageSize->getCropWidth(), $sourceImageSize->getCropHeight(), $cropped);
			
		if ( ! $this->fileExists($image)) {
			$this->log()->warn("Image '{$image->getFileName()}' is missing in the filesystem, tried to resize cropped variant to {$sizeName}");

			return $sizeName;
		}
		
		$size = $image->getImageSize($sizeName);

		if (($size->getTargetHeight() == $targetHeight)
				&& ($size->getTargetWidth() == $targetWidth)
				&& ($size->getQuality() == $quality)
				&& ($size->getCropMode() == $cropped)
				&& empty($force)
		) {
			// nothing to update
			return $sizeName;
		}
		
		$size->setCropTop($sourceImageSize->getCropTop());
		$size->setCropLeft($sourceImageSize->getCropLeft());
		$size->setCropWidth($sourceImageSize->getCropWidth());
		$size->setCropHeight($sourceImageSize->getCropHeight());
		
		$size->setCropSourceWidth($sourceImageSize->getWidth());
		$size->setCropSourceHeight($sourceImageSize->getHeight());
	
		$size->setQuality($quality);
		$size->setCropMode($cropped);
		$size->setTargetWidth($targetWidth);
		$size->setTargetHeight($targetHeight);

		$variantFileName = $this->getImagePath($image, $sourceImageSize->getName());

		if ( ! file_exists($variantFileName)) {
			$this->log()->warn("Image cropped variant file {$variantFileName} is missing in the filesystem");

			return $sizeName;
		}
		
		$resizer = $this->getImageResizer();
		
		$resizer->setSourceFile($variantFileName)
				->setOutputQuality($quality)
				->setTargetWidth($targetWidth)
				->setTargetHeight($targetHeight)
				->setCropMode($cropped);

		$expectedSize = $resizer->getExpectedSize();
		$size->setWidth($expectedSize['width']);
		$size->setHeight($expectedSize['height']);

		$resizedFileDir = $this->getFilesystemDir($image)
				. self::RESERVED_DIR_SIZE . DIRECTORY_SEPARATOR
				. $size->getFolderName();

		if ( ! file_exists($resizedFileDir)) {
			$mkdirResult = mkdir($resizedFileDir, $this->folderAccessMode, true);
			if (empty($mkdirResult)) {
				throw new Exception\RuntimeException(
						'Could not create directory for resized image');
			}
		}

		$resizedFilePath = $resizedFileDir . DIRECTORY_SEPARATOR . $image->getFileName();
		$resizer->setOutputFile($resizedFilePath);
		$resizer->process();

		$entityManager = $this->getDoctrineEntityManager();

		$entityManager->persist($size);
		
		$size->setCropSourceWidth($sourceImageSize->getWidth());
		$size->setCropSourceHeight($sourceImageSize->getHeight());
		
		$entityManager->flush();

		return $sizeName;
	}
1250398
private function _resize(int $width, int $height, bool $resample)
    {
        $this->check();

        $src_w = $this->image_width;
        $src_h = $this->image_height;

        $xscale = $src_w / $width;
        $yscale = $src_h / $height;

        if ($yscale > $xscale) {
            $dst_w = (int)round($src_w * (1 / $yscale));
            $dst_h = (int)round($src_h * (1 / $yscale));
        } else {
            $dst_w = (int)round($src_w * (1 / $xscale));
            $dst_h = (int)round($src_h * (1 / $xscale));
        }

        $res = imagecreatetruecolor($dst_w, $dst_h);

        if ($this->image_type == 3) {
            imagealphablending($res, false);
            imagesavealpha($res, true);
        }
        if ($resample) {
            imagecopyresampled($res, $this->image_resource, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
        } else {
            imagecopyresized($res, $this->image_resource, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
        }

        imagedestroy($this->image_resource);

        $this->image_resource = $res;
        $this->image_width = $dst_w;
        $this->image_height = $dst_h;

        return $this;
    }
515042
protected function getSpecialOffset(\Imagick $original, $targetWidth, $targetHeight)
    {
        return $this->getRandomEdgeOffset($original, $targetWidth, $targetHeight);
    }
388795
protected function getCenterOffset(\Imagick $image, $targetWidth, $targetHeight)
    {
        $size = $image->getImageGeometry();
        $originalWidth = $size['width'];
        $originalHeight = $size['height'];
        $goalX = (int) (($originalWidth-$targetWidth)/2);
        $goalY = (int) (($originalHeight-$targetHeight)/2);

        return array('x' => $goalX, 'y' => $goalY);
    }
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;
    }
106270
public function resize_image($w, $h, $crop = false)
    {
        list($width, $height) = getimagesize($this->file);
        $r = $width / $height;
        if ($crop) {
            if ($width > $height) {
                $width = ceil($width - ($width * abs($r - $w / $h)));
            } else {
                $height = ceil($height - ($height * abs($r - $w / $h)));
            }
            $newwidth = $w;
            $newheight = $h;
        } else {
            if ($w / $h > $r) {
                $newwidth = $h * $r;
                $newheight = $h;
            } else {
                $newheight = $w / $r;
                $newwidth = $w;
            }
        }
        $dst = imagecreatetruecolor($newwidth, $newheight);
        imagecopyresampled($dst, $this->image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        $this->image = $dst;
    }
887738
protected function calculateDimensions($originalWidth, $originalHeight)
	{
		// check if target size is set and valid
		if (empty($this->targetWidth) || ($this->targetWidth <= 0)) {
			throw new ImageProcessorException('Target width is not set or is invalid');
		}
		if (empty($this->targetHeight) || ($this->targetHeight <= 0)) {
			throw new ImageProcessorException('Target height is not set or is invalid');
		}

		$newDimensions = $this->getExpectedSize(false);
		
		$dimensions = array();
		
		// set default dimensions for image-to-image copy
		$dimensions['sourceLeft'] = null;
		$dimensions['sourceTop'] = null;
		$dimensions['sourceWidth'] = null;
		$dimensions['sourceHeight'] = null;
		
		$dimensions['destWidth'] = max(round($newDimensions['width']), 1);
		$dimensions['destHeight'] = max(round($newDimensions['height']), 1);

		// get ratios 
		$wRatio = max($originalWidth / $newDimensions['width'], 1);
		$hRatio = max($originalHeight / $newDimensions['height'], 1);

		if ( ! $this->cropMode) {
			$wRatio = $hRatio = max($wRatio, $hRatio);
		} else {
			$wRatio = $hRatio = min($wRatio, $hRatio);
		}

		// set source dimensions to center (with target aspect ratio)
		$sourceHeight = $newDimensions['height'] * $hRatio;
		$dimensions['sourceTop'] =
				round(($originalHeight - $sourceHeight) / 2);
		$dimensions['sourceHeight'] = round($sourceHeight);

		$sourceWidth = $newDimensions['width'] * $wRatio;
		$dimensions['sourceLeft'] =
				round(($originalWidth - $sourceWidth) / 2);
		$dimensions['sourceWidth'] = round($sourceWidth);

		return $dimensions;
	}
543191
public function getExpectedSize($round = true)
	{
		if (empty($this->sourceFilename)) {
			throw new ImageProcessorException('Source image is not set');
		}
		if (empty($this->targetWidth) || ($this->targetWidth <= 0)) {
			throw new ImageProcessorException('Target width is not set or is invalid');
		}
		if (empty($this->targetHeight) || ($this->targetHeight <= 0)) {
			throw new ImageProcessorException('Target height is not set or is invalid');
		}

		$imageInfo = $this->getImageInfo($this->sourceFilename);

		$wRatio = max($imageInfo->getWidth() / $this->targetWidth, 1);
		$hRatio = max($imageInfo->getHeight() / $this->targetHeight, 1);

		if ( ! $this->cropMode) {
			$wRatio = $hRatio = max($wRatio, $hRatio);
		}

		$dimensions = array(
			'width' => $imageInfo->getWidth() / $wRatio,
			'height' => $imageInfo->getHeight() / $hRatio,
		);

		if ($round) {
			$dimensions = array_map('round', $dimensions);
		}

		return $dimensions;
	}
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;
    }
1168856
public function cropAndResize($filePath, $x, $y, $h, $w, array $format)
    {
        $image = $this->imagickFactory->create($filePath);
        $image->cropImage($w, $h, $x, $y);
        $image = $this->resizeImage($format, $image);

        $pathInfo = pathinfo($filePath);
        $croppedFilePath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR . time() . $pathInfo['basename'];

        $this->saveImage($croppedFilePath, $image, $format['compression_quality']);

        return $croppedFilePath;
    }
638727
private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight)
        {
            $cropPoints = $this->getCropPoints($optimalWidth, $optimalHeight, $newWidth, $newHeight);

            // Find center - this will be used for the crop.
            $cropStartX = $cropPoints['x'];
            $cropStartY = $cropPoints['y'];

            $crop = $this->imageResized;

            $destOffsetX        = max(0, -$cropStartX);
            $destOffsetY        = max(0, -$cropStartY);
            $cropStartX         = max(0, $cropStartX);
            $cropStartY         = max(0, $cropStartY);
            $destWidth          = min($optimalWidth, $newWidth);
            $destHeight         = min($optimalHeight, $newHeight);

            // Now crop from center to exact requested size.
            $this->imageResized = imagecreatetruecolor($newWidth, $newHeight);

            imagealphablending($crop, true);
            imagealphablending($this->imageResized, false);
            imagesavealpha($this->imageResized, true);

            imagefilledrectangle(
                $this->imageResized,
                0,
                0,
                $newWidth,
                $newHeight,
                imagecolorallocatealpha(
                    $this->imageResized,
                    255,
                    255,
                    255,
                    127
               )
           );

            imagecopyresampled($this->imageResized, $crop, $destOffsetX, $destOffsetY, $cropStartX, $cropStartY, $destWidth, $destHeight, $destWidth, $destHeight);

            return true;
        }
1151177
protected function getSafeResizeOffset(\Imagick $image, $targetWidth, $targetHeight)
    {
        $source = $image->getImageGeometry();
        if (0 == $targetHeight || ($source['width'] / $source['height']) < ($targetWidth / $targetHeight)) {
            $scale = $source['width'] / $targetWidth;
        } else {
            $scale = $source['height'] / $targetHeight;
        }

        return array('width' => (int) ($source['width'] / $scale), 'height' => (int) ($source['height'] / $scale));
    }
993809
public function cropTransparentImage($imageResource)
    {
        /** @var int $imageTransparency Transparent color */
        $imageTransparency = imagecolorallocatealpha($imageResource, 255, 255, 255, 127);
        /** @var resource $croppedImage Cropped image resource */
        $croppedImage = imagecreatetruecolor($_POST['crop_width'], $_POST['crop_height']);
        // Fill new image with transparent color
        imagefill($croppedImage, 0, 0, $imageTransparency);
        // Save Alpha chanel
        imagesavealpha($croppedImage, true);
        /** @var resource $rotatedImage Rotated image resource */
        $rotatedImage = imagerotate($imageResource, -($_POST['rotate']), $imageTransparency);
        // Copy rotated image to cropped one
        imagecopy(
            $croppedImage,
            $rotatedImage,
            0,
            0,
            $_POST['crop_x'],
            $_POST['crop_y'],
            $_POST['crop_width'],
            $_POST['crop_height']
        );
        // Delete temp image resource
        imagedestroy($rotatedImage);
        // Return result image
        return $croppedImage;
    }
1047761
public static function resizeImage($image, $newWidth, $newHeight, $autoRotate = false, $mode = ImageTools::MODE_SCALE_CROP, $backgroundColor = 0x00000000)
    {
        $sourceWidth = imagesx($image);
        $sourceHeight = imagesy($image);

        $sourcePar = $sourceWidth / $sourceHeight;
        $newPar = $newWidth / $newHeight;
        $sourceX = $newX = 0;
        $sourceY = $newY = 0;

        if ($autoRotate && (($sourcePar > 1 && $newPar < 1) || ($sourcePar < 1 && $newPar > 1))) {
            $t = $newWidth;
            $newWidth = $newHeight;
            $newHeight = $t;
            $newPar = $newWidth / $newHeight;
        }

        $targetWidth = $newWidth;
        $targetHeight = $newHeight;

        // MODE_EXACT gets handled through default variables

        if ($mode == ImageTools::MODE_CROP) {
            if ($sourceWidth > $newWidth) {
                $sourceX = ($sourceWidth - $newWidth) >> 1;
                $sourceWidth = $newWidth;
            } else {
                $targetWidth = $sourceWidth;
                $newX = ($newWidth - $sourceWidth) >> 1;
            }

            if ($sourceHeight > $newHeight) {
                $sourceY = ($sourceHeight - $newHeight) >> 1;
                $sourceHeight = $newHeight;
            } else {
                $targetHeight = $sourceHeight;
                $newY = ($newHeight - $sourceHeight) >> 1;
            }

        } elseif ($mode == ImageTools::MODE_SCALE_CROP) {
            if ($sourcePar > $newPar) {
                $orgWidth = $sourceWidth;
                $sourceWidth = $sourceHeight * $newPar;
                $sourceX = ($orgWidth - $sourceWidth) >> 1;
            } else {
                $orgHeight = $sourceHeight;
                $sourceHeight = $sourceWidth / $newPar;
                $sourceY = ($orgHeight - $sourceHeight) >> 1;
            }

        } elseif ($mode == ImageTools::MODE_SCALE_PAD) {
            if ($sourcePar > $newPar) {
                $targetHeight = $targetWidth / $sourcePar;
                $newY = ($newHeight - $targetHeight) >> 1;
            } else {
                $targetWidth = $targetHeight * $sourcePar;
                $newX = ($newWidth - $targetWidth) >> 1;
            }
        } elseif ($mode == ImageTools::MODE_FIT) {
            if ($sourcePar > $newPar) {
                $targetHeight = $newHeight = round($newWidth / $sourcePar);
            } else {
                $targetWidth = $newWidth = round($newHeight * $sourcePar);
            }
        }

        $new = imagecreatetruecolor($newWidth, $newHeight);

        imagealphablending($new, false);

        $backgroundColor = self::colorHexToRgba($backgroundColor);
        $backgroundColor = imagecolorallocatealpha($new, $backgroundColor['r'], $backgroundColor['g'], $backgroundColor['b'], 127 - floor($backgroundColor['a'] / 2));

        imagefilledrectangle($new, 0, 0, $newWidth, $newHeight, $backgroundColor);

        imagesavealpha($new, true);

        imagecopyresampled($new, $image, $newX, $newY, $sourceX, $sourceY, $targetWidth, $targetHeight, $sourceWidth, $sourceHeight);

        return $new;
    }
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;
    }
976838
public function copyFromWithResize(Image $srcImage, $srcX = 0, $srcY = 0, $x = 0, $y = 0, $srcWidth = 0, $srcHeight = 0, $width = 0, $height = 0) {
		if (empty($srcWidth)) {
			$srcWidth = $srcImage->getWidth();
		}
		if (empty($srcHeight)) {
			$srcHeight = $srcImage->getHeight();
		}
		if (empty($width)) {
			$width = $this->getWidth();
		}
		if (empty($height)) {
			$height = $this->getHeight();
		}
		return imagecopyresized($this->image, $srcImage->image, $x, $y, $srcX, $srcY, $width, $height, $srcWidth, $srcHeight);
	}
443819
public function resizeImage($newWidth, $newHeight, $option = 0, $sharpen = false, $autoRotate = false)
		# Author:     Jarrod Oberto
		# Date:       27-02-08
		# Purpose:    Resizes the image
		# Param in:   $newWidth:
		#             $newHeight:
		#             $option:     0 / exact = defined size;
		#                          1 / portrait = keep aspect set height;
		#                          2 / landscape = keep aspect set width;
		#                          3 / auto = auto;
		#                          4 / crop= resize and crop;
		#
		#         $option can also be an array containing options for
		#         cropping. E.G., array('crop', 'r')
		#
		#         This array only applies to 'crop' and the 'r' refers to
		#         "crop right". Other value include; tl, t, tr, l, m (default),
		#         r, bl, b, br, or you can specify your own co-ords (which
		#         isn't recommended.
		#
		#       $sharpen:    true: sharpen (jpg only);
		#                false: don't sharpen
		# Param out:  n/a
		# Reference:
		# Notes:      To clarify the $option input:
		#               0 = The exact height and width dimensions you set.
		#               1 = Whatever height is passed in will be the height that
		#                   is set. The width will be calculated and set automatically
		#                   to a the value that keeps the original aspect ratio.
		#               2 = The same but based on the width. We try make the image the
		#                  biggest size we can while stil fitting inside the box size
		#               3 = Depending whether the image is landscape or portrait, this
		#                   will automatically determine whether to resize via
		#                   dimension 1,2 or 0
		#               4 = Will resize and then crop the image for best fit
		#
		#       forceStretch can be applied to options 1,2,3 and 4
		#
	{

		// *** We can pass in an array of options to change the crop position
		$cropPos = 'm';
		if (is_array($option) && fix_strtolower($option[0]) == 'crop')
		{
			$cropPos = $option[1];         # get the crop option
		}
		else
		{
			if (strpos($option, '-') !== false)
			{
				// *** Or pass in a hyphen seperated option
				$optionPiecesArray = explode('-', $option);
				$cropPos = end($optionPiecesArray);
			}
		}

		// *** Check the option is valid
		$option = $this->prepOption($option);

		// *** Make sure the file passed in is valid
		if ( ! $this->image)
		{
			if ($this->debug)
			{
				throw new Exception('file ' . $this->getFileName() . ' is missing or invalid');
			}
			else
			{
				throw new Exception();
			}
		};

		// *** Get optimal width and height - based on $option
		$dimensionsArray = $this->getDimensions($newWidth, $newHeight, $option);

		$optimalWidth = $dimensionsArray['optimalWidth'];
		$optimalHeight = $dimensionsArray['optimalHeight'];

		// *** Resample - create image canvas of x, y size
		$this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
		$this->keepTransparancy($optimalWidth, $optimalHeight, $this->imageResized);
		imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);


		// *** If '4', then crop too
		if ($option == 4 || $option == 'crop')
		{

			if (($optimalWidth >= $newWidth && $optimalHeight >= $newHeight))
			{
				$this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight, $cropPos);
			}
		}

		// *** If Rotate.
		if ($autoRotate)
		{

			$exifData = $this->getExif(false);
			if (count($exifData) > 0)
			{

				switch ($exifData['orientation'])
				{
					case 8:
						$this->imageResized = imagerotate($this->imageResized, 90, 0);
						break;
					case 3:
						$this->imageResized = imagerotate($this->imageResized, 180, 0);
						break;
					case 6:
						$this->imageResized = imagerotate($this->imageResized, -90, 0);
						break;
				}
			}
		}

		// *** Sharpen image (if jpg and the user wishes to do so)
		if ($sharpen && in_array($this->fileExtension, $this->sharpenArray))
		{

			// *** Sharpen
			$this->sharpen();
		}
	}