Elgg\ImageService::resize PHP Method

resize() public method

Crop and resize an image
public resize ( string $source, string $destination = null, array $params = [] ) : boolean
$source string Path to source image
$destination string Path to destination If not set, will modify the source image
$params array An array of cropping/resizing parameters - INT 'w' represents the width of the new image With upscaling disabled, this is the maximum width of the new image (in case the source image is smaller than the expected width) - INT 'h' represents the height of the new image With upscaling disabled, this is the maximum height - INT 'x1', 'y1', 'x2', 'y2' represent optional cropping coordinates. The source image will first be cropped to these coordinates, and then resized to match width/height parameters - BOOL 'square' - square images will fill the bounding box (width x height). In Imagine's terms, this equates to OUTBOUND mode - BOOL 'upscale' - if enabled, smaller images will be upscaled to fit the bounding box.
return boolean
    public function resize($source, $destination = null, array $params = [])
    {
        if (!isset($destination)) {
            $destination = $source;
        }
        try {
            $image = $this->imagine->open($source);
            $width = $image->getSize()->getWidth();
            $height = $image->getSize()->getHeight();
            $params = $this->normalizeResizeParameters($width, $height, $params);
            $max_width = elgg_extract('w', $params);
            $max_height = elgg_extract('h', $params);
            $x1 = (int) elgg_extract('x1', $params, 0);
            $y1 = (int) elgg_extract('y1', $params, 0);
            $x2 = (int) elgg_extract('x2', $params, 0);
            $y2 = (int) elgg_extract('y2', $params, 0);
            if ($x2 > $x1 && $y2 > $y1) {
                $crop_start = new Point($x1, $y1);
                $crop_size = new Box($x2 - $x1, $y2 - $y1);
                $image->crop($crop_start, $crop_size);
            }
            $target_size = new Box($max_width, $max_height);
            $thumbnail = $image->resize($target_size);
            $thumbnail->save($destination, ['jpeg_quality' => elgg_extract('jpeg_quality', $params, self::JPEG_QUALITY)]);
            unset($image);
            unset($thumbnail);
        } catch (Exception $ex) {
            _elgg_services()->logger->error($ex->getMessage());
            return false;
        }
        return true;
    }