Takes either file path or ImageInterface. In case of file path, creates an instance of ImageInterface from it.

@param string|resource|ImageInterface $image
@return ImageInterface
@throws \yii\base\InvalidParamException
@since 2.1.0
489578
protected static function ensureImageInterfaceInstance($image)
    {
        if ($image instanceof ImageInterface) {
            return $image;
        }

        if (is_resource($image)) {
            return static::getImagine()->read($image);
        }

        if (is_string($image)) {
            return static::getImagine()->open(Yii::getAlias($image));
        }

        throw new InvalidParamException('File should be either ImageInterface, resource or a string containing file path.');
    }
1193513
public function dispatchImageOnUploadEvent(ImageInterface $image)
    {
        $imageUploadedEvent = new ImageUploadedEvent($image);

        $this->eventDispatcher->dispatch(
            ElcodiMediaEvents::IMAGE_ONUPLOAD,
            $imageUploadedEvent
        );

        return $this;
    }
389969
private function init($image)
    {
        /** @var ConfigModel $settings */
        $settings = ImagerService::getConfig();

        if (\is_string($image)) {
            if (strncmp($image, $settings->imagerUrl, \strlen($settings->imagerUrl)) === 0) {
                // Url to a file that is in the imager library
                $this->getPathsForLocalImagerFile($image);
            } else {
                if (strncmp($image, '//', 2) === 0) {
                    // Protocol relative url, add https
                    $image = 'https:'.$image;
                }

                if (strncmp($image, 'http', 4) === 0 || strncmp($image, 'https', 5) === 0) {
                    // External url
                    $this->type = 'remoteurl';
                    $this->getPathsForUrl($image);
                } else {
                    // Relative path, assume that it's relative to document root
                    $this->getPathsForLocalFile($image);
                }
            }
        } else {
            // It's some kind of model
            if (\get_class($image) === 'aelvan\\imager\\models\\CraftTransformedImageModel') {
                $this->getPathsForLocalImagerFile($image->url);
            } else {
                if ($image instanceof Asset) {
                    $this->asset = $image;

                    // todo : Improve this check to take into account other volumes that are local?
                    try {
                        $volumeClass = \get_class($image->getVolume());
                    } catch (InvalidConfigException $e) {
                        Craft::error($e->getMessage(), __METHOD__);
                        throw new ImagerException($e->getMessage(), $e->getCode(), $e);
                    }

                    if ($volumeClass === 'craft\volumes\Local') {
                        $this->getPathsForLocalAsset($image);
                    } else {
                        $this->type = 'volume';
                        $this->getPathsForVolumeAsset($image);
                    }
                } else {
                    throw new ImagerException(Craft::t('imager', 'An unknown image object was used.'));
                }
            }
        }
    }
69263
public function setImage(\Core\Entity\Image $image = null) {
        $this->image = $image;

        return $this;
    }
120604
public function updateFile(ImagineImageInterface $image)
    {
        if ($this->angle !== null && is_int($this->angle)) {
            $transformation = new Transformation();
            $transformation->rotate($this->angle, null);
            $transformation
                ->apply($image)
                ;
        }

        return $image;
    }
798325
public function load(ImageInterface $image, array $options = [])
    {
        $angle = isset($options['angle']) ? (int) $options['angle'] : 0;

        return 0 === $angle ? $image : $image->rotate($angle);
    }
365804
public function loadImage($image)
    {
        $imageLibrary = ucfirst($this->imageLibrary);
        $functionName = 'loadWith' . $imageLibrary;
        $this->image = $image;

        if (!method_exists($this, $functionName)) {
            throw new IcrRuntimeException('Wrong image library name provided!');
        }

        return call_user_func([$this, $functionName]);
    }
13806
public function updateFile(ImagineImageInterface $image)
    {
        if ($this->direction !== null) {
            $transformation = new Transformation();

            if ($this->direction == 'x') {
                $transformation->flipHorizontally();
            } elseif ($this->direction == 'y') {
                $transformation->flipVertically();
            }

            $transformation->apply($image);
        }

        return $image;
    }
1360417
protected function resize(ImagineImageInterface $image, $mode = ImageInterface::RATIOMODE_INSET, $filter = ImagineImageInterface::FILTER_UNDEFINED)
    {
        if ($mode !== ImageInterface::RATIOMODE_INSET &&
            $mode !== ImageInterface::RATIOMODE_OUTBOUND
        ) {
            throw new \InvalidArgumentException('Invalid mode specified');
        }

        $imageSize = $image->getSize();
        $requestedDimensions = $this->calculateDimensions($imageSize);

        $image->strip();

        $resizeDimensions = $requestedDimensions;
        if ($mode === ImageInterface::RATIOMODE_OUTBOUND) {
            $resizeDimensions = $this->calculateOutboundScalingDimensions($imageSize, $requestedDimensions);
        }

        $image->resize($resizeDimensions, $filter);

        if ($mode === ImageInterface::RATIOMODE_OUTBOUND) {
            $image->crop(new Point(
                max(0, round(($resizeDimensions->getWidth() - $requestedDimensions->getWidth()) / 2)),
                max(0, round(($resizeDimensions->getHeight() - $requestedDimensions->getHeight()) / 2))
            ), $requestedDimensions);
        }

        return $image;
    }
914111
public function execute($image)
    {
        $width = $this->argument(0)->type('digit')->required()->value();
        $height = $this->argument(1)->type('digit')->required()->value();
        $x = $this->argument(2)->type('digit')->value();
        $y = $this->argument(3)->type('digit')->value();

        if (is_null($width) || is_null($height)) {
            throw new \Intervention\Image\Exception\InvalidArgumentException(
                "Width and height of cutout needs to be defined."
            );
        }

        $cropped = new Size($width, $height);
        $position = new Point($x, $y);

        // align boxes
        if (is_null($x) && is_null($y)) {
            $position = $image->getSize()->align('center')->relativePosition($cropped->align('center'));
        }

        // crop image core
        return $this->modify($image, 0, 0, $position->x, $position->y, $cropped->width, $cropped->height, $cropped->width, $cropped->height);
    }
66694
public function dispatchImagePreUploadEvent(ImageInterface $image)
    {
        $imageUploadedEvent = new ImageUploadedEvent($image);

        $this->eventDispatcher->dispatch(
            ElcodiMediaEvents::IMAGE_PREUPLOAD,
            $imageUploadedEvent
        );

        return $this;
    }
491472
public function open($image) {
		if (!file_exists($image)) {
			throw new \RuntimeException(sprintf('File `%s` does not exist!', $image));
		}
		$this->_image = $this->imagine()->open($image);
		return $this;
	}
853175
private function getPathsForLocalAsset(Asset $image)
    {
        /** @var LocalVolumeInterface $volume */
        try {
            $volume = $image->getVolume();
            $this->transformPath = ImagerHelpers::getTransformPathForAsset($image);
        } catch (InvalidConfigException $e) {
            Craft::error($e->getMessage(), __METHOD__);
            throw new ImagerException($e->getMessage(), $e->getCode(), $e);
        }

        $this->path = FileHelper::normalizePath($volume->getRootPath().'/'.$image->folderPath);
        $this->url = $image->getUrl();
        $this->filename = $image->getFilename();
        $this->basename = $image->getFilename(false);
        $this->extension = $image->getExtension();
    }
19700
protected function render(ImageInterface $image)
    {
        $this->response->make($image);
        $image->close();

        return $this->response->getResponse();
    }
639376
public static function hydrateImageInfo(ImageInterface $image, $path)
    {
        $imageSize = getimagesize($path);
        $finfo     = new \finfo(FILEINFO_MIME_TYPE);

        $image->setMimeType($finfo->file($path));

        if ($image->getMimeType() == 'text/html') {
            $image->setMimeType('image/svg+xml');
        }

        $image->setExtension((new MimeType)->extensionToMimeType($image->getMimeType()));
        $image->setWeight(filesize($path));
        $image->setWidth($imageSize[0]);
        $image->setHeight($imageSize[1]);

        return $image;
    }
1187591
public function getImageSource(ImageInterface $image)
    {
        $path = $this->getAbsolutePath($image->getPath());

        if (!is_file($path)) {
            throw new IOException(sprintf('Not found image source "%s"', $image->getId()));
        }

        return file_get_contents($path);
    }
87580
public function apply(ImageInterface $image)
    {

        //        1        2       3      4         5            6           7          8
        //
        //        888888  888888      88  88      8888888888  88                  88  8888888888
        //        88          88      88  88      88  88      88  88          88  88      88  88
        //        8888      8888    8888  8888    88          8888888888  8888888888          88
        //        88          88      88  88
        //        88          88  888888  888888


        switch ($this->orientation) {
            case 2:
                $image->flipHorizontally();
                break;
            case 3:
                $image->rotate(180);
                break;
            case 4:
                $image->flipVertically();
                break;
            case 5:
                $image->rotate(90)->flipHorizontally();
                break;
            case 6:
                $image->rotate(90);
                break;
            case 7:
                $image->rotate(-90)->flipHorizontally();
                break;
            case 8:
                $image->rotate(-90);
                break;
            default:
                break;
        }

        // after rotating the image, we need to reset orientation in Exif data to 1
        if ($image instanceof \Imagine\Imagick\Image) {
            $image->getImagick()->setImageOrientation(1);
        } else {
            // TODO: how can this be done in GD and Gmagick?
            // Workaround: delete all metadata
            $image->strip();
        }
        return $image;
    }