Pimcore\Image\Adapter\Imagick::resize PHP Method

resize() public method

public resize ( $width, $height ) : self
$width
$height
return self
    public function resize($width, $height)
    {
        $this->preModify();
        // this is the check for vector formats because they need to have a resolution set
        // this does only work if "resize" is the first step in the image-pipeline
        if ($this->isVectorGraphic()) {
            // the resolution has to be set before loading the image, that's why we have to destroy the instance and load it again
            $res = $this->resource->getImageResolution();
            $x_ratio = $res['x'] / $this->getWidth();
            $y_ratio = $res['y'] / $this->getHeight();
            $this->resource->removeImage();
            $newRes = ["x" => $width * $x_ratio, "y" => $height * $y_ratio];
            // only use the calculated resolution if we need a higher one that the one we got from the metadata (getImageResolution)
            // this is because sometimes the quality is much better when using the "native" resolution from the metadata
            if ($newRes["x"] > $res["x"] && $newRes["y"] > $res["y"]) {
                $this->resource->setResolution($newRes["x"], $newRes["y"]);
            } else {
                $this->resource->setResolution($res["x"], $res["y"]);
            }
            $this->resource->readImage($this->imagePath);
            $this->setColorspaceToRGB();
        }
        $width = (int) $width;
        $height = (int) $height;
        $this->resource->resizeimage($width, $height, \Imagick::FILTER_UNDEFINED, 1, false);
        $this->setWidth($width);
        $this->setHeight($height);
        $this->postModify();
        return $this;
    }

Usage Example

 /**
  * @param $width
  * @param $height
  * @return $this|Imagick
  */
 public function resize($width, $height)
 {
     if (!$this->isOriginal || !$this->isSvg()) {
         return parent::resize($width, $height);
     }
     $width = (int) $width;
     $height = (int) $height;
     $tmpFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/" . uniqid() . "_pimcore_image_svg_resize_tmp_file.png";
     $this->tmpFiles[] = $tmpFile;
     Console::exec(self::getBinary() . " -w " . $width . " -h " . $height . " -D -f " . $this->imagePath . " -e " . $tmpFile);
     $this->initImagick($tmpFile);
     return $this;
 }