HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/honger.komma.pro/app/KommaApp/Images/InterventionImageCropperBridge.php
<?php
namespace App\KommaApp\Images;

use Intervention\Image\Exception\NotReadableException;
use Intervention\Image\Image;
use Intervention\Image\ImageManager;

class InterventionImageCropperBridge implements CropperInterface
{
    /**
     * @var ImageManager $cropper
     */
    private $cropper;

    /**
     * @var Image $image
     */
    private $image;

    private $handle;

    public function __construct()
    {
        $this->cropper = new ImageManager(array('driver' => \Config::get('app.imageDriver')));
    }

    /**
     * @param string $file Path to the source image location
     * @return bool
     */
    public function open(string $file):bool
    {
        try {
            $this->handle = fopen($file, 'r');
            $this->image = $this->cropper->make($file);
        }
        catch (NotReadableException $exception)
        {
            return false;
        }

        return (is_a($this->image, Image::class));
    }

    /**
     * Cut out a rectangular part of the current image with given width and height.
     * Define optional x,y coordinates to move the top-left corner of the cutout to a certain position.
     *
     * @param int $width
     * @param int $height
     * @param int $x X-Coordinate of the top-left corner if the rectangular cutout.
     * @param int $y Y-Coordinate of the top-left corner if the rectangular cutout.
     * @return bool
     */
    public function crop(int $width, int $height = null, int $x = null, int $y = null):bool
    {
        if($height == null) throw new \InvalidArgumentException('The InterventionImage cropper needs a height specified but did not get one so it cannot use the "crop" method to adjust the image.');
        ini_set('memory_limit','256M');

        $this->image = $this->image->crop($width, $height, $x, $y);

        return (is_a($this->image, Image::class));
    }

    /**
     * Combine cropping and resizing to format image in a smart way.
     * The method will find the best fitting aspect ratio of your given width and height on the current image automatically,
     * cut it out and resize it to the given dimension
     *
     * @param int $width
     * @param int $height
     * @param string $position Set a position where cutout will be positioned.
     * @return bool
     */
    public function fit(int $width, int $height = null, string $position = CropperInterface::CENTER):bool
    {
        if(!$this->image) return false;
        ini_set('memory_limit','256M');

        $this->image->fit($width, $height, function($constraint) {
            $constraint->aspectRatio();
        }, $position);

        return (is_a($this->image, Image::class));
    }

    /**
     * Resizes current image based on given width and/or height.
     *
     * @param int $width
     * @param int $height
     * @return bool
     */
    public function resize(int $width, int $height)
    {
        if(!$this->image) return false;
        ini_set('memory_limit','256M');

        if($height == 0) $height = null;
        if($width == 0) $height = null;

        $this->image->resize($width, $height, function($constraint) {
            $constraint->aspectRatio();
            $constraint->upSize();
        });

        return (is_a($this->image, Image::class));
    }

    /**
     * Saves the image
     *
     * @param string $to
     * @param int|null $quality
     * @return bool
     */
    public function save(string $to, int $quality = 87):bool
    {
        if(!$this->image) return false;

        $folderName = dirname($to);

        if(!file_exists($folderName))
            if(!mkdir($folderName))
                throw new \RuntimeException("Could not create folder: ".$folderName);


        $this->image->save($to, $quality);

        if(is_a($this->image, Image::class)) {
            if(file_exists($to)) return true;
        }

        return false;
    }

    /**
     * returns a gd resource or imagick class instance or false if the core is not available
     *
     * @return resource|object|bool
     */
    public function getCore(): resource
    {
        if(!$this->image) return false;


        $core = $this->image->getCore();

        if(is_resource($core))
        {
            if(get_resource_type($core) == "gd") return $core;
        }
        elseif(is_object($core))
        {
            if(is_a($core, \Imagick::class)) return $core;
        }
        else {
            return false;
        }

        return false;
    }

    /**
     * Determine whether an image should be encoded in interlaced or standard mode
     *
     * @param bool $enable
     * @return true on success, false on failure
     */
    public function enableProgressive(bool $enable)
    {
        if(!$this->image) return false;

        $this->image->interlace($enable);

        return false;
    }

    /**
     * @return bool true on success, false on failure
     */
    public function destroy():bool
    {
        if(!$this->image) return false;

        $this->image->destroy();
        $this->image = null;

        if($this->handle) {
            fclose($this->handle);
            $this->handle = null;
        }

        return true;
    }

    /**
     * Check if the passed position is really a position that is defined as a constant in this class.
     *
     * @param int $position
     * @param bool $strict If true it will only consider a position as valid if it is an int. If it for example is a numeric string it won't consider it valid
     * @return bool Returns true if the position is a valid one, false otherwise
     * @throws \ReflectionException
     */
    static function isValidPosition(int $position, $strict = false) {
        return in_array($position, self::getAllPositions(), $strict);
    }

    /**
     * Returns an array containing integers representing the defined positions.
     *
     * @return int[]
     * @throws \ReflectionException
     */
    private static function getAllPositions()
    {
        $thisClassAsReflectionClass = new \ReflectionClass(__CLASS__);
        return $thisClassAsReflectionClass->getConstants();
    }
}