ImagemagickDriver::resize

Resize an image using the given new width and height.

@param bool $crop Perform auto crop or not

@return bool
424503
public function resize($crop = false)
    {
        if ($crop) {
            return $this->resizeCrop();
        } else {
            $sourceDimension = $this->sourceImageFile->getDimension();
            $resampleDimension = $this->resampleImageFile->getDimension();

            if (($sourceDimension->getWidth() <= $resampleDimension->getWidth()) && ($sourceDimension->getHeight() <= $resampleDimension->getHeight())) {
                return true;
            } //no resizing needed

            //try max width first...
            $resizeRatio = $resampleDimension->getWidth() / $sourceDimension->getWidth();
            $resizeWidth = $resampleDimension->getWidth();
            $resizeHeight = $sourceDimension->getHeight() * $resizeRatio;

            //if that didn't work
            if ($resizeHeight > $resampleDimension->getHeight()) {
                $resizeRatio = $resampleDimension->getHeight() / $sourceDimension->getHeight();
                $resizeHeight = $resampleDimension->getHeight();
                $resizeWidth = $sourceDimension->getWidth() * $resizeRatio;
            }

            $resampleImageResource =& $this->getResampleImageResource();

            return $resampleImageResource->resizeImage(
                $resizeWidth,
                $resizeHeight,
                \Imagick::FILTER_CATROM, 0.9, true);
        }
    }
1024711
public function resize(int $width, int $height, bool $crop = false): self
    {
        $this->image = $this->resizeImage($this->image, $width, $height, $crop);
        return $this;
    }
1137227
public function resize($new_width, $new_height)
        {
            $dest = imagecreatetruecolor($new_width, $new_height);

            // Transparency fix contributed by Google Code user 'desfrenes'
            imagealphablending($dest, false);
            imagesavealpha($dest, true);

            if(imagecopyresampled($dest, $this->im, 0, 0, 0, 0, $new_width, $new_height, $this->width, $this->height)) {
                $this->im = $dest;
                $this->width = imagesx($this->im);
                $this->height = imagesy($this->im);
                return true;
            }

            return false;
        }
1070279
public function resize($props = array(), $coords = null) {

		$croppable = elgg_extract('croppable', $props, false);

		$width = elgg_extract('w', $props);
		$height = elgg_extract('h', $props);

		if (is_array($coords) && $croppable) {
			$master_width = elgg_extract('master_width', $coords, self::MASTER);
			$master_height = elgg_extract('master_height', $coords, self::MASTER);

			$x1 = elgg_extract('x1', $coords, 0);
			$y1 = elgg_extract('y1', $coords, 0);
			$x2 = elgg_extract('x2', $coords, self::MASTER);
			$y2 = elgg_extract('y2', $coords, self::MASTER);

			// scale to master size and then crop
			$this->source = $this->source->resize($master_width, $master_height, 'inside', 'down')->crop($x1, $y1, $x2 - $x1, $y2 - $y1)->resize($width);
		} else if ($croppable) {
			// crop to icon width and height
			$this->source = $this->source->resize($width, $height, 'outside', 'any')->crop('center', 'center', $width, $height);
		} else {
			$this->source = $this->source->resize($width, $height, 'inside', 'down');
		}

		return $this;
	}
452063
final protected function addImageSize($name, $width, $height, $crop = false)
    {
        $this->getWpBridge()->addImageSize($name, $width, $height, $crop);
    }
327559
public function runCropResize(Image $image, $width, $height)
    {
        list($resize_width, $resize_height) = $this->resolveCropResizeDimensions($image, $width, $height);

        $zoom = $this->getCrop()[2];

        $image->resize($resize_width * $zoom, $resize_height * $zoom, function ($constraint) {
            $constraint->aspectRatio();
        });

        list($offset_x, $offset_y) = $this->resolveCropOffset($image, $width, $height);

        return $image->crop($width, $height, $offset_x, $offset_y);
    }
366223
public function resize($crop = false)
    {
        if ($crop) {
            return $this->resizeCrop();
        } else {
            $sourceDimension = $this->sourceImageFile->getDimension();
            $resampleDimension = $this->resampleImageFile->getDimension();

            if (($sourceDimension->getWidth() <= $resampleDimension->getWidth()) && ($sourceDimension->getHeight() <= $resampleDimension->getHeight())) {
                return true;
            } //no resizing needed

            //try max width first...
            $resizeRatio = $resampleDimension->getWidth() / $sourceDimension->getWidth();
            $resizeWidth = $resampleDimension->getWidth();
            $resizeHeight = $sourceDimension->getHeight() * $resizeRatio;

            //if that didn't work
            if ($resizeHeight > $resampleDimension->getHeight()) {
                $resizeRatio = $resampleDimension->getHeight() / $sourceDimension->getHeight();
                $resizeHeight = $resampleDimension->getHeight();
                $resizeWidth = $sourceDimension->getWidth() * $resizeRatio;
            }

            $resampleImageResource =& $this->getResampleImageResource();

            try {
                $resampleImageResource->resizeimage($resizeWidth, $resizeHeight,
                    \Gmagick::FILTER_CATROM, 0.9, true);

                return true;

            } catch (\GmagickException $e) {

                $this->errors[ $e->getCode() ] = $e->getMessage();

                return false;
            }
        }
    }
477574
public function resizeCrop()
    {
        $sourceDimension = $this->sourceImageFile->getDimension();
        $resampleDimension = $this->resampleImageFile->getDimension();

        if ($resampleDimension->getOrientation() === 'SQUARE') {
            if ($sourceDimension->getOrientation() === 'LANDSCAPE') {
                $sourceSquareSize = $sourceDimension->getHeight();

                $sourceDimension = new Dimension(
                    $sourceSquareSize,
                    $sourceSquareSize,
                    ($sourceDimension->getWidth() - $sourceDimension->getHeight()) / 2,
                    0
                );
            } elseif ($sourceDimension->getOrientation() === 'PORTRAIT') {
                $sourceSquareSize = $sourceDimension->getWidth();

                $sourceDimension = new Dimension(
                    $sourceSquareSize,
                    $sourceSquareSize,
                    0,
                    ($sourceDimension->getHeight() - $sourceDimension->getWidth()) / 2
                );
            }
        } else {

            //try max width first...
            $resizeRatio = $resampleDimension->getWidth() / $sourceDimension->getWidth();
            $resizeWidth = $resampleDimension->getWidth();
            $resizeHeight = $sourceDimension->getHeight() * $resizeRatio;

            //if that didn't work
            if ($resizeHeight > $resampleDimension->getHeight()) {
                $resizeRatio = $resampleDimension->getHeight() / $sourceDimension->getHeight();
                $resizeHeight = $resampleDimension->getHeight();
                $resizeWidth = $sourceDimension->getWidth() * $resizeRatio;
            }

            switch ($resampleDimension->getFocus()) {
                default:
                case 'CENTER':

                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        ($sourceDimension->getWidth() / 2) - ($resizeWidth / 2),
                        ($sourceDimension->getHeight() / 2) - ($resizeHeight / 2)
                    );

                    break;
                case 'NORTH':
                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        ($sourceDimension->getWidth() - $resizeWidth) / 2,
                        0
                    );
                    break;
                case 'NORTHWEST':
                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        0,
                        0
                    );
                    break;
                case 'NORTHEAST':
                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        $sourceDimension->getWidth() - $resizeWidth,
                        0
                    );
                    break;
                case 'SOUTH':
                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        ($sourceDimension->getWidth() - $resizeWidth) / 2,
                        $sourceDimension->getHeight() - $resizeHeight
                    );
                    break;
                case 'SOUTHWEST':
                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        0,
                        $sourceDimension->getHeight() - $resizeHeight
                    );
                    break;
                case 'SOUTHEAST':
                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        $sourceDimension->getWidth() - $resizeWidth,
                        $sourceDimension->getHeight() - $resizeHeight
                    );
                    break;
                case 'WEST':
                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        0,
                        ($sourceDimension->getHeight() - $resizeHeight) / 2
                    );
                    break;
                case 'EAST':
                    $sourceDimension = new Dimension(
                        $resizeWidth,
                        $resizeHeight,
                        $sourceDimension->getWidth() - $resizeWidth,
                        ($sourceDimension->getHeight() - $resizeHeight) / 2
                    );
                    break;
            }
        }

        $resampleAxis = $this->resampleImageFile->getDimension()->getAxis();
        $sourceAxis = $sourceDimension->getAxis();

        if (function_exists('imagecreatetruecolor')) {
            $this->resampleImageResource = imagecreatetruecolor(
                $resampleDimension->getWidth(),
                $resampleDimension->getHeight()
            );

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

            $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127);
            imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(),
                $resampleDimension->getHeight(), $transparent);

            return imagecopyresampled(
                $this->resampleImageResource,
                $this->sourceImageResource,
                $resampleAxis->getX(),
                $resampleAxis->getY(),
                $sourceAxis->getX(),
                $sourceAxis->getY(),
                $resampleDimension->getWidth(),
                $resampleDimension->getHeight(),
                $sourceDimension->getWidth(),
                $sourceDimension->getHeight()
            );
        } else {
            $this->resampleImageResource = imagecreate($resampleDimension->getWidth(),
                $resampleDimension->getHeight());

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

            $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127);
            imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(),
                $resampleDimension->getHeight(), $transparent);

            return imagecopyresized(
                $this->resampleImageResource,
                $this->sourceImageResource,
                $resampleAxis->getX(),
                $resampleAxis->getY(),
                $sourceAxis->getX(),
                $sourceAxis->getY(),
                $resampleDimension->getWidth(),
                $resampleDimension->getHeight(),
                $sourceDimension->getWidth(),
                $sourceDimension->getHeight()
            );
        }
    }
529451
public function setWidth( int $width ) : ImageSizeReducer
   {

      if ( $width <= 0 )
      {
         throw new ArgumentError( 'width', $width, 'Drawing.Image', 'Can not crop an image with an empty width!' );
      }

      $this->data[ 'width' ] = $width;

      return $this;

   }
275320
public static function resize($image, $size)
    {
        if (!$image) {
            return false;
        }

        list($width, $height) = self::parseSize($size);

        if ($height == 'auto' && $width == 'auto') {
            return false;
        }

        $o_wd = imagesx($image);
        $o_ht = imagesy($image);

        $x = $y = 0;
        if ($height == 'auto') {
            $height = round(($width / $o_wd) * $o_ht);
        } elseif ($width == 'auto') {
            $width = round(($height / $o_ht) * $o_wd);
        } elseif ($width && !$height) {
            //make square image
            $height = $width;
            if ($o_wd > $o_ht) {
                $x = ceil(($width - $height) / 2);
                $o_wd = $o_ht;
            } elseif ($o_ht > $o_wd) {
                $y = ceil(($height - $width) / 2);
                $o_ht = $o_wd;
            }
        } else {
            $w = round($o_wd * $height / $o_ht);
            $h = round($o_ht * $width / $o_wd);

            if (($height - $h) < ($width - $w)) {
                $width = &$w;
            } else {
                $height = &$h;
            }
        }

        $tmp = imageCreateTrueColor($width, $height);
        imagesavealpha($tmp, true);
        $color = imagecolorallocatealpha($tmp, 0x00, 0x00, 0x00, 127);
        imagefill($tmp, 0, 0, $color);
        imagecopyresampled($tmp, $image, 0, 0, $x, $y, $width, $height, $o_wd, $o_ht);

        return $tmp;
    }
791220
private function crop($optimal_width, $optimal_height, $new_width, $new_height)
    {
        // Find center - this will be used for the crop
        $crop_start_x = ($optimal_width  / 2) - ($new_width  / 2);
        $crop_start_y = ($optimal_height / 2) - ($new_height / 2);

        $crop = $this->image_resized;

        $dest_offset_x = max(0, -$crop_start_x);
        $dest_offset_y = max(0, -$crop_start_y);
        $crop_start_x  = max(0, $crop_start_x);
        $crop_start_y  = max(0, $crop_start_y);
        $dest_width    = min($optimal_width, $new_width);
        $dest_height   = min($optimal_height, $new_height);

        // Now crop from center to exact requested size
        $this->image_resized = imagecreatetruecolor($new_width, $new_height);

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

        imagefilledrectangle($this->image_resized, 0, 0, $new_width, $new_height,
            imagecolorallocatealpha($this->image_resized, 255, 255, 255, 127)
        );

        imagecopyresampled($this->image_resized, $crop, $dest_offset_x, $dest_offset_y, $crop_start_x, $crop_start_y, $dest_width, $dest_height, $dest_width, $dest_height);

        return true;
    }
674163
public function resize($width, $height, $ratio = false, $interlace = false) {

		$image = ['width' => imagesx($this -> image), 'height' => imagesy($this -> image)];

		//Set width and height
		if($height == 0 && $width > 0) {
			$height = round($image['height'] / ($image['width'] / $width), 0);
		}
		elseif($width == 0 && $height > 0) {
			$width = round($image['width'] / ($image['height'] / $height), 0);
		}

		$x = 0;
		$y = 0;

		//Ratio
		if(true === $ratio) {

			$ratio 		= [$image['width'] / $image['height'], $width / $height];
			$tmp_width 	= $image['width'];
			$tmp_height = $image['height'];

			if($ratio[0] > $ratio[1]) {

				$image['width'] = $image['height'] * $ratio[1];
				$x = ($tmp_width - $image['width']) / 2;
			}
			elseif($ratio[0] < $ratio[1]) {

				$image['height'] = $image['width'] / $ratio[1];
				$y = ($tmp_height - $image['height']) / 2;
			}
		}

		//Resizing
		return $this -> createImage($x, $y, $image['width'], $image['height'], $width, $height, $interlace);
	}
860640
protected function crop($with, $height, $newWidth, $newHeight)
    {
        $cropStartX = ($with / 2) - ($newWidth / 2);
        $cropStartY = ($height / 2) - ($newHeight / 2);
        $crop = $this->imageResized;
        $this->imageResized = imagecreatetruecolor($newWidth, $newHeight);
        imagecopyresampled(
            $this->imageResized,
            $crop,
            0,
            0,
            $cropStartX,
            $cropStartY,
            $newWidth,
            $newHeight,
            $newWidth,
            $newHeight
        );

        return $this;
    }
973922
public function resize($width, $height)
    {

        $new = imagecreatetruecolor($width, $height);
        imagealphablending($new, false);
        imagesavealpha($new, true);
        imagecopyresampled($new, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height);

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

        return $this;

    }
1015164
public static function resizeImage($file, $width, $height, $desFile = '')
    {
        try {
            if (is_file($file)) {
                if (!$desFile) {
                    $desFile = $file;
                }
                Image::open($file)->resize($width, $height)->save($desFile);
                return true;
            }
        } catch (Exception $e) {
        }
        return false;
    }
260761
public function reSize($width = 0, $height = 0)
    {
        $this->gdExtensionCheck();
        $dosya = $this->getFilePath();
        list($genislik, $yukseklik) = getimagesize($dosya);


        if (strstr($width, "%")) {
            $width = str_replace("%", "", $width);
            $bolw = (100 / $width);
            $width = ($genislik / $bolw);
        }
        if (strstr($height, "%")) {
            $height = str_replace("%", "", $height);
            $bolh = (100 / $height);
            $height = ($yukseklik / $bolh);
        }
        $hedef = imagecreatetruecolor($width, $height);


        switch ($this->getExt()) {
            case 'png':
                $kaynak = imagecreatefrompng($dosya);
                unlink($dosya);
                imagecopyresampled($hedef, $kaynak, 0, 0, 0, 0, $width, $height, $genislik, $yukseklik);
                imagepng($hedef, $dosya, 100);
                break;
            case 'jpg':
                $kaynak = imagecreatefromjpeg($dosya);
                unlink($dosya);
                imagecopyresampled($hedef, $kaynak, 0, 0, 0, 0, $width, $height, $genislik, $yukseklik);
                imagejpeg($hedef, $dosya, 100);
                break;
            case 'gif':
                $kaynak = imagecreatefromgif($dosya);
                unlink($dosya);
                imagecopyresampled($hedef, $kaynak, 0, 0, 0, 0, $width, $height, $genislik, $yukseklik);
                imagegif($hedef, $dosya, 100);
                break;
        }
        imagedestroy($hedef);
        imagedestroy($kaynak);

        return $this;
    }
1167238
public function resizeAndCrop($targetWidth, $targetHeight)
    {
        if ($this->getAutoOrient()) {
            $this->autoOrient();
        }

        // First get the size that we can use to safely trim down the image without cropping any sides
        $crop = $this->getSafeResizeOffset($this->originalImage, $targetWidth, $targetHeight);
        // Resize the image
        $this->originalImage->resizeImage($crop['width'], $crop['height'], $this->getFilter(), $this->getBlur());
        // Get the offset for cropping the image further
        $offset = $this->getSpecialOffset($this->originalImage, $targetWidth, $targetHeight);
        // Crop the image
        $this->originalImage->cropImage($targetWidth, $targetHeight, $offset['x'], $offset['y']);

        return $this->originalImage;
    }