Get watermark image
@param ImagineInterface $imagine
@return ImageInterface|null
@throws \Imagine\Exception\RuntimeException
@throws \UnexpectedValueException
@throws \ErrorException
11019
private function getWatermark(ImagineInterface $imagine): ?ImageInterface
    {
        $resource = null;
        if ($this->getMarkFilepath() !== null) {
            $resource = fopen($this->getMarkFilepath(), 'rb');
        } elseif (\is_string($this->markContent)) {
            $resource = 'a'; // TODO: create resource.
        } elseif (\is_resource($this->markContent)) {
            $resource = $this->markContent;
        }

        if ($resource === null) {
            return null;
        }

        return $imagine->read($resource);
    }
224587
function imageObject( $createMissing = true )
    {
        if ( $this->ImageObject === null or
             $this->ImageObjectRef === null )
        {
            if ( $createMissing )
            {
                if ( $this->StoredFile != '' )
                {
                    $this->process();
                }
            }
        }
        return $this->ImageObject;
    }
463988
public function getValidImage(PurchasableInterface $purchasable)
    {
        $namespace = $this->getPurchasableNamespace();
        if (!$purchasable instanceof $namespace) {
            return false;
        }

        /**
         * @var ProductInterface $purchasable
         */
        return $this
            ->imageResolver
            ->resolveImage($purchasable);
    }
249179
private function _getImagine(OutputInterface $output)
    {
        // Use Imagick extension by default
        if (extension_loaded('imagick') && class_exists("Imagick"))
        {
            $output->writeln('Imagick library is used!');
            return new \Imagine\Imagick\Imagine();
        }
        // fall back to GD
        if (extension_loaded('gd') && function_exists('gd_info'))
        {
            $output->writeln('GD library is used!');
            return new \Imagine\Gd\Imagine();
        }
        // Quit if can not find both extensions
        $output->writeln("<error>The `imagick` or `gd` extension must be loaded to generate image</error>");
        exit(1);
    }
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;
    }
129850
public function do_watermark($file, $offsetX = 0, $offsetY = 0, $opacity = 1.0)
    {
        $watermark = new \Imagick($this->alias->resolve($file));

        if ($watermark->getImageAlphaChannel() === \Imagick::ALPHACHANNEL_UNDEFINED) {
            $watermark->setImageOpacity($opacity);
        }

        if ($watermark->getNumberImages() !== 1) {
            throw new PreconditionException(['not support multiple iterations: `:file`', 'file' => $file]);
        }

        if (!$this->_image->compositeImage($watermark, \Imagick::COMPOSITE_OVER, $offsetX, $offsetY)) {
            throw new RuntimeException('Imagick::compositeImage Failed');
        }

        $watermark->clear();
        $watermark->destroy();

        return $this;
    }
297207
public function imageInfo($fileName)
    {
        if ($fileName instanceof ImageInterface) {
            return $fileName;
        }

        $image = $this->filesystem->getFile('files://' . $fileName, new NullableImage());

        return $image;
    }
826315
private function createImagineInstance()
    {
        $extension = mb_strtolower(\Craft::$app->getConfig()->getGeneral()->imageDriver);
        
        try {
            if ($extension === 'gd') {
                return new \Imagine\Gd\Imagine();
            }

            if ($extension === 'imagick') {
                return new \Imagine\Imagick\Imagine();
            }
        } catch (RuntimeException $e) {
            // just ignore for now
        }

        return null;
    }
1321280
public function getApi()
    {
        $imageManager = new ImageManager([
            'driver' => extension_loaded('imagick') ? 'imagick' : 'gd'
        ]);
        $manipulators = [
            new Size($this->maxImageSize),
            new Orientation(),
            new Crop(),
            new Brightness(),
            new Contrast(),
            new Gamma(),
            new Sharpen(),
            new Filter(),
            new Flip(),
            new Blur(),
            new Pixelate(),
            new Background(),
            new Border(),
            new Watermark($this->getWatermarks(), $this->watermarksPathPrefix),
            new Encode(),
        ];

        return new Api($imageManager, $manipulators);
    }
976767
public function setImagineImage(ImagineImageInterface $imagineImage, $mimeType = null)
    {
        if (!$mimeType) {
            $mimeType = $this->getMimeType();
        }
        if ($this->imagineImage !== $imagineImage || $mimeType != $this->getMimeType()) {
            // imagine image was changed OR MIME type was changed
            //  -> we flag binary content to be regerated from imagine image
            if ($this->imagineImage !== $imagineImage) {
                $this->imagineImage = $imagineImage;
            }
            if ($this->mimeType !== $mimeType) {
                $this->mimeType = $mimeType;
            }
            $this->binaryContentDirty = true;
        }
    }
143381
public function getImageSize(PersistentResource $resource)
    {
        $cacheIdentifier = $resource->getCacheEntryIdentifier();

        $imageSize = $this->imageSizeCache->get($cacheIdentifier);
        if ($imageSize !== false) {
            return $imageSize;
        }

        // TODO: Special handling for SVG should be refactored at a later point.
        if ($resource->getMediaType() === 'image/svg+xml') {
            $imageSize = ['width' => null, 'height' => null];
        } else {
            try {
                $imagineImage = $this->imagineService->read($resource->getStream());
                $sizeBox = $imagineImage->getSize();
                $imageSize = ['width' => $sizeBox->getWidth(), 'height' => $sizeBox->getHeight()];
            } catch (\Exception $e) {
                throw new ImageFileException(sprintf('The given resource was not an image file your choosen driver can open. The original error was: %s', $e->getMessage()), 1336662898);
            }
        }

        $this->imageSizeCache->set($cacheIdentifier, $imageSize);
        return $imageSize;
    }
932336
public function getThumbnail(): Imagick
    {
        $image = new Imagick($this->file->getPathname().'[0]');
        $image->setImageFormat('png');

        return $image;
    }
737347
public function getIcon()
    {
        $icon = $this->composerJsonAttribute('extra.flarum-extension.icon');
        $file = Arr::get($icon, 'image');

        if (is_null($icon) || is_null($file)) {
            return $icon;
        }

        $file = $this->path.'/'.$file;

        if (file_exists($file)) {
            $extension = pathinfo($file, PATHINFO_EXTENSION);
            if (! array_key_exists($extension, self::LOGO_MIMETYPES)) {
                throw new \RuntimeException('Invalid image type');
            }

            $mimetype = self::LOGO_MIMETYPES[$extension];
            $data = base64_encode(file_get_contents($file));

            $icon['backgroundImage'] = "url('data:$mimetype;base64,$data')";
        }

        return $icon;
    }
1282011
public function handle(ImagineInterface $imagine)
    {
        $data = $this->getFilteredOptions($this->options);
        $path = \rtrim($data['path'], '/');

        $source = Str::replace('{filename}.{extension}', $data);
        $destination = Str::replace($data['format'], $data);

        $this->handleImageManipulation(
            $imagine->open("{$path}/{$source}"), $data
        )->save("{$path}/{$destination}");
    }
622193
public function getImageResource()
    {
        // Get existing resource
        if ($this->image) {
            return $this->image;
        }

        // Load container
        $assetContainer = $this->getAssetContainer();
        if (!$assetContainer) {
            return null;
        }

        // Avoid repeat load of broken images
        $hash = $assetContainer->getHash();
        $variant = $assetContainer->getVariant();
        if ($this->hasFailed($hash, $variant)) {
            return null;
        }

        // Validate stream is readable
        // Note: Mark failed regardless of whether a failed stream is exceptional or not
        $error = self::FAILED_MISSING;
        try {
            $stream = $assetContainer->getStream();
            if ($this->isStreamReadable($stream)) {
                $error = null;
            } else {
                return null;
            }
        } finally {
            if ($error) {
                $this->markFailed($hash, $variant, $error);
            }
        }

        // Handle resource
        $error = self::FAILED_UNKNOWN;
        try {
            // write the file to a local path so we can extract exif data if it exists.
            // Currently exif data can only be read from file paths and not streams
            $path = tempnam(TEMP_PATH, 'interventionimage_');
            if ($extension = pathinfo($assetContainer->getFilename(), PATHINFO_EXTENSION)) {
                //tmpnam creates a file, we should clean it up if we are changing the path name
                unlink($path);
                $path .= "." . $extension;
            }
            $bytesWritten = file_put_contents($path, $stream);
            // if we fail to write, then load from stream
            if ($bytesWritten === false) {
                $resource = $this->getImageManager()->make($stream);
            } else {
                $this->setTempPath($path);
                $resource = $this->getImageManager()->make($path);
            }

            // Fix image orientation
            try {
                $resource->orientate();
            } catch (NotSupportedException $e) {
                // noop - we can't orientate, don't worry about it
            }

            $this->setImageResource($resource);
            $this->markSuccess($hash, $variant);
            $this->warmCache($hash, $variant);
            $error = null;
            return $resource;
        } catch (NotReadableException $ex) {
            // Handle unsupported image encoding on load (will be marked as failed)
            // Unsupported exceptions are handled without being raised as exceptions
            $error = self::FAILED_INVALID;
        } finally {
            if ($error) {
                $this->markFailed($hash, $variant, $error);
            }
        }
        return null;
    }
796103
public static function getImageConvertExecutable()
    {
        if ( !is_null( self::$imageConvert ) )
        {
            return self::$imageConvert;
        }
        return ( self::$imageConvert = self::findExecutableInPath( 'convert' ) );
    }
1239693
public function watermark(AbstractWatermark $watermark)
    {
        $resampleImageResource =& $this->getResampleImageResource();

        if ($watermark instanceof Text) {

            $draw = new \GmagickDraw();
            $draw->setFont($watermark->getFontPath());
            $draw->setFontSize($watermark->getFontSize());
            $draw->setFillColor($watermark->getFontColor());

            if (false !== ($textAxis = $watermark->getAxis())) {
                $draw->annotation($textAxis->getX(), $textAxis->getY(), $watermark->getString());
            } else {
                switch ($watermark->getPosition()) {
                    default:
                    case 'MIDDLE_MIDDLE':
                    case 'MIDDLE':
                    case 'CENTER':
                        $draw->setgravity(\Gmagick::GRAVITY_CENTER);
                        break;

                    case 'MIDDLE_LEFT':
                        $draw->setgravity(\Gmagick::GRAVITY_WEST);
                        break;

                    case 'MIDDLE_RIGHT':
                        $draw->setgravity(\Gmagick::GRAVITY_EAST);
                        break;

                    case 'MIDDLE_TOP':
                        $draw->setgravity(\Gmagick::GRAVITY_NORTH);
                        break;

                    case 'MIDDLE_BOTTOM':
                        $draw->setgravity(\Gmagick::GRAVITY_SOUTH);
                        break;

                    case 'TOP_LEFT':
                        $draw->setgravity(\Gmagick::GRAVITY_NORTHWEST);
                        break;

                    case 'TOP_RIGHT':
                        $draw->setgravity(\Gmagick::GRAVITY_NORTHEAST);
                        break;

                    case 'BOTTOM_LEFT':
                        $draw->setgravity(\Gmagick::GRAVITY_SOUTHWEST);
                        break;

                    case 'BOTTOM_RIGHT':
                        $draw->setgravity(\Gmagick::GRAVITY_SOUTHEAST);
                        break;
                }
            }

            try {

                $resampleImageResource->annotateimage(
                    $draw,
                    $watermark->getPadding(),
                    $watermark->getPadding(),
                    $watermark->getAngle(),
                    $watermark->getString()
                );

                return true;

            } catch (\GmagickException $e) {

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

            }
        } elseif ($watermark instanceof Overlay) {
            $watermarkImage = new self;
            $watermarkImage->setSourceImage($watermark->getImagePath());
            $watermarkImage->createFromSource();

            $watermarkImageFile = $watermarkImage->getSourceImageFile();
            $watermarkImageDimension = $watermarkImageFile->getDimension();
            $watermarkImageDimension->maintainAspectRatio = true;

            $resampleImageDimension = $this->resampleImageFile->getDimension();

            if (false === ($scale = $watermark->getImageScale())) {
                $scale = min(
                    round((($resampleImageDimension->getWidth() / 2) / $watermarkImageDimension->getWidth()) * 100),
                    round((($resampleImageDimension->getHeight() / 2) / $watermarkImageDimension->getHeight()) * 100)
                );
            }

            if ($scale > 0) {
                $watermarkImage->setResampleImage($watermarkImageFile->withDimension(
                    $watermarkImageDimension
                        ->withScale($scale)
                ));
            }

            $watermarkImageDimension = $watermarkImage->getResampleImageFile()->getDimension();

            if ($watermarkImage->scale()) {
                $watermarkImageResource = $watermarkImage->getResampleImageResource();

                if (false !== ($watermarkAxis = $watermark->getAxis())) {
                    $watermarkImageAxisX = $watermarkAxis->getX();
                    $watermarkImageAxisY = $watermarkAxis->getY();
                } else {
                    switch ($watermark->getPosition()) {
                        default:
                        case 'MIDDLE_MIDDLE':
                        case 'MIDDLE':
                        case 'CENTER':
                            $watermarkImageAxisX = ($resampleImageDimension->getWidth() - $watermarkImageDimension->getWidth()) / 2;
                            $watermarkImageAxisY = ($resampleImageDimension->getHeight() - $watermarkImageDimension->getHeight()) / 2;
                            break;

                        case 'MIDDLE_LEFT':
                            $watermarkImageAxisX = $watermark->getPadding();
                            $watermarkImageAxisY = ($resampleImageDimension->getHeight() - $watermarkImageDimension->getHeight()) / 2;
                            break;

                        case 'MIDDLE_RIGHT':
                            $watermarkImageAxisX = $resampleImageDimension->getWidth() - ($watermarkImageDimension->getWidth() + $watermark->getPadding());
                            $watermarkImageAxisY = ($resampleImageDimension->getHeight() - $watermarkImageDimension->getHeight()) / 2;
                            break;

                        case 'MIDDLE_TOP':
                            $watermarkImageAxisX = ($resampleImageDimension->getWidth() - $watermarkImageDimension->getWidth()) / 2;
                            $watermarkImageAxisY = $watermarkImageDimension->getHeight() + $watermark->getPadding();
                            break;

                        case 'MIDDLE_BOTTOM':
                            $watermarkImageAxisX = ($resampleImageDimension->getWidth() - $watermarkImageDimension->getWidth()) / 2;
                            $watermarkImageAxisY = $resampleImageDimension->getHeight() - ($watermarkImageDimension->getHeight() + $watermark->getPadding());
                            break;

                        case 'TOP_LEFT':
                            $watermarkImageAxisX = $watermark->getPadding();
                            $watermarkImageAxisY = $watermarkImageDimension->getHeight() + $watermark->getPadding();
                            break;

                        case 'TOP_RIGHT':
                            $watermarkImageAxisX = $resampleImageDimension->getWidth() - ($watermarkImageDimension->getWidth() + $watermark->getPadding());
                            $watermarkImageAxisY = $watermarkImageDimension->getHeight() + $watermark->getPadding();
                            break;

                        case 'BOTTOM_LEFT':
                            $watermarkImageAxisX = $watermark->getPadding();
                            $watermarkImageAxisY = $resampleImageDimension->getHeight() - $watermarkImageDimension->getHeight() + $watermark->getPadding();
                            break;

                        case 'BOTTOM_RIGHT':
                            $watermarkImageAxisX = $resampleImageDimension->getWidth() - ($watermarkImageDimension->getWidth() + $watermark->getPadding());
                            $watermarkImageAxisY = $resampleImageDimension->getHeight() - ($watermarkImageDimension->getHeight() + $watermark->getPadding());
                            break;
                    }
                }

                try {
                    $resampleImageResource->compositeimage($watermarkImageResource, \Gmagick::COMPOSITE_OVER,
                        $watermarkImageAxisX,
                        $watermarkImageAxisY);

                    return true;
                } catch (\GmagickException $e) {
                    $this->errors[ $e->getCode() ] = $e->getMessage();
                }
            }
        }

        return false;
    }