diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 8c230ceb..cd3ed495 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -4,7 +4,7 @@ on: pull_request: ~ push: branches: - - master + - upgrade-to-glide-3 jobs: coding-style: @@ -13,22 +13,22 @@ jobs: - uses: actions/checkout@v4 - uses: shivammathur/setup-php@v2 with: - php-version: '7.4' + php-version: '8.3' extensions: gd, imagick - name: php-cs-fixer run: | - wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.16.4/php-cs-fixer.phar -q - php php-cs-fixer.phar fix --dry-run --diff + composer update --no-interaction --no-ansi --no-progress + vendor/bin/php-cs-fixer fix --dry-run --diff tests: runs-on: ubuntu-22.04 strategy: fail-fast: false matrix: - php-version: ['7.4', '8.0', '8.1', '8.2', '8.3'] + php-version: ['8.1', '8.2', '8.3', '8.4'] prefer-lowest: [''] include: - - php-version: '7.2' + - php-version: '8.1' prefer-lowest: 'prefer-lowest' steps: - uses: actions/checkout@v4 diff --git a/.gitignore b/.gitignore index 201c0ad1..fefeecec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ build vendor docs/_site -.php_cs.cache +.php-cs-fixer.cache .phpunit.result.cache composer.lock +.history diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 00000000..358f818d --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,14 @@ +in('src') + ->in('tests') +; + +return (new PhpCsFixer\Config()) + ->setRules([ + '@Symfony' => true, + 'phpdoc_annotation_without_dot' => false, + ]) + ->setFinder($finder) +; diff --git a/.php_cs.dist b/.php_cs.dist deleted file mode 100644 index d6c057e9..00000000 --- a/.php_cs.dist +++ /dev/null @@ -1,13 +0,0 @@ -in(['src', 'tests']) -; - -return PhpCsFixer\Config::create() - ->setRules(array( - '@Symfony' => true, - 'phpdoc_annotation_without_dot' => false, - )) - ->setFinder($finder) -; diff --git a/composer.json b/composer.json index 57b646bc..ef252063 100644 --- a/composer.json +++ b/composer.json @@ -26,15 +26,16 @@ } ], "require": { - "php": "^7.2|^8.0", - "intervention/image": "^2.7", + "php": "^8.1", + "intervention/image": "^3.2", "league/flysystem": "^2.0|^3.0", "psr/http-message": "^1.0|^2.0" }, "require-dev": { "mockery/mockery": "^1.3.3", "phpunit/phpunit": "^8.5|^9.0", - "phpunit/php-token-stream": "^3.1|^4.0" + "phpunit/php-token-stream": "^3.1|^4.0", + "friendsofphp/php-cs-fixer": "^3.48" }, "autoload": { "psr-4": { diff --git a/src/Api/Api.php b/src/Api/Api.php index 54e7065f..f8483452 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -3,7 +3,6 @@ namespace League\Glide\Api; use Intervention\Image\ImageManager; -use InvalidArgumentException; use League\Glide\Manipulators\ManipulatorInterface; class Api implements ApiInterface @@ -18,7 +17,7 @@ class Api implements ApiInterface /** * Collection of manipulators. * - * @var array + * @var ManipulatorInterface[] */ protected $manipulators; @@ -59,7 +58,7 @@ public function getImageManager() /** * Set the manipulators. * - * @param array $manipulators Collection of manipulators. + * @param ManipulatorInterface[] $manipulators Collection of manipulators. * * @return void */ @@ -67,7 +66,7 @@ public function setManipulators(array $manipulators) { foreach ($manipulators as $manipulator) { if (!($manipulator instanceof ManipulatorInterface)) { - throw new InvalidArgumentException('Not a valid manipulator.'); + throw new \InvalidArgumentException('Not a valid manipulator.'); } } @@ -94,7 +93,7 @@ public function getManipulators() */ public function run($source, array $params) { - $image = $this->imageManager->make($source); + $image = $this->imageManager->read($source); foreach ($this->manipulators as $manipulator) { $manipulator->setParams($params); @@ -102,6 +101,6 @@ public function run($source, array $params) $image = $manipulator->run($image); } - return $image->getEncoded(); + return $image->encodeByMediaType()->toString(); } } diff --git a/src/Filesystem/FileNotFoundException.php b/src/Filesystem/FileNotFoundException.php index acd25c05..cc0a3598 100644 --- a/src/Filesystem/FileNotFoundException.php +++ b/src/Filesystem/FileNotFoundException.php @@ -2,8 +2,6 @@ namespace League\Glide\Filesystem; -use Exception; - -class FileNotFoundException extends Exception +class FileNotFoundException extends \Exception { } diff --git a/src/Filesystem/FilesystemException.php b/src/Filesystem/FilesystemException.php index df6cbd6c..6826f82a 100644 --- a/src/Filesystem/FilesystemException.php +++ b/src/Filesystem/FilesystemException.php @@ -2,8 +2,6 @@ namespace League\Glide\Filesystem; -use Exception; - -class FilesystemException extends Exception +class FilesystemException extends \Exception { } diff --git a/src/Manipulators/Background.php b/src/Manipulators/Background.php index 555a34a3..ee527d53 100644 --- a/src/Manipulators/Background.php +++ b/src/Manipulators/Background.php @@ -2,7 +2,8 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Interfaces\ImageInterface; +use Intervention\Image\Origin; use League\Glide\Manipulators\Helpers\Color; /** @@ -13,11 +14,11 @@ class Background extends BaseManipulator /** * Perform background image manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(Image $image) + public function run(ImageInterface $image): ImageInterface { if (is_null($this->bg)) { return $image; @@ -26,9 +27,14 @@ public function run(Image $image) $color = (new Color($this->bg))->formatted(); if ($color) { - $new = $image->getDriver()->newImage($image->width(), $image->height(), $color); - $new->mime = $image->mime; - $image = $new->insert($image, 'top-left', 0, 0); + $new = $image->driver()->createImage($image->width(), $image->height()) + ->fill($color) + ->place($image, 'top-left', 0, 0) + ->setOrigin( + new Origin($image->origin()->mediaType()) + ); + + $image = $new; } return $image; diff --git a/src/Manipulators/BaseManipulator.php b/src/Manipulators/BaseManipulator.php index 88345aa8..693e0135 100644 --- a/src/Manipulators/BaseManipulator.php +++ b/src/Manipulators/BaseManipulator.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Interfaces\ImageInterface; abstract class BaseManipulator implements ManipulatorInterface { @@ -44,7 +44,7 @@ public function __get($name) /** * Perform the image manipulation. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - abstract public function run(Image $image); + abstract public function run(ImageInterface $image): ImageInterface; } diff --git a/src/Manipulators/Blur.php b/src/Manipulators/Blur.php index c3db393e..2a5f4ce4 100644 --- a/src/Manipulators/Blur.php +++ b/src/Manipulators/Blur.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Interfaces\ImageInterface; /** * @property string $blur @@ -12,11 +12,11 @@ class Blur extends BaseManipulator /** * Perform blur image manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(Image $image) + public function run(ImageInterface $image): ImageInterface { $blur = $this->getBlur(); diff --git a/src/Manipulators/Border.php b/src/Manipulators/Border.php index 83fd164f..601b68ac 100644 --- a/src/Manipulators/Border.php +++ b/src/Manipulators/Border.php @@ -2,7 +2,8 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Geometry\Factories\RectangleFactory; +use Intervention\Image\Interfaces\ImageInterface; use League\Glide\Manipulators\Helpers\Color; use League\Glide\Manipulators\Helpers\Dimension; @@ -15,11 +16,11 @@ class Border extends BaseManipulator /** * Perform border image manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(Image $image) + public function run(ImageInterface $image): ImageInterface { if ($border = $this->getBorder($image)) { list($width, $color, $method) = $border; @@ -43,39 +44,41 @@ public function run(Image $image) /** * Resolve border amount. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * * @return (float|string)[]|null The resolved border amount. * * @psalm-return array{0: float, 1: string, 2: string}|null */ - public function getBorder(Image $image) + public function getBorder(ImageInterface $image): ?array { if (!$this->border) { - return; + return null; } $values = explode(',', $this->border); $width = $this->getWidth($image, $this->getDpr(), isset($values[0]) ? $values[0] : null); - $color = $this->getColor(isset($values[1]) ? $values[1] : null); - $method = $this->getMethod(isset($values[2]) ? $values[2] : null); + $color = $this->getColor(isset($values[1]) ? $values[1] : 'ffffff'); + $method = $this->getMethod(isset($values[2]) ? $values[2] : 'overlay'); if ($width) { return [$width, $color, $method]; } + + return null; } /** * Get border width. * - * @param Image $image The source image. - * @param float $dpr The device pixel ratio. - * @param string $width The border width. + * @param ImageInterface $image The source image. + * @param float $dpr The device pixel ratio. + * @param string $width The border width. * * @return float|null The resolved border width. */ - public function getWidth(Image $image, $dpr, $width) + public function getWidth(ImageInterface $image, $dpr, $width): ?float { return (new Dimension($image, $dpr))->get($width); } @@ -87,7 +90,7 @@ public function getWidth(Image $image, $dpr, $width) * * @return string The formatted color. */ - public function getColor($color) + public function getColor(string $color) { return (new Color($color))->formatted(); } @@ -99,7 +102,7 @@ public function getColor($color) * * @return string The resolved border method. */ - public function getMethod($method) + public function getMethod(string $method) { if (!in_array($method, ['expand', 'shrink', 'overlay'], true)) { return 'overlay'; @@ -129,21 +132,23 @@ public function getDpr() /** * Run the overlay border method. * - * @param Image $image The source image. - * @param float $width The border width. - * @param string $color The border color. + * @param ImageInterface $image The source image. + * @param float $width The border width. + * @param string $color The border color. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function runOverlay(Image $image, $width, $color) + public function runOverlay(ImageInterface $image, $width, $color): ImageInterface { - return $image->rectangle( + return $image->drawRectangle( (int) round($width / 2), (int) round($width / 2), - (int) round($image->width() - ($width / 2)), - (int) round($image->height() - ($width / 2)), - function ($draw) use ($width, $color) { - $draw->border($width, $color); + function (RectangleFactory $rectangle) use ($image, $width, $color) { + $rectangle->size( + (int) round($image->width() - $width), + (int) round($image->height() - $width), + ); + $rectangle->border($color, intval($width)); } ); } @@ -151,45 +156,43 @@ function ($draw) use ($width, $color) { /** * Run the shrink border method. * - * @param Image $image The source image. - * @param float $width The border width. - * @param string $color The border color. + * @param ImageInterface $image The source image. + * @param float $width The border width. + * @param string $color The border color. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function runShrink(Image $image, $width, $color) + public function runShrink(ImageInterface $image, $width, $color): ImageInterface { return $image ->resize( (int) round($image->width() - ($width * 2)), (int) round($image->height() - ($width * 2)) ) - ->resizeCanvas( + ->resizeCanvasRelative( (int) round($width * 2), (int) round($width * 2), + $color, 'center', - true, - $color ); } /** * Run the expand border method. * - * @param Image $image The source image. - * @param float $width The border width. - * @param string $color The border color. + * @param ImageInterface $image The source image. + * @param float $width The border width. + * @param string $color The border color. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function runExpand(Image $image, $width, $color) + public function runExpand(ImageInterface $image, $width, $color): ImageInterface { - return $image->resizeCanvas( + return $image->resizeCanvasRelative( (int) round($width * 2), (int) round($width * 2), + $color, 'center', - true, - $color ); } } diff --git a/src/Manipulators/Brightness.php b/src/Manipulators/Brightness.php index 2a33544e..35e13301 100644 --- a/src/Manipulators/Brightness.php +++ b/src/Manipulators/Brightness.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Interfaces\ImageInterface; /** * @property string|null $bri @@ -12,11 +12,11 @@ class Brightness extends BaseManipulator /** * Perform brightness image manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(Image $image) + public function run(ImageInterface $image): ImageInterface { $brightness = $this->getBrightness(); diff --git a/src/Manipulators/Contrast.php b/src/Manipulators/Contrast.php index c2cf9140..e009565e 100644 --- a/src/Manipulators/Contrast.php +++ b/src/Manipulators/Contrast.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Interfaces\ImageInterface; /** * @property string|null $con @@ -12,11 +12,11 @@ class Contrast extends BaseManipulator /** * Perform contrast image manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(Image $image) + public function run(ImageInterface $image): ImageInterface { $contrast = $this->getContrast(); diff --git a/src/Manipulators/Crop.php b/src/Manipulators/Crop.php index 9a8428df..12ebcbe0 100644 --- a/src/Manipulators/Crop.php +++ b/src/Manipulators/Crop.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Interfaces\ImageInterface; /** * @property string|null $crop @@ -12,11 +12,11 @@ class Crop extends BaseManipulator /** * Perform crop image manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(Image $image) + public function run(ImageInterface $image): ImageInterface { $coordinates = $this->getCoordinates($image); @@ -37,32 +37,32 @@ public function run(Image $image) /** * Resolve coordinates. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * * @return int[]|null The resolved coordinates. * * @psalm-return array{0: int, 1: int, 2: int, 3: int}|null */ - public function getCoordinates(Image $image) + public function getCoordinates(ImageInterface $image): ?array { if (null === $this->crop) { - return; + return null; } $coordinates = explode(',', $this->crop); - if (4 !== count($coordinates) or - (!is_numeric($coordinates[0])) or - (!is_numeric($coordinates[1])) or - (!is_numeric($coordinates[2])) or - (!is_numeric($coordinates[3])) or - ($coordinates[0] <= 0) or - ($coordinates[1] <= 0) or - ($coordinates[2] < 0) or - ($coordinates[3] < 0) or - ($coordinates[2] >= $image->width()) or - ($coordinates[3] >= $image->height())) { - return; + if (4 !== count($coordinates) + or (!is_numeric($coordinates[0])) + or (!is_numeric($coordinates[1])) + or (!is_numeric($coordinates[2])) + or (!is_numeric($coordinates[3])) + or ($coordinates[0] <= 0) + or ($coordinates[1] <= 0) + or ($coordinates[2] < 0) + or ($coordinates[3] < 0) + or ($coordinates[2] >= $image->width()) + or ($coordinates[3] >= $image->height())) { + return null; } return [ @@ -76,12 +76,12 @@ public function getCoordinates(Image $image) /** * Limit coordinates to image boundaries. * - * @param Image $image The source image. - * @param int[] $coordinates The coordinates. + * @param ImageInterface $image The source image. + * @param int[] $coordinates The coordinates. * * @return int[] The limited coordinates. */ - public function limitToImageBoundaries(Image $image, array $coordinates) + public function limitToImageBoundaries(ImageInterface $image, array $coordinates): array { if ($coordinates[0] > ($image->width() - $coordinates[2])) { $coordinates[0] = $image->width() - $coordinates[2]; diff --git a/src/Manipulators/Encode.php b/src/Manipulators/Encode.php index cad211be..b730e074 100644 --- a/src/Manipulators/Encode.php +++ b/src/Manipulators/Encode.php @@ -2,7 +2,10 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Drivers\Gd\Driver as GdDriver; +use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver; +use Intervention\Image\ImageManager; +use Intervention\Image\Interfaces\ImageInterface; /** * @property string $fm @@ -13,43 +16,55 @@ class Encode extends BaseManipulator /** * Perform output image manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(Image $image) + public function run(ImageInterface $image): ImageInterface { $format = $this->getFormat($image); $quality = $this->getQuality(); + $driver = $image->driver(); if (in_array($format, ['jpg', 'pjpg'], true)) { - $image = $image->getDriver() - ->newImage($image->width(), $image->height(), '#fff') - ->insert($image, 'top-left', 0, 0); + $image = (new ImageManager($driver)) + ->create($image->width(), $image->height()) + ->fill('ffffff') + ->place($image, 'top-left', 0, 0); } - if ('pjpg' === $format) { - $image->interlace(); - $format = 'jpg'; + if (in_array($format, ['png', 'pjpg'], true)) { + $i = $image->core()->native(); + if ($driver instanceof ImagickDriver) { + $i->setInterlaceScheme(3); // 3 = Imagick::INTERLACE_PLANE constant + } elseif ($driver instanceof GdDriver) { + imageinterlace($i, true); + } + + if ('pjpg' === $format) { + $format = 'jpg'; + } } - return $image->encode($format, $quality); + return (new ImageManager($driver))->read( + $image->encodeByExtension($format, $quality)->toFilePointer() + ); } /** * Resolve format. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * * @return string The resolved format. */ - public function getFormat(Image $image) + public function getFormat(ImageInterface $image) { if (array_key_exists($this->fm, static::supportedFormats())) { return $this->fm; } - return array_search($image->mime(), static::supportedFormats(), true) ?: 'jpg'; + return array_search($image->origin()->mediaType(), static::supportedFormats(), true) ?: 'jpg'; } /** diff --git a/src/Manipulators/Filter.php b/src/Manipulators/Filter.php index 7d71729e..ebcc83f2 100644 --- a/src/Manipulators/Filter.php +++ b/src/Manipulators/Filter.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Interfaces\ImageInterface; /** * @property string $filt @@ -12,11 +12,11 @@ class Filter extends BaseManipulator /** * Perform filter image manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(Image $image) + public function run(ImageInterface $image): ImageInterface { if ('greyscale' === $this->filt) { return $this->runGreyscaleFilter($image); @@ -32,11 +32,11 @@ public function run(Image $image) /** * Perform greyscale manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function runGreyscaleFilter(Image $image) + public function runGreyscaleFilter(ImageInterface $image): ImageInterface { return $image->greyscale(); } @@ -44,11 +44,11 @@ public function runGreyscaleFilter(Image $image) /** * Perform sepia manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function runSepiaFilter(Image $image) + public function runSepiaFilter(ImageInterface $image): ImageInterface { $image->greyscale(); $image->brightness(-10); diff --git a/src/Manipulators/Flip.php b/src/Manipulators/Flip.php index fcec77f8..0f66c93d 100644 --- a/src/Manipulators/Flip.php +++ b/src/Manipulators/Flip.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Interfaces\ImageInterface; /** * @property string $flip @@ -12,18 +12,24 @@ class Flip extends BaseManipulator /** * Perform flip image manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(Image $image) + public function run(ImageInterface $image): ImageInterface { if ($flip = $this->getFlip()) { if ('both' === $flip) { - return $image->flip('h')->flip('v'); + return $image->flip()->flop(); } - return $image->flip($flip); + if ('v' === $flip) { + return $image->flip(); + } + + if ('h' === $flip) { + return $image->flop(); + } } return $image; diff --git a/src/Manipulators/Gamma.php b/src/Manipulators/Gamma.php index 035bb8de..a3bd6055 100644 --- a/src/Manipulators/Gamma.php +++ b/src/Manipulators/Gamma.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Interfaces\ImageInterface; /** * @property string|null $gam @@ -12,11 +12,11 @@ class Gamma extends BaseManipulator /** * Perform gamma image manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(Image $image) + public function run(ImageInterface $image): ImageInterface { $gamma = $this->getGamma(); diff --git a/src/Manipulators/Helpers/Color.php b/src/Manipulators/Helpers/Color.php index 4b77e36a..b401a679 100644 --- a/src/Manipulators/Helpers/Color.php +++ b/src/Manipulators/Helpers/Color.php @@ -7,22 +7,22 @@ class Color /** * 3 digit color code expression. */ - const SHORT_RGB = '/^[0-9a-f]{3}$/i'; + public const SHORT_RGB = '/^[0-9a-f]{3}$/i'; /** * 4 digit color code expression. */ - const SHORT_ARGB = '/^[0-9]{1}[0-9a-f]{3}$/i'; + public const SHORT_ARGB = '/^[0-9]{1}[0-9a-f]{3}$/i'; /** * 6 digit color code expression. */ - const LONG_RGB = '/^[0-9a-f]{6}$/i'; + public const LONG_RGB = '/^[0-9a-f]{6}$/i'; /** * 8 digit color code expression. */ - const LONG_ARGB = '/^[0-9]{2}[0-9a-f]{6}$/i'; + public const LONG_ARGB = '/^[0-9]{2}[0-9a-f]{6}$/i'; /** * The red value. diff --git a/src/Manipulators/Helpers/Dimension.php b/src/Manipulators/Helpers/Dimension.php index ac9fda4b..e7502aee 100644 --- a/src/Manipulators/Helpers/Dimension.php +++ b/src/Manipulators/Helpers/Dimension.php @@ -2,14 +2,14 @@ namespace League\Glide\Manipulators\Helpers; -use Intervention\Image\Image; +use Intervention\Image\Interfaces\ImageInterface; class Dimension { /** * The source image. * - * @var Image + * @var ImageInterface */ protected $image; @@ -23,10 +23,10 @@ class Dimension /** * Create dimension helper instance. * - * @param Image $image The source image. - * @param float $dpr The device pixel ratio. + * @param ImageInterface $image The source image. + * @param float $dpr The device pixel ratio. */ - public function __construct(Image $image, $dpr = 1) + public function __construct(ImageInterface $image, $dpr = 1) { $this->image = $image; $this->dpr = $dpr; @@ -52,5 +52,7 @@ public function get($value) return (float) $this->image->width() * ((float) $matches[1] / 100); } + + return null; } } diff --git a/src/Manipulators/ManipulatorInterface.php b/src/Manipulators/ManipulatorInterface.php index 3b66643e..7bb13ca4 100644 --- a/src/Manipulators/ManipulatorInterface.php +++ b/src/Manipulators/ManipulatorInterface.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Interfaces\ImageInterface; interface ManipulatorInterface { @@ -16,9 +16,9 @@ public function setParams(array $params); /** * Perform the image manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(Image $image); + public function run(ImageInterface $image): ImageInterface; } diff --git a/src/Manipulators/Orientation.php b/src/Manipulators/Orientation.php index 231cbebe..36e4b06e 100644 --- a/src/Manipulators/Orientation.php +++ b/src/Manipulators/Orientation.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Interfaces\ImageInterface; /** * @property string $or @@ -12,16 +12,41 @@ class Orientation extends BaseManipulator /** * Perform orientation image manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(Image $image) + public function run(ImageInterface $image): ImageInterface { $orientation = $this->getOrientation(); + $originalOrientation = $image->exif('Orientation'); - if ('auto' === $orientation) { - return $image->orientate(); + if ('auto' === $orientation && is_numeric($originalOrientation)) { + switch ($originalOrientation) { + case 2: + $image->flip(); + break; + case 3: + $image->rotate(180); + break; + case 4: + $image->rotate(180)->flip(); + break; + case 5: + $image->rotate(270)->flip(); + break; + case 6: + $image->rotate(270); + break; + case 7: + $image->rotate(90)->flip(); + break; + case 8: + $image->rotate(90); + break; + } + + return $image; } return $image->rotate((float) $orientation); diff --git a/src/Manipulators/Pixelate.php b/src/Manipulators/Pixelate.php index 1de165df..573358ce 100644 --- a/src/Manipulators/Pixelate.php +++ b/src/Manipulators/Pixelate.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Interfaces\ImageInterface; /** * @property string $pixel @@ -12,11 +12,11 @@ class Pixelate extends BaseManipulator /** * Perform pixelate image manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(Image $image) + public function run(ImageInterface $image): ImageInterface { $pixelate = $this->getPixelate(); diff --git a/src/Manipulators/Sharpen.php b/src/Manipulators/Sharpen.php index 4a44a5a3..40ecddaa 100644 --- a/src/Manipulators/Sharpen.php +++ b/src/Manipulators/Sharpen.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Interfaces\ImageInterface; /** * @property string $sharp @@ -12,11 +12,11 @@ class Sharpen extends BaseManipulator /** * Perform sharpen image manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(Image $image) + public function run(ImageInterface $image): ImageInterface { $sharpen = $this->getSharpen(); diff --git a/src/Manipulators/Size.php b/src/Manipulators/Size.php index dd93b55a..619f9458 100644 --- a/src/Manipulators/Size.php +++ b/src/Manipulators/Size.php @@ -2,7 +2,8 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Geometry\Rectangle; +use Intervention\Image\Interfaces\ImageInterface; /** * @property string $dpr @@ -54,11 +55,11 @@ public function getMaxImageSize() /** * Perform size image manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(Image $image) + public function run(ImageInterface $image): ImageInterface { $width = $this->getWidth(); $height = $this->getHeight(); @@ -69,7 +70,7 @@ public function run(Image $image) list($width, $height) = $this->applyDpr($width, $height, $dpr); list($width, $height) = $this->limitImageSize($width, $height); - if ((int) $width !== (int) $image->width() || (int) $height !== (int) $image->height() || 1.0 !== $this->getCrop()[2]) { + if ((int) $width !== $image->width() || (int) $height !== $image->height() || 1.0 !== $this->getCrop()[2]) { $image = $this->runResize($image, $fit, (int) $width, (int) $height); } @@ -155,13 +156,13 @@ public function getDpr() /** * Resolve missing image dimensions. * - * @param Image $image The source image. - * @param int|null $width The image width. - * @param int|null $height The image height. + * @param ImageInterface $image The source image. + * @param int|null $width The image width. + * @param int|null $height The image height. * * @return int[] The resolved width and height. */ - public function resolveMissingDimensions(Image $image, $width, $height) + public function resolveMissingDimensions(ImageInterface $image, $width, $height) { if (is_null($width) and is_null($height)) { $width = $image->width(); @@ -169,13 +170,11 @@ public function resolveMissingDimensions(Image $image, $width, $height) } if (is_null($width) || is_null($height)) { - $size = (new \Intervention\Image\Size($image->width(), $image->height())) - ->resize($width, $height, function ($constraint) { - $constraint->aspectRatio(); - }); + $size = (new Rectangle($image->width(), $image->height())) + ->scale($width, $height); - $width = $size->getWidth(); - $height = $size->getHeight(); + $width = $size->width(); + $height = $size->height(); } return [ @@ -232,14 +231,14 @@ public function limitImageSize($width, $height) /** * Perform resize image manipulation. * - * @param Image $image The source image. - * @param string $fit The fit. - * @param int $width The width. - * @param int $height The height. + * @param ImageInterface $image The source image. + * @param string $fit The fit. + * @param int $width The width. + * @param int $height The height. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function runResize(Image $image, $fit, $width, $height) + public function runResize(ImageInterface $image, $fit, $width, $height): ImageInterface { if ('contain' === $fit) { return $this->runContainResize($image, $width, $height); @@ -271,80 +270,69 @@ public function runResize(Image $image, $fit, $width, $height) /** * Perform contain resize image manipulation. * - * @param Image $image The source image. - * @param int $width The width. - * @param int $height The height. + * @param ImageInterface $image The source image. + * @param int $width The width. + * @param int $height The height. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function runContainResize(Image $image, $width, $height) + public function runContainResize(ImageInterface $image, $width, $height): ImageInterface { - return $image->resize($width, $height, function ($constraint) { - $constraint->aspectRatio(); - }); + return $image->scale($width, $height); } /** * Perform max resize image manipulation. * - * @param Image $image The source image. - * @param int $width The width. - * @param int $height The height. + * @param ImageInterface $image The source image. + * @param int $width The width. + * @param int $height The height. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function runMaxResize(Image $image, $width, $height) + public function runMaxResize(ImageInterface $image, $width, $height): ImageInterface { - return $image->resize($width, $height, function ($constraint) { - $constraint->aspectRatio(); - $constraint->upsize(); - }); + return $image->scaleDown($width, $height); } /** * Perform fill resize image manipulation. * - * @param Image $image The source image. - * @param int $width The width. - * @param int $height The height. + * @param ImageInterface $image The source image. + * @param int $width The width. + * @param int $height The height. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function runFillResize($image, $width, $height) + public function runFillResize(ImageInterface $image, $width, $height): ImageInterface { - $image = $this->runMaxResize($image, $width, $height); - - return $image->resizeCanvas($width, $height, 'center'); + return $image->pad($width, $height); } /** * Perform fill-max resize image manipulation. * - * @param Image $image The source image. - * @param int $width The width. - * @param int $height The height. + * @param ImageInterface $image The source image. + * @param int $width The width. + * @param int $height The height. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function runFillMaxResize(Image $image, $width, $height) + public function runFillMaxResize(ImageInterface $image, $width, $height): ImageInterface { - $image = $image->resize($width, $height, function ($constraint) { - $constraint->aspectRatio(); - }); - - return $image->resizeCanvas($width, $height, 'center'); + return $image->contain($width, $height); } /** * Perform stretch resize image manipulation. * - * @param Image $image The source image. - * @param int $width The width. - * @param int $height The height. + * @param ImageInterface $image The source image. + * @param int $width The width. + * @param int $height The height. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function runStretchResize(Image $image, $width, $height) + public function runStretchResize(ImageInterface $image, $width, $height): ImageInterface { return $image->resize($width, $height); } @@ -352,21 +340,19 @@ public function runStretchResize(Image $image, $width, $height) /** * Perform crop resize image manipulation. * - * @param Image $image The source image. - * @param int $width The width. - * @param int $height The height. + * @param ImageInterface $image The source image. + * @param int $width The width. + * @param int $height The height. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function runCropResize(Image $image, $width, $height) + public function runCropResize(ImageInterface $image, $width, $height): ImageInterface { list($resize_width, $resize_height) = $this->resolveCropResizeDimensions($image, $width, $height); $zoom = $this->getCrop()[2]; - $image->resize($resize_width * $zoom, $resize_height * $zoom, function ($constraint) { - $constraint->aspectRatio(); - }); + $image->scale((int) round($resize_width * $zoom), (int) round($resize_height * $zoom)); list($offset_x, $offset_y) = $this->resolveCropOffset($image, $width, $height); @@ -376,13 +362,13 @@ public function runCropResize(Image $image, $width, $height) /** * Resolve the crop resize dimensions. * - * @param Image $image The source image. - * @param int $width The width. - * @param int $height The height. + * @param ImageInterface $image The source image. + * @param int $width The width. + * @param int $height The height. * * @return array The resize dimensions. */ - public function resolveCropResizeDimensions(Image $image, $width, $height) + public function resolveCropResizeDimensions(ImageInterface $image, $width, $height): array { if ($height > $width * ($image->height() / $image->width())) { return [$height * ($image->width() / $image->height()), $height]; @@ -394,13 +380,13 @@ public function resolveCropResizeDimensions(Image $image, $width, $height) /** * Resolve the crop offset. * - * @param Image $image The source image. - * @param int $width The width. - * @param int $height The height. + * @param ImageInterface $image The source image. + * @param int $width The width. + * @param int $height The height. * * @return array The crop offset. */ - public function resolveCropOffset(Image $image, $width, $height) + public function resolveCropOffset(ImageInterface $image, $width, $height): array { list($offset_percentage_x, $offset_percentage_y) = $this->getCrop(); diff --git a/src/Manipulators/Watermark.php b/src/Manipulators/Watermark.php index 5e88c843..a93427cf 100644 --- a/src/Manipulators/Watermark.php +++ b/src/Manipulators/Watermark.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Image; +use Intervention\Image\Interfaces\ImageInterface; use League\Flysystem\FilesystemException as FilesystemV2Exception; use League\Flysystem\FilesystemOperator; use League\Glide\Filesystem\FilesystemException; @@ -94,11 +94,11 @@ public function getWatermarksPathPrefix() /** * Perform watermark image manipulation. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(Image $image) + public function run(ImageInterface $image): ImageInterface { if ($watermark = $this->getImage($image)) { $markw = $this->getDimension($image, 'markw'); @@ -122,11 +122,7 @@ public function run(Image $image) ]); $watermark = $size->run($watermark); - if ($markalpha < 100) { - $watermark->opacity($markalpha); - } - - $image->insert($watermark, $markpos, intval($markx), intval($marky)); + $image->place($watermark, $markpos, intval($markx), intval($marky), $markalpha); } return $image; @@ -135,22 +131,22 @@ public function run(Image $image) /** * Get the watermark image. * - * @param Image $image The source image. + * @param ImageInterface $image The source image. * - * @return Image|null The watermark image. + * @return ImageInterface|null The watermark image. */ - public function getImage(Image $image) + public function getImage(ImageInterface $image): ?ImageInterface { if (is_null($this->watermarks)) { - return; + return null; } if (!is_string($this->mark)) { - return; + return null; } if ('' === $this->mark) { - return; + return null; } $path = $this->mark; @@ -163,22 +159,28 @@ public function getImage(Image $image) if ($this->watermarks->fileExists($path)) { $source = $this->watermarks->read($path); - return $image->getDriver()->init($source); + $mark = $image->driver()->handleInput($source); } } catch (FilesystemV2Exception $exception) { throw new FilesystemException('Could not read the image `'.$path.'`.'); } + + if (!$mark instanceof ImageInterface) { + return null; + } + + return $mark; } /** * Get a dimension. * - * @param Image $image The source image. - * @param string $field The requested field. + * @param ImageInterface $image The source image. + * @param string $field The requested field. * * @return float|null The dimension. */ - public function getDimension(Image $image, $field) + public function getDimension(ImageInterface $image, $field) { if ($this->{$field}) { return (new Dimension($image, $this->getDpr()))->get($this->{$field}); diff --git a/src/Responses/PsrResponseFactory.php b/src/Responses/PsrResponseFactory.php index cfd2637b..cff5a1c2 100644 --- a/src/Responses/PsrResponseFactory.php +++ b/src/Responses/PsrResponseFactory.php @@ -2,7 +2,6 @@ namespace League\Glide\Responses; -use Closure; use League\Flysystem\FilesystemOperator; use Psr\Http\Message\ResponseInterface; @@ -18,7 +17,7 @@ class PsrResponseFactory implements ResponseFactoryInterface /** * Callback to create stream. * - * @var Closure + * @var \Closure */ protected $streamCallback; @@ -26,9 +25,9 @@ class PsrResponseFactory implements ResponseFactoryInterface * Create PsrResponseFactory instance. * * @param ResponseInterface $response Base response object. - * @param Closure $streamCallback Callback to create stream. + * @param \Closure $streamCallback Callback to create stream. */ - public function __construct(ResponseInterface $response, Closure $streamCallback) + public function __construct(ResponseInterface $response, \Closure $streamCallback) { $this->response = $response; $this->streamCallback = $streamCallback; diff --git a/src/Server.php b/src/Server.php index fb77046e..0e30a109 100644 --- a/src/Server.php +++ b/src/Server.php @@ -2,8 +2,6 @@ namespace League\Glide; -use Closure; -use InvalidArgumentException; use League\Flysystem\FilesystemException as FilesystemV2Exception; use League\Flysystem\FilesystemOperator; use League\Glide\Api\ApiInterface; @@ -292,12 +290,12 @@ public function getTempDir() * * @return void * - * @throws InvalidArgumentException + * @throws \InvalidArgumentException */ public function setTempDir($tempDir) { if (!$tempDir || !is_dir($tempDir)) { - throw new InvalidArgumentException(sprintf('Invalid temp dir provided: "%s" does not exist.', $tempDir)); + throw new \InvalidArgumentException(sprintf('Invalid temp dir provided: "%s" does not exist.', $tempDir)); } $this->tempDir = rtrim($tempDir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; @@ -352,7 +350,7 @@ public function getCacheWithFileExtensions() * * @param \Closure|null $cachePathCallable The custom cache path callable. It receives the same arguments as @see getCachePath */ - public function setCachePathCallable(?Closure $cachePathCallable) + public function setCachePathCallable(?\Closure $cachePathCallable) { $this->cachePathCallable = $cachePathCallable; } @@ -379,7 +377,7 @@ public function getCachePath($path, array $params = []) { $customCallable = $this->getCachePathCallable(); if (null !== $customCallable) { - $boundCallable = Closure::bind($customCallable, $this, static::class); + $boundCallable = \Closure::bind($customCallable, $this, static::class); return $boundCallable($path, $params); } @@ -442,7 +440,7 @@ public function cacheFileExists($path, array $params) public function deleteCache($path) { if (!$this->groupCacheInFolders) { - throw new InvalidArgumentException('Deleting cached image manipulations is not possible when grouping cache into folders is disabled.'); + throw new \InvalidArgumentException('Deleting cached image manipulations is not possible when grouping cache into folders is disabled.'); } try { @@ -574,12 +572,12 @@ public function getResponseFactory() * * @return mixed Image response. * - * @throws InvalidArgumentException + * @throws \InvalidArgumentException */ public function getImageResponse($path, array $params) { if (is_null($this->responseFactory)) { - throw new InvalidArgumentException('Unable to get image response, no response factory defined.'); + throw new \InvalidArgumentException('Unable to get image response, no response factory defined.'); } $path = $this->makeImage($path, $params); @@ -616,9 +614,9 @@ public function getImageAsBase64($path, array $params) * @param string $path Image path. * @param array $params Image manipulation params. * - * @throws InvalidArgumentException - * * @return void + * + * @throws \InvalidArgumentException */ public function outputImage($path, array $params) { @@ -627,7 +625,7 @@ public function outputImage($path, array $params) try { header('Content-Type:'.$this->cache->mimeType($path)); header('Content-Length:'.$this->cache->fileSize($path)); - header('Cache-Control:'.'max-age=31536000, public'); + header('Cache-Control:max-age=31536000, public'); header('Expires:'.date_create('+1 years')->format('D, d M Y H:i:s').' GMT'); $stream = $this->cache->readStream($path); diff --git a/src/ServerFactory.php b/src/ServerFactory.php index 3dbf987c..2ee849e0 100644 --- a/src/ServerFactory.php +++ b/src/ServerFactory.php @@ -3,7 +3,6 @@ namespace League\Glide; use Intervention\Image\ImageManager; -use InvalidArgumentException; use League\Flysystem\Filesystem; use League\Flysystem\FilesystemOperator; use League\Flysystem\Local\LocalFilesystemAdapter; @@ -82,7 +81,7 @@ public function getServer() public function getSource() { if (!isset($this->config['source'])) { - throw new InvalidArgumentException('A "source" file system must be set.'); + throw new \InvalidArgumentException('A "source" file system must be set.'); } if (is_string($this->config['source'])) { @@ -114,7 +113,7 @@ public function getSourcePathPrefix() public function getCache() { if (!isset($this->config['cache'])) { - throw new InvalidArgumentException('A "cache" file system must be set.'); + throw new \InvalidArgumentException('A "cache" file system must be set.'); } if (is_string($this->config['cache'])) { @@ -246,9 +245,15 @@ public function getImageManager() $driver = $this->config['driver']; } - return new ImageManager([ - 'driver' => $driver, - ]); + if ('gd' === $driver) { + $manager = ImageManager::gd(); + } elseif ('imagick' === $driver) { + $manager = ImageManager::imagick(); + } else { + $manager = ImageManager::withDriver($driver); + } + + return $manager; } /** diff --git a/src/Signatures/Signature.php b/src/Signatures/Signature.php index 82200a37..44bdec78 100644 --- a/src/Signatures/Signature.php +++ b/src/Signatures/Signature.php @@ -40,9 +40,9 @@ public function addSignature($path, array $params) * @param string $path The resource path. * @param array $params The manipulation params. * - * @throws SignatureException - * * @return void + * + * @throws SignatureException */ public function validateRequest($path, array $params) { diff --git a/src/Signatures/SignatureException.php b/src/Signatures/SignatureException.php index ba2f4c2c..76722e0f 100644 --- a/src/Signatures/SignatureException.php +++ b/src/Signatures/SignatureException.php @@ -2,8 +2,6 @@ namespace League\Glide\Signatures; -use Exception; - -class SignatureException extends Exception +class SignatureException extends \Exception { } diff --git a/src/Urls/UrlBuilder.php b/src/Urls/UrlBuilder.php index 5cc2d357..ee1fffd4 100644 --- a/src/Urls/UrlBuilder.php +++ b/src/Urls/UrlBuilder.php @@ -2,7 +2,6 @@ namespace League\Glide\Urls; -use InvalidArgumentException; use League\Glide\Signatures\SignatureInterface; class UrlBuilder @@ -82,7 +81,7 @@ public function getUrl($path, array $params = []) $parts = parse_url($this->baseUrl.'/'.trim($path, '/')); if (false === $parts) { - throw new InvalidArgumentException('Not a valid path.'); + throw new \InvalidArgumentException('Not a valid path.'); } $parts['path'] = '/'.trim($parts['path'], '/'); diff --git a/tests/Api/ApiTest.php b/tests/Api/ApiTest.php index 8320e423..da3b58cc 100644 --- a/tests/Api/ApiTest.php +++ b/tests/Api/ApiTest.php @@ -2,53 +2,55 @@ namespace League\Glide\Api; -use InvalidArgumentException; -use Mockery; +use Intervention\Image\ImageManager; +use Intervention\Image\Interfaces\EncodedImageInterface; +use Intervention\Image\Interfaces\ImageInterface; +use League\Glide\Manipulators\ManipulatorInterface; use PHPUnit\Framework\TestCase; class ApiTest extends TestCase { - private $output; + private $api; public function setUp(): void { - $this->api = new Api(Mockery::mock('Intervention\Image\ImageManager'), []); + $this->api = new Api(ImageManager::gd(), []); } public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() { - $this->assertInstanceOf('League\Glide\Api\Api', $this->api); + $this->assertInstanceOf(Api::class, $this->api); } public function testSetImageManager() { - $this->api->setImageManager(Mockery::mock('Intervention\Image\ImageManager')); - $this->assertInstanceOf('Intervention\Image\ImageManager', $this->api->getImageManager()); + $this->api->setImageManager(ImageManager::gd()); + $this->assertInstanceOf(ImageManager::class, $this->api->getImageManager()); } public function testGetImageManager() { - $this->assertInstanceOf('Intervention\Image\ImageManager', $this->api->getImageManager()); + $this->assertInstanceOf(ImageManager::class, $this->api->getImageManager()); } public function testSetManipulators() { - $this->api->setManipulators([Mockery::mock('League\Glide\Manipulators\ManipulatorInterface')]); + $this->api->setManipulators([\Mockery::mock(ManipulatorInterface::class)]); $manipulators = $this->api->getManipulators(); - $this->assertInstanceOf('League\Glide\Manipulators\ManipulatorInterface', $manipulators[0]); + $this->assertInstanceOf(ManipulatorInterface::class, $manipulators[0]); } public function testSetInvalidManipulator() { - $this->expectException(InvalidArgumentException::class); + $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Not a valid manipulator.'); - $this->api->setManipulators([new \StdClass()]); + $this->api->setManipulators([new \stdClass()]); } public function testGetManipulators() @@ -58,21 +60,21 @@ public function testGetManipulators() public function testRun() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { - $mock->shouldReceive('getEncoded')->andReturn('encoded'); + $image = \Mockery::mock(ImageInterface::class, function ($mock) { + $mock->shouldReceive('encodeByMediaType')->andReturn(\Mockery::mock(EncodedImageInterface::class, function ($mock) { + $mock->shouldReceive('toString')->andReturn('encoded'); + })); }); - $manager = Mockery::mock('Intervention\Image\ImageManager', function ($mock) use ($image) { - $mock->shouldReceive('make')->andReturn($image); - }); + $manager = ImageManager::gd(); - $manipulator = Mockery::mock('League\Glide\Manipulators\ManipulatorInterface', function ($mock) use ($image) { + $manipulator = \Mockery::mock(ManipulatorInterface::class, function ($mock) use ($image) { $mock->shouldReceive('setParams')->with([]); $mock->shouldReceive('run')->andReturn($image); }); $api = new Api($manager, [$manipulator]); - $this->assertEquals('encoded', $api->run('source', [])); + $this->assertEquals('encoded', $api->run(dirname(__FILE__, 2).'/files/red-pixel.png', [])); } } diff --git a/tests/Manipulators/BackgroundTest.php b/tests/Manipulators/BackgroundTest.php index 8fdd6c02..6276434a 100644 --- a/tests/Manipulators/BackgroundTest.php +++ b/tests/Manipulators/BackgroundTest.php @@ -2,7 +2,9 @@ namespace League\Glide\Manipulators; -use Mockery; +use Intervention\Image\Interfaces\DriverInterface; +use Intervention\Image\Interfaces\ImageInterface; +use Intervention\Image\Origin; use PHPUnit\Framework\TestCase; class BackgroundTest extends TestCase @@ -11,7 +13,7 @@ class BackgroundTest extends TestCase public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() @@ -21,19 +23,28 @@ public function testCreateInstance() public function testRun() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { + $originMock = \Mockery::mock(Origin::class, ['mediaType' => 'image/jpeg']); + $mock->shouldReceive('width')->andReturn(100)->once(); $mock->shouldReceive('height')->andReturn(100)->once(); - $mock->shouldReceive('getDriver')->andReturn(Mockery::mock('Intervention\Image\AbstractDriver', function ($mock) { - $mock->shouldReceive('newImage')->with(100, 100, 'rgba(0, 0, 0, 1)')->andReturn(Mockery::mock('Intervention\Image\Image', function ($mock) { - $mock->shouldReceive('insert')->andReturn($mock)->once(); + $mock->shouldReceive('origin')->andReturn($originMock)->once(); + + $mock->shouldReceive('driver')->andReturn(\Mockery::mock(DriverInterface::class, function ($mock) { + $mock->shouldReceive('createImage')->with(100, 100)->andReturn(\Mockery::mock(ImageInterface::class, function ($mock) { + $mock->shouldReceive('fill')->with('rgba(0, 0, 0, 1)')->andReturn(\Mockery::mock(ImageInterface::class, function ($mock) { + $mock->shouldReceive('setOrigin')->withArgs(function ($arg1) { + return $arg1 instanceof Origin; + })->andReturn($mock)->once(); + $mock->shouldReceive('place')->andReturn($mock)->once(); + }))->once(); }))->once(); }))->once(); }); $border = new Background(); - $this->assertInstanceOf('Intervention\Image\Image', $border->run($image)); - $this->assertInstanceOf('Intervention\Image\Image', $border->setParams(['bg' => 'black'])->run($image)); + $this->assertInstanceOf(ImageInterface::class, $border->run($image)); + $this->assertInstanceOf(ImageInterface::class, $border->setParams(['bg' => 'black'])->run($image)); } } diff --git a/tests/Manipulators/BlurTest.php b/tests/Manipulators/BlurTest.php index 6e15a536..f41543e3 100644 --- a/tests/Manipulators/BlurTest.php +++ b/tests/Manipulators/BlurTest.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Mockery; +use Intervention\Image\Interfaces\ImageInterface; use PHPUnit\Framework\TestCase; class BlurTest extends TestCase @@ -16,7 +16,7 @@ public function setUp(): void public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() @@ -26,12 +26,12 @@ public function testCreateInstance() public function testRun() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('blur')->with('10')->once(); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->setParams(['blur' => 10])->run($image) ); } diff --git a/tests/Manipulators/BorderTest.php b/tests/Manipulators/BorderTest.php index 3683c6a6..15deff9b 100644 --- a/tests/Manipulators/BorderTest.php +++ b/tests/Manipulators/BorderTest.php @@ -2,7 +2,8 @@ namespace League\Glide\Manipulators; -use Mockery; +use Intervention\Image\Geometry\Factories\RectangleFactory; +use Intervention\Image\Interfaces\ImageInterface; use PHPUnit\Framework\TestCase; class BorderTest extends TestCase @@ -11,7 +12,7 @@ class BorderTest extends TestCase public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() @@ -21,7 +22,7 @@ public function testCreateInstance() public function testGetBorder() { - $image = Mockery::mock('Intervention\Image\Image'); + $image = \Mockery::mock(ImageInterface::class); $border = new Border(); @@ -35,7 +36,7 @@ public function testGetBorder() public function testGetInvalidBorder() { - $image = Mockery::mock('Intervention\Image\Image'); + $image = \Mockery::mock(ImageInterface::class); $border = new Border(); @@ -46,7 +47,7 @@ public function testGetInvalidBorder() public function testGetWidth() { - $image = Mockery::mock('Intervention\Image\Image'); + $image = \Mockery::mock(ImageInterface::class); $border = new Border(); @@ -82,19 +83,26 @@ public function testGetDpr() public function testRunWithNoBorder() { - $image = Mockery::mock('Intervention\Image\Image'); + $image = \Mockery::mock(ImageInterface::class); $border = new Border(); - $this->assertInstanceOf('Intervention\Image\Image', $border->run($image)); + $this->assertInstanceOf(ImageInterface::class, $border->run($image)); } public function testRunOverlay() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('width')->andReturn(100)->once(); $mock->shouldReceive('height')->andReturn(100)->once(); - $mock->shouldReceive('rectangle')->with(5, 5, 95, 95, Mockery::on(function ($closure) { + $mock->shouldReceive('drawRectangle')->with(5, 5, \Mockery::on(function ($closure) { + $mock2 = \Mockery::mock(RectangleFactory::class, function ($mock2) { + $mock2->shouldReceive('size')->once(); + $mock2->shouldReceive('border')->once(); + }); + + $closure($mock2); + return true; }))->andReturn($mock)->once(); }); @@ -102,33 +110,33 @@ public function testRunOverlay() $border = new Border(); $border->setParams(['border' => '10,5000,overlay']); - $this->assertInstanceOf('Intervention\Image\Image', $border->run($image)); + $this->assertInstanceOf(ImageInterface::class, $border->run($image)); } public function testRunShrink() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('width')->andReturn(100)->once(); $mock->shouldReceive('height')->andReturn(100)->once(); $mock->shouldReceive('resize')->with(80, 80)->andReturn($mock)->once(); - $mock->shouldReceive('resizeCanvas')->with(20, 20, 'center', true, 'rgba(0, 0, 0, 0.5)')->andReturn($mock)->once(); + $mock->shouldReceive('resizeCanvasRelative')->with(20, 20, 'rgba(0, 0, 0, 0.5)', 'center')->andReturn($mock)->once(); }); $border = new Border(); $border->setParams(['border' => '10,5000,shrink']); - $this->assertInstanceOf('Intervention\Image\Image', $border->run($image)); + $this->assertInstanceOf(ImageInterface::class, $border->run($image)); } public function testRunExpand() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { - $mock->shouldReceive('resizeCanvas')->with(20, 20, 'center', true, 'rgba(0, 0, 0, 0.5)')->andReturn($mock)->once(); + $image = \Mockery::mock(ImageInterface::class, function ($mock) { + $mock->shouldReceive('resizeCanvasRelative')->with(20, 20, 'rgba(0, 0, 0, 0.5)', 'center')->andReturn($mock)->once(); }); $border = new Border(); $border->setParams(['border' => '10,5000,expand']); - $this->assertInstanceOf('Intervention\Image\Image', $border->run($image)); + $this->assertInstanceOf(ImageInterface::class, $border->run($image)); } } diff --git a/tests/Manipulators/BrightnessTest.php b/tests/Manipulators/BrightnessTest.php index 076ea54c..5f0a657f 100644 --- a/tests/Manipulators/BrightnessTest.php +++ b/tests/Manipulators/BrightnessTest.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Mockery; +use Intervention\Image\Interfaces\ImageInterface; use PHPUnit\Framework\TestCase; class BrightnessTest extends TestCase @@ -16,7 +16,7 @@ public function setUp(): void public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() @@ -26,12 +26,12 @@ public function testCreateInstance() public function testRun() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('brightness')->with('50')->once(); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->setParams(['bri' => 50])->run($image) ); } diff --git a/tests/Manipulators/ContrastTest.php b/tests/Manipulators/ContrastTest.php index 724a38cc..a814bd07 100644 --- a/tests/Manipulators/ContrastTest.php +++ b/tests/Manipulators/ContrastTest.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Mockery; +use Intervention\Image\Interfaces\ImageInterface; use PHPUnit\Framework\TestCase; class ContrastTest extends TestCase @@ -16,7 +16,7 @@ public function setUp(): void public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() @@ -26,12 +26,12 @@ public function testCreateInstance() public function testRun() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('contrast')->with('50')->once(); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->setParams(['con' => 50])->run($image) ); } diff --git a/tests/Manipulators/CropTest.php b/tests/Manipulators/CropTest.php index 98c4d570..627ad007 100644 --- a/tests/Manipulators/CropTest.php +++ b/tests/Manipulators/CropTest.php @@ -2,17 +2,18 @@ namespace League\Glide\Manipulators; -use Mockery; +use Intervention\Image\Interfaces\ImageInterface; use PHPUnit\Framework\TestCase; class CropTest extends TestCase { private $manipulator; + private $image; public function setUp(): void { $this->manipulator = new Crop(); - $this->image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $this->image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('width')->andReturn(100); $mock->shouldReceive('height')->andReturn(100); }); @@ -20,7 +21,7 @@ public function setUp(): void public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() @@ -33,7 +34,7 @@ public function testRun() $this->image->shouldReceive('crop')->with(100, 100, 0, 0)->once(); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->setParams(['crop' => '100,100,0,0'])->run($this->image) ); } diff --git a/tests/Manipulators/EncodeTest.php b/tests/Manipulators/EncodeTest.php index 8cb3b8c2..8b7080ed 100644 --- a/tests/Manipulators/EncodeTest.php +++ b/tests/Manipulators/EncodeTest.php @@ -2,8 +2,9 @@ namespace League\Glide\Manipulators; +use Intervention\Image\Encoders\MediaTypeEncoder; use Intervention\Image\ImageManager; -use Mockery; +use Intervention\Image\Interfaces\ImageInterface; use PHPUnit\Framework\TestCase; class EncodeTest extends TestCase @@ -18,17 +19,27 @@ class EncodeTest extends TestCase public function setUp(): void { - $manager = new ImageManager(); - $this->jpg = $manager->canvas(100, 100)->encode('jpg'); - $this->png = $manager->canvas(100, 100)->encode('png'); - $this->gif = $manager->canvas(100, 100)->encode('gif'); + $manager = ImageManager::gd(); + $this->jpg = $manager->read( + $manager->create(100, 100)->encode(new MediaTypeEncoder('image/jpeg'))->toFilePointer() + ); + $this->png = $manager->read( + $manager->create(100, 100)->encode(new MediaTypeEncoder('image/png'))->toFilePointer() + ); + $this->gif = $manager->read( + $manager->create(100, 100)->encode(new MediaTypeEncoder('image/gif'))->toFilePointer() + ); if (function_exists('imagecreatefromwebp')) { - $this->webp = $manager->canvas(100, 100)->encode('webp'); + $this->webp = $manager->read( + $manager->create(100, 100)->encode(new MediaTypeEncoder('image/webp'))->toFilePointer() + ); } if (function_exists('imagecreatefromavif')) { - $this->avif = $manager->canvas(100, 100)->encode('avif'); + $this->avif = $manager->read( + $manager->create(100, 100)->encode(new MediaTypeEncoder('image/avif'))->toFilePointer() + ); } $this->manipulator = new Encode(); @@ -36,7 +47,7 @@ public function setUp(): void public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() @@ -46,60 +57,61 @@ public function testCreateInstance() public function testRun() { - $this->assertSame('image/jpeg', $this->manipulator->setParams(['fm' => 'jpg'])->run($this->jpg)->mime); - $this->assertSame('image/jpeg', $this->manipulator->setParams(['fm' => 'jpg'])->run($this->png)->mime); - $this->assertSame('image/jpeg', $this->manipulator->setParams(['fm' => 'jpg'])->run($this->gif)->mime); - $this->assertSame('image/jpeg', $this->manipulator->setParams(['fm' => 'pjpg'])->run($this->jpg)->mime); - $this->assertSame('image/jpeg', $this->manipulator->setParams(['fm' => 'pjpg'])->run($this->png)->mime); - $this->assertSame('image/jpeg', $this->manipulator->setParams(['fm' => 'pjpg'])->run($this->gif)->mime); - $this->assertSame('image/png', $this->manipulator->setParams(['fm' => 'png'])->run($this->jpg)->mime); - $this->assertSame('image/png', $this->manipulator->setParams(['fm' => 'png'])->run($this->png)->mime); - $this->assertSame('image/png', $this->manipulator->setParams(['fm' => 'png'])->run($this->gif)->mime); - $this->assertSame('image/gif', $this->manipulator->setParams(['fm' => 'gif'])->run($this->jpg)->mime); - $this->assertSame('image/gif', $this->manipulator->setParams(['fm' => 'gif'])->run($this->png)->mime); - $this->assertSame('image/gif', $this->manipulator->setParams(['fm' => 'gif'])->run($this->gif)->mime); + $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'jpg'])->run($this->jpg))); + $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'jpg'])->run($this->png))); + $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'jpg'])->run($this->gif))); + $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'pjpg'])->run($this->jpg))); + $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'pjpg'])->run($this->png))); + $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'pjpg'])->run($this->gif))); + $this->assertSame('image/png', $this->getMime($this->manipulator->setParams(['fm' => 'png'])->run($this->jpg))); + $this->assertSame('image/png', $this->getMime($this->manipulator->setParams(['fm' => 'png'])->run($this->png))); + $this->assertSame('image/png', $this->getMime($this->manipulator->setParams(['fm' => 'png'])->run($this->gif))); + $this->assertSame('image/gif', $this->getMime($this->manipulator->setParams(['fm' => 'gif'])->run($this->jpg))); + $this->assertSame('image/gif', $this->getMime($this->manipulator->setParams(['fm' => 'gif'])->run($this->png))); + $this->assertSame('image/gif', $this->getMime($this->manipulator->setParams(['fm' => 'gif'])->run($this->gif))); if (function_exists('imagecreatefromwebp')) { - $this->assertSame('image/jpeg', $this->manipulator->setParams(['fm' => 'jpg'])->run($this->webp)->mime); - $this->assertSame('image/jpeg', $this->manipulator->setParams(['fm' => 'pjpg'])->run($this->webp)->mime); - $this->assertSame('image/png', $this->manipulator->setParams(['fm' => 'png'])->run($this->webp)->mime); - $this->assertSame('image/gif', $this->manipulator->setParams(['fm' => 'gif'])->run($this->webp)->mime); - $this->assertSame('image/webp', $this->manipulator->setParams(['fm' => 'webp'])->run($this->jpg)->mime); - $this->assertSame('image/webp', $this->manipulator->setParams(['fm' => 'webp'])->run($this->png)->mime); - $this->assertSame('image/webp', $this->manipulator->setParams(['fm' => 'webp'])->run($this->gif)->mime); - $this->assertSame('image/webp', $this->manipulator->setParams(['fm' => 'webp'])->run($this->webp)->mime); + $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'jpg'])->run($this->webp))); + $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'pjpg'])->run($this->webp))); + $this->assertSame('image/png', $this->getMime($this->manipulator->setParams(['fm' => 'png'])->run($this->webp))); + $this->assertSame('image/gif', $this->getMime($this->manipulator->setParams(['fm' => 'gif'])->run($this->webp))); + $this->assertSame('image/webp', $this->getMime($this->manipulator->setParams(['fm' => 'webp'])->run($this->jpg))); + $this->assertSame('image/webp', $this->getMime($this->manipulator->setParams(['fm' => 'webp'])->run($this->png))); + $this->assertSame('image/webp', $this->getMime($this->manipulator->setParams(['fm' => 'webp'])->run($this->gif))); + $this->assertSame('image/webp', $this->getMime($this->manipulator->setParams(['fm' => 'webp'])->run($this->webp))); } if (function_exists('imagecreatefromavif')) { - $this->assertSame('image/jpeg', $this->manipulator->setParams(['fm' => 'jpg'])->run($this->avif)->mime); - $this->assertSame('image/jpeg', $this->manipulator->setParams(['fm' => 'pjpg'])->run($this->avif)->mime); - $this->assertSame('image/png', $this->manipulator->setParams(['fm' => 'png'])->run($this->avif)->mime); - $this->assertSame('image/gif', $this->manipulator->setParams(['fm' => 'gif'])->run($this->avif)->mime); - $this->assertSame('image/avif', $this->manipulator->setParams(['fm' => 'avif'])->run($this->jpg)->mime); - $this->assertSame('image/avif', $this->manipulator->setParams(['fm' => 'avif'])->run($this->png)->mime); - $this->assertSame('image/avif', $this->manipulator->setParams(['fm' => 'avif'])->run($this->gif)->mime); - $this->assertSame('image/avif', $this->manipulator->setParams(['fm' => 'avif'])->run($this->avif)->mime); + $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'jpg'])->run($this->avif))); + $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'pjpg'])->run($this->avif))); + $this->assertSame('image/png', $this->getMime($this->manipulator->setParams(['fm' => 'png'])->run($this->avif))); + $this->assertSame('image/gif', $this->getMime($this->manipulator->setParams(['fm' => 'gif'])->run($this->avif))); + $this->assertSame('image/avif', $this->getMime($this->manipulator->setParams(['fm' => 'avif'])->run($this->jpg))); + $this->assertSame('image/avif', $this->getMime($this->manipulator->setParams(['fm' => 'avif'])->run($this->png))); + $this->assertSame('image/avif', $this->getMime($this->manipulator->setParams(['fm' => 'avif'])->run($this->gif))); + $this->assertSame('image/avif', $this->getMime($this->manipulator->setParams(['fm' => 'avif'])->run($this->avif))); } if (function_exists('imagecreatefromwebp') && function_exists('imagecreatefromavif')) { - $this->assertSame('image/webp', $this->manipulator->setParams(['fm' => 'webp'])->run($this->avif)->mime); - $this->assertSame('image/avif', $this->manipulator->setParams(['fm' => 'avif'])->run($this->webp)->mime); + $this->assertSame('image/webp', $this->getMime($this->manipulator->setParams(['fm' => 'webp'])->run($this->avif))); + $this->assertSame('image/avif', $this->getMime($this->manipulator->setParams(['fm' => 'avif'])->run($this->webp))); } } public function testGetFormat() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { - $mock->shouldReceive('mime')->andReturn('image/jpeg')->once(); - $mock->shouldReceive('mime')->andReturn('image/png')->once(); - $mock->shouldReceive('mime')->andReturn('image/gif')->once(); - $mock->shouldReceive('mime')->andReturn('image/bmp')->once(); - $mock->shouldReceive('mime')->andReturn('image/jpeg')->twice(); + $image = \Mockery::mock(ImageInterface::class, function ($mock) { + $mock->shouldReceive('origin')->andReturn(\Mockery::mock('Intervention\Image\Origin', ['mediaType' => 'image/jpeg']))->once(); + $mock->shouldReceive('origin')->andReturn(\Mockery::mock('Intervention\Image\Origin', ['mediaType' => 'image/png']))->once(); + $mock->shouldReceive('origin')->andReturn(\Mockery::mock('Intervention\Image\Origin', ['mediaType' => 'image/gif']))->once(); + $mock->shouldReceive('origin')->andReturn(\Mockery::mock('Intervention\Image\Origin', ['mediaType' => 'image/bmp']))->once(); + $mock->shouldReceive('origin')->andReturn(\Mockery::mock('Intervention\Image\Origin', ['mediaType' => 'image/jpeg']))->twice(); if (function_exists('imagecreatefromwebp')) { - $mock->shouldReceive('mime')->andReturn('image/webp')->once(); + $mock->shouldReceive('origin')->andReturn(\Mockery::mock('Intervention\Image\Origin', ['mediaType' => 'image/webp']))->once(); } + if (function_exists('imagecreatefromavif')) { - $mock->shouldReceive('mime')->andReturn('image/avif')->once(); + $mock->shouldReceive('origin')->andReturn(\Mockery::mock('Intervention\Image\Origin', ['mediaType' => 'image/avif']))->once(); } }); @@ -145,15 +157,20 @@ public function testWithImagick() 'The imagick extension is not available.' ); } - $manager = new ImageManager(['driver' => 'imagick']); - //These need to be recreated with the imagick driver selected in the manager - $this->jpg = $manager->canvas(100, 100)->encode('jpg'); - $this->png = $manager->canvas(100, 100)->encode('png'); - $this->gif = $manager->canvas(100, 100)->encode('gif'); - $this->tif = $manager->canvas(100, 100)->encode('tiff'); - - $this->assertSame('image/tiff', $this->manipulator->setParams(['fm' => 'tiff'])->run($this->jpg)->mime); - $this->assertSame('image/tiff', $this->manipulator->setParams(['fm' => 'tiff'])->run($this->png)->mime); - $this->assertSame('image/tiff', $this->manipulator->setParams(['fm' => 'tiff'])->run($this->gif)->mime); + $manager = ImageManager::imagick(); + // These need to be recreated with the imagick driver selected in the manager + $this->jpg = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/jpeg'))->toFilePointer()); + $this->png = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/png'))->toFilePointer()); + $this->gif = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/gif'))->toFilePointer()); + $this->tif = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/tiff'))->toFilePointer()); + + $this->assertSame('image/tiff', $this->getMime($this->manipulator->setParams(['fm' => 'tiff'])->run($this->jpg))); + $this->assertSame('image/tiff', $this->getMime($this->manipulator->setParams(['fm' => 'tiff'])->run($this->png))); + $this->assertSame('image/tiff', $this->getMime($this->manipulator->setParams(['fm' => 'tiff'])->run($this->gif))); + } + + public function getMime(ImageInterface $image) + { + return $image->origin()->mediaType(); } } diff --git a/tests/Manipulators/FilterTest.php b/tests/Manipulators/FilterTest.php index 6f4ff4cb..a49833e8 100644 --- a/tests/Manipulators/FilterTest.php +++ b/tests/Manipulators/FilterTest.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Mockery; +use Intervention\Image\Interfaces\ImageInterface; use PHPUnit\Framework\TestCase; class FilterTest extends TestCase @@ -16,7 +16,7 @@ public function setUp(): void public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() @@ -26,7 +26,7 @@ public function testCreateInstance() public function testRun() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('greyscale')->twice()->andReturn($mock) ->shouldReceive('brightness')->with(-10)->twice()->andReturn($mock) ->shouldReceive('contrast')->with(10)->twice()->andReturn($mock) @@ -34,36 +34,36 @@ public function testRun() }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->setParams(['filt' => 'greyscale'])->run($image) ); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->setParams(['filt' => 'sepia'])->run($image) ); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->setParams([])->run($image) ); } public function testRunGreyscaleFilter() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('greyscale')->andReturn($mock)->once(); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->runGreyscaleFilter($image) ); } public function testRunSepiaFilter() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('greyscale')->once()->andReturn($mock) ->shouldReceive('brightness')->with(-10)->twice()->andReturn($mock) ->shouldReceive('contrast')->with(10)->twice()->andReturn($mock) @@ -71,7 +71,7 @@ public function testRunSepiaFilter() }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->runSepiaFilter($image) ); } diff --git a/tests/Manipulators/FlipTest.php b/tests/Manipulators/FlipTest.php index 77c50071..aa7fec15 100644 --- a/tests/Manipulators/FlipTest.php +++ b/tests/Manipulators/FlipTest.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Mockery; +use Intervention\Image\Interfaces\ImageInterface; use PHPUnit\Framework\TestCase; class FlipTest extends TestCase @@ -16,7 +16,7 @@ public function setUp(): void public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() @@ -26,22 +26,35 @@ public function testCreateInstance() public function testRun() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { - $mock->shouldReceive('flip')->andReturn($mock)->with('h')->once(); - $mock->shouldReceive('flip')->andReturn($mock)->with('v')->once(); + $image = \Mockery::mock(ImageInterface::class, function ($mock) { + $mock->shouldReceive('flip')->andReturn($mock)->once(); + $mock->shouldReceive('flop')->andReturn($mock)->once(); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->setParams(['flip' => 'h'])->run($image) ); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->setParams(['flip' => 'v'])->run($image) ); } + public function testRunBoth() + { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { + $mock->shouldReceive('flip')->andReturn($mock)->once(); + $mock->shouldReceive('flop')->andReturn($mock)->once(); + }); + + $this->assertInstanceOf( + ImageInterface::class, + $this->manipulator->setParams(['flip' => 'both'])->run($image) + ); + } + public function testGetFlip() { $this->assertSame('h', $this->manipulator->setParams(['flip' => 'h'])->getFlip()); diff --git a/tests/Manipulators/GammaTest.php b/tests/Manipulators/GammaTest.php index 90c8245b..a1907ccd 100644 --- a/tests/Manipulators/GammaTest.php +++ b/tests/Manipulators/GammaTest.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Mockery; +use Intervention\Image\Interfaces\ImageInterface; use PHPUnit\Framework\TestCase; class GammaTest extends TestCase @@ -16,7 +16,7 @@ public function setUp(): void public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() @@ -26,12 +26,12 @@ public function testCreateInstance() public function testRun() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('gamma')->with('1.5')->once(); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->setParams(['gam' => '1.5'])->run($image) ); } diff --git a/tests/Manipulators/Helpers/DimensionTest.php b/tests/Manipulators/Helpers/DimensionTest.php index 0d45b17c..021fca68 100644 --- a/tests/Manipulators/Helpers/DimensionTest.php +++ b/tests/Manipulators/Helpers/DimensionTest.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators\Helpers; -use Mockery; +use Intervention\Image\Interfaces\ImageInterface; use PHPUnit\Framework\TestCase; class DimensionTest extends TestCase @@ -11,12 +11,12 @@ class DimensionTest extends TestCase public function setUp(): void { - $this->image = Mockery::mock('Intervention\Image\Image'); + $this->image = \Mockery::mock(ImageInterface::class); } public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testPixels() diff --git a/tests/Manipulators/OrientationTest.php b/tests/Manipulators/OrientationTest.php index 2a46ebe0..bcc01a5b 100644 --- a/tests/Manipulators/OrientationTest.php +++ b/tests/Manipulators/OrientationTest.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Mockery; +use Intervention\Image\Interfaces\ImageInterface; use PHPUnit\Framework\TestCase; class OrientationTest extends TestCase @@ -16,7 +16,7 @@ public function setUp(): void public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() @@ -26,18 +26,20 @@ public function testCreateInstance() public function testRun() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { - $mock->shouldReceive('orientate')->andReturn($mock)->once(); + $image = \Mockery::mock(ImageInterface::class, function ($mock) { + $mock->shouldReceive('exif')->withArgs(['Orientation'])->andReturn(null)->twice(); + + $mock->shouldReceive('rotate')->andReturn($mock)->with('0')->once(); $mock->shouldReceive('rotate')->andReturn($mock)->with('90')->once(); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->setParams(['or' => 'auto'])->run($image) ); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->setParams(['or' => '90'])->run($image) ); } diff --git a/tests/Manipulators/PixelateTest.php b/tests/Manipulators/PixelateTest.php index 3a093db9..04abd105 100644 --- a/tests/Manipulators/PixelateTest.php +++ b/tests/Manipulators/PixelateTest.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Mockery; +use Intervention\Image\Interfaces\ImageInterface; use PHPUnit\Framework\TestCase; class PixelateTest extends TestCase @@ -16,7 +16,7 @@ public function setUp(): void public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() @@ -26,12 +26,12 @@ public function testCreateInstance() public function testRun() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('pixelate')->with('10')->once(); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->setParams(['pixel' => '10'])->run($image) ); } diff --git a/tests/Manipulators/SharpenTest.php b/tests/Manipulators/SharpenTest.php index d443089f..b6795120 100644 --- a/tests/Manipulators/SharpenTest.php +++ b/tests/Manipulators/SharpenTest.php @@ -2,7 +2,7 @@ namespace League\Glide\Manipulators; -use Mockery; +use Intervention\Image\Interfaces\ImageInterface; use PHPUnit\Framework\TestCase; class SharpenTest extends TestCase @@ -16,7 +16,7 @@ public function setUp(): void public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() @@ -26,12 +26,12 @@ public function testCreateInstance() public function testRun() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('sharpen')->with('10')->once(); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->setParams(['sharp' => '10'])->run($image) ); } diff --git a/tests/Manipulators/SizeTest.php b/tests/Manipulators/SizeTest.php index 9d9c074d..f8fbdeeb 100644 --- a/tests/Manipulators/SizeTest.php +++ b/tests/Manipulators/SizeTest.php @@ -2,25 +2,21 @@ namespace League\Glide\Manipulators; -use Mockery; +use Intervention\Image\Interfaces\ImageInterface; use PHPUnit\Framework\TestCase; class SizeTest extends TestCase { private $manipulator; - private $callback; public function setUp(): void { $this->manipulator = new Size(); - $this->callback = Mockery::on(function () { - return true; - }); } public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() @@ -41,14 +37,14 @@ public function testGetMaxImageSize() public function testRun() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('width')->andReturn('200')->twice(); $mock->shouldReceive('height')->andReturn('200')->once(); - $mock->shouldReceive('resize')->with(100, 100, $this->callback)->andReturn($mock)->once(); + $mock->shouldReceive('scale')->with(100, 100)->andReturn($mock)->once(); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->setParams(['w' => 100])->run($image) ); } @@ -117,7 +113,7 @@ public function testGetDpr() public function testResolveMissingDimensions() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('width')->andReturn(400); $mock->shouldReceive('height')->andReturn(200); }); @@ -129,7 +125,7 @@ public function testResolveMissingDimensions() public function testResolveMissingDimensionsWithOddDimensions() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('width')->andReturn(1024); $mock->shouldReceive('height')->andReturn(553); }); @@ -147,132 +143,133 @@ public function testLimitImageSize() public function testRunResize() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('width')->andReturn(100)->times(4); $mock->shouldReceive('height')->andReturn(100)->times(4); $mock->shouldReceive('crop')->andReturn($mock)->once(); - $mock->shouldReceive('resize')->with(100, 100, $this->callback)->andReturn($mock)->times(5); + $mock->shouldReceive('contain')->andReturn($mock)->once(); $mock->shouldReceive('resize')->with(100, 100)->andReturn($mock)->once(); - $mock->shouldReceive('resizeCanvas')->with(100, 100, 'center')->andReturn($mock)->times(2); + $mock->shouldReceive('pad')->andReturn($mock)->once(); + $mock->shouldReceive('scaleDown')->with(100, 100)->andReturn($mock)->times(1); + $mock->shouldReceive('scale')->with(100, 100)->andReturn($mock)->times(2); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->runResize($image, 'contain', 100, 100) ); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->runResize($image, 'fill', 100, 100) ); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->runResize($image, 'fill-max', 100, 100) ); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->runResize($image, 'max', 100, 100) ); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->runResize($image, 'stretch', 100, 100) ); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->runResize($image, 'crop', 100, 100) ); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->runResize($image, 'invalid', 100, 100) ); } public function testRunContainResize() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { - $mock->shouldReceive('resize')->with(100, 100, $this->callback)->andReturn($mock)->once(); + $image = \Mockery::mock(ImageInterface::class, function ($mock) { + $mock->shouldReceive('scale')->with(100, 100)->andReturn($mock)->once(); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->runContainResize($image, 100, 100) ); } public function testRunFillResize() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { - $mock->shouldReceive('resize')->with(100, 100, $this->callback)->andReturn($mock)->once(); - $mock->shouldReceive('resizeCanvas')->with(100, 100, 'center')->andReturn($mock)->once(); + $image = \Mockery::mock(ImageInterface::class, function ($mock) { + $mock->shouldReceive('pad')->with(100, 100)->andReturn($mock)->once(); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->runFillResize($image, 100, 100) ); } public function testRunMaxResize() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { - $mock->shouldReceive('resize')->with(100, 100, $this->callback)->andReturn($mock)->once(); + $image = \Mockery::mock(ImageInterface::class, function ($mock) { + $mock->shouldReceive('scaleDown')->with(100, 100)->andReturn($mock)->once(); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->runMaxResize($image, 100, 100) ); } public function testRunStretchResize() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('resize')->with(100, 100)->andReturn($mock)->once(); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->runStretchResize($image, 100, 100) ); } public function testRunCropResize() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('width')->andReturn(100)->times(4); $mock->shouldReceive('height')->andReturn(100)->times(4); - $mock->shouldReceive('resize')->with(100, 100, $this->callback)->andReturn($mock)->once(); + $mock->shouldReceive('scale')->with(100, 100)->andReturn($mock)->once(); $mock->shouldReceive('crop')->with(100, 100, 0, 0)->andReturn($mock)->once(); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->runCropResize($image, 100, 100, 'center') ); } public function testResizeDoesNotRunWhenNoParamsAreSet() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('width')->andReturn(100)->twice(); $mock->shouldReceive('height')->andReturn(100)->twice(); $mock->shouldReceive('resize')->never(); }); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->run($image) ); } public function testResizeDoesNotRunWhenSettingFitCropToCenterWithNoZoom() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('width')->andReturn(100)->twice(); $mock->shouldReceive('height')->andReturn(100)->twice(); $mock->shouldReceive('resize')->never(); @@ -281,24 +278,24 @@ public function testResizeDoesNotRunWhenSettingFitCropToCenterWithNoZoom() $this->manipulator->setParams(['fit' => 'crop-50-50-1']); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->run($image) ); } public function testResizeDoesRunWhenDimensionsAreTheSameAndTheCropZoomIsNotDefaultOne() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('width')->andReturn(100); $mock->shouldReceive('height')->andReturn(100); - $mock->shouldReceive('resize')->once(); + $mock->shouldReceive('scale')->once(); $mock->shouldReceive('crop')->once()->andReturn($mock); }); $this->manipulator->setParams(['fit' => 'crop-50-50-3.2']); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->run($image) ); } diff --git a/tests/Manipulators/WatermarkTest.php b/tests/Manipulators/WatermarkTest.php index c64ef7b0..176686f8 100644 --- a/tests/Manipulators/WatermarkTest.php +++ b/tests/Manipulators/WatermarkTest.php @@ -2,8 +2,9 @@ namespace League\Glide\Manipulators; +use Intervention\Image\Interfaces\DriverInterface; +use Intervention\Image\Interfaces\ImageInterface; use League\Glide\Filesystem\FilesystemException; -use Mockery; use PHPUnit\Framework\TestCase; class WatermarkTest extends TestCase @@ -13,13 +14,13 @@ class WatermarkTest extends TestCase public function setUp(): void { $this->manipulator = new Watermark( - Mockery::mock('League\Flysystem\FilesystemOperator') + \Mockery::mock('League\Flysystem\FilesystemOperator') ); } public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() @@ -29,7 +30,7 @@ public function testCreateInstance() public function testSetWatermarks() { - $this->manipulator->setWatermarks(Mockery::mock('League\Flysystem\FilesystemOperator')); + $this->manipulator->setWatermarks(\Mockery::mock('League\Flysystem\FilesystemOperator')); $this->assertInstanceOf('League\Flysystem\FilesystemOperator', $this->manipulator->getWatermarks()); } @@ -51,17 +52,17 @@ public function testGetWatermarksPathPrefix() public function testRun() { - $image = Mockery::mock('Intervention\Image\Image', function ($mock) { - $mock->shouldReceive('insert')->once(); - $mock->shouldReceive('getDriver')->andReturn(Mockery::mock('Intervention\Image\AbstractDriver', function ($mock) { - $mock->shouldReceive('init')->with('content')->andReturn(Mockery::mock('Intervention\Image\Image', function ($mock) { + $image = \Mockery::mock(ImageInterface::class, function ($mock) { + $mock->shouldReceive('place')->once(); + $mock->shouldReceive('driver')->andReturn(\Mockery::mock(DriverInterface::class, function ($mock) { + $mock->shouldReceive('handleInput')->with('content')->andReturn(\Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('width')->andReturn(0)->once(); - $mock->shouldReceive('resize')->once(); + $mock->shouldReceive('scale')->once(); }))->once(); }))->once(); }); - $this->manipulator->setWatermarks(Mockery::mock('League\Flysystem\FilesystemOperator', function ($watermarks) { + $this->manipulator->setWatermarks(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($watermarks) { $watermarks->shouldReceive('fileExists')->with('image.jpg')->andReturn(true)->once(); $watermarks->shouldReceive('read')->with('image.jpg')->andReturn('content')->once(); })); @@ -74,7 +75,7 @@ public function testRun() ]); $this->assertInstanceOf( - 'Intervention\Image\Image', + ImageInterface::class, $this->manipulator->run($image) ); } @@ -96,14 +97,14 @@ public function testGetImage() $this->manipulator->setWatermarksPathPrefix('watermarks'); - $driver = Mockery::mock('Intervention\Image\AbstractDriver'); - $driver->shouldReceive('init') + $driver = \Mockery::mock(DriverInterface::class); + $driver->shouldReceive('handleInput') ->with('content') - ->andReturn(Mockery::mock('Intervention\Image\Image')) + ->andReturn(\Mockery::mock(ImageInterface::class)) ->once(); - $image = Mockery::mock('Intervention\Image\Image'); - $image->shouldReceive('getDriver') + $image = \Mockery::mock(ImageInterface::class); + $image->shouldReceive('driver') ->andReturn($driver) ->once(); @@ -125,21 +126,21 @@ public function testGetImageWithUnreadableSource() ->andThrow('League\Flysystem\UnableToReadFile') ->once(); - $image = Mockery::mock('Intervention\Image\Image'); + $image = \Mockery::mock(ImageInterface::class); $this->manipulator->setParams(['mark' => 'image.jpg'])->getImage($image); } public function testGetImageWithoutMarkParam() { - $image = Mockery::mock('Intervention\Image\Image'); + $image = \Mockery::mock(ImageInterface::class); $this->assertNull($this->manipulator->getImage($image)); } public function testGetImageWithEmptyMarkParam() { - $image = Mockery::mock('Intervention\Image\Image'); + $image = \Mockery::mock(ImageInterface::class); $this->assertNull($this->manipulator->setParams(['mark' => ''])->getImage($image)); } @@ -148,14 +149,14 @@ public function testGetImageWithoutWatermarksFilesystem() { $this->manipulator->setWatermarks(null); - $image = Mockery::mock('Intervention\Image\Image'); + $image = \Mockery::mock(ImageInterface::class); $this->assertNull($this->manipulator->setParams(['mark' => 'image.jpg'])->getImage($image)); } public function testGetDimension() { - $image = Mockery::mock('Intervention\Image\Image'); + $image = \Mockery::mock(ImageInterface::class); $image->shouldReceive('width')->andReturn(2000); $image->shouldReceive('height')->andReturn(1000); diff --git a/tests/Responses/PsrResponseFactoryTest.php b/tests/Responses/PsrResponseFactoryTest.php index 0bf65bcd..68b3fedb 100644 --- a/tests/Responses/PsrResponseFactoryTest.php +++ b/tests/Responses/PsrResponseFactoryTest.php @@ -2,14 +2,13 @@ namespace League\Glide\Responses; -use Mockery; use PHPUnit\Framework\TestCase; class PsrResponseFactoryTest extends TestCase { public function testCreateInstance() { - $response = Mockery::mock('Psr\Http\Message\ResponseInterface'); + $response = \Mockery::mock('Psr\Http\Message\ResponseInterface'); $streamCallback = function () { }; @@ -21,7 +20,7 @@ public function testCreateInstance() public function testCreate() { - $response = Mockery::mock('Psr\Http\Message\ResponseInterface', function ($mock) { + $response = \Mockery::mock('Psr\Http\Message\ResponseInterface', function ($mock) { $mock->shouldReceive('withBody')->andReturn($mock)->once(); $mock->shouldReceive('withHeader')->andReturn($mock)->times(4); }); @@ -30,11 +29,11 @@ public function testCreate() return $stream; }; - $cache = Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $cache = \Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('mimeType')->andReturn('image/jpeg'); $mock->shouldReceive('fileSize')->andReturn(0); $mock->shouldReceive('readStream')->andReturn( - Mockery::mock('Psr\Http\Message\StreamInterface') + \Mockery::mock('Psr\Http\Message\StreamInterface') ); }); diff --git a/tests/ServerFactoryTest.php b/tests/ServerFactoryTest.php index 8d71f547..74350c1d 100644 --- a/tests/ServerFactoryTest.php +++ b/tests/ServerFactoryTest.php @@ -2,8 +2,6 @@ namespace League\Glide; -use InvalidArgumentException; -use Mockery; use PHPUnit\Framework\TestCase; class ServerFactoryTest extends TestCase @@ -16,9 +14,9 @@ public function testCreateServerFactory() public function testGetServer() { $server = new ServerFactory([ - 'source' => Mockery::mock('League\Flysystem\FilesystemOperator'), - 'cache' => Mockery::mock('League\Flysystem\FilesystemOperator'), - 'response' => Mockery::mock('League\Glide\Responses\ResponseFactoryInterface'), + 'source' => \Mockery::mock('League\Flysystem\FilesystemOperator'), + 'cache' => \Mockery::mock('League\Flysystem\FilesystemOperator'), + 'response' => \Mockery::mock('League\Glide\Responses\ResponseFactoryInterface'), ]); $this->assertInstanceOf('League\Glide\Server', $server->getServer()); @@ -27,7 +25,7 @@ public function testGetServer() public function testGetSource() { $server = new ServerFactory([ - 'source' => Mockery::mock('League\Flysystem\FilesystemOperator'), + 'source' => \Mockery::mock('League\Flysystem\FilesystemOperator'), ]); $this->assertInstanceOf('League\Flysystem\FilesystemOperator', $server->getSource()); @@ -41,7 +39,7 @@ public function testGetSource() public function testGetSourceWithNoneSet() { - $this->expectException(InvalidArgumentException::class); + $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('A "source" file system must be set.'); $server = new ServerFactory(); @@ -60,7 +58,7 @@ public function testGetSourcePathPrefix() public function testGetCache() { $server = new ServerFactory([ - 'cache' => Mockery::mock('League\Flysystem\FilesystemOperator'), + 'cache' => \Mockery::mock('League\Flysystem\FilesystemOperator'), ]); $this->assertInstanceOf('League\Flysystem\FilesystemOperator', $server->getCache()); @@ -74,7 +72,7 @@ public function testGetCache() public function testGetCacheWithNoneSet() { - $this->expectException(InvalidArgumentException::class); + $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('A "cache" file system must be set.'); $server = new ServerFactory(); @@ -128,7 +126,7 @@ public function testGetCacheWithFileExtensions() public function testGetWatermarks() { $server = new ServerFactory([ - 'watermarks' => Mockery::mock('League\Flysystem\FilesystemOperator'), + 'watermarks' => \Mockery::mock('League\Flysystem\FilesystemOperator'), ]); $this->assertInstanceOf('League\Flysystem\FilesystemOperator', $server->getWatermarks()); @@ -156,7 +154,7 @@ public function testGetApi() $this->assertInstanceOf('League\Glide\Api\Api', $server->getApi()); } - public function testGetImageManager() + public function testGetImageManagerWithImagick() { $server = new ServerFactory([ 'driver' => 'imagick', @@ -164,7 +162,16 @@ public function testGetImageManager() $imageManager = $server->getImageManager(); $this->assertInstanceOf('Intervention\Image\ImageManager', $imageManager); - $this->assertSame('imagick', $imageManager->config['driver']); + } + + public function testGetImageManagerWithGd() + { + $server = new ServerFactory([ + 'driver' => 'gd', + ]); + $imageManager = $server->getImageManager(); + + $this->assertInstanceOf('Intervention\Image\ImageManager', $imageManager); } public function testGetImageManagerWithNoneSet() @@ -173,7 +180,6 @@ public function testGetImageManagerWithNoneSet() $imageManager = $server->getImageManager(); $this->assertInstanceOf('Intervention\Image\ImageManager', $imageManager); - $this->assertSame('gd', $imageManager->config['driver']); } public function testGetManipulators() @@ -234,7 +240,7 @@ public function testGetBaseUrl() public function testGetResponseFactory() { $server = new ServerFactory([ - 'response' => Mockery::mock('League\Glide\Responses\ResponseFactoryInterface'), + 'response' => \Mockery::mock('League\Glide\Responses\ResponseFactoryInterface'), ]); $this->assertInstanceOf('League\Glide\Responses\ResponseFactoryInterface', $server->getResponseFactory()); @@ -250,9 +256,9 @@ public function testGetResponseFactoryWithNoneSet() public function testCreate() { $server = ServerFactory::create([ - 'source' => Mockery::mock('League\Flysystem\FilesystemOperator'), - 'cache' => Mockery::mock('League\Flysystem\FilesystemOperator'), - 'response' => Mockery::mock('League\Glide\Responses\ResponseFactoryInterface'), + 'source' => \Mockery::mock('League\Flysystem\FilesystemOperator'), + 'cache' => \Mockery::mock('League\Flysystem\FilesystemOperator'), + 'response' => \Mockery::mock('League\Glide\Responses\ResponseFactoryInterface'), 'temp_dir' => __DIR__, ]); diff --git a/tests/ServerTest.php b/tests/ServerTest.php index bd58a63a..53c6fe87 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -3,10 +3,8 @@ namespace League\Glide; use Hamcrest\Matchers; -use InvalidArgumentException; use League\Glide\Filesystem\FileNotFoundException; use League\Glide\Filesystem\FilesystemException; -use Mockery; use PHPUnit\Framework\TestCase; class ServerTest extends TestCase @@ -15,9 +13,9 @@ class ServerTest extends TestCase public function setUp(): void { - $response = Mockery::mock('Psr\Http\Message\ResponseInterface'); + $response = \Mockery::mock('Psr\Http\Message\ResponseInterface'); - $responseFactory = Mockery::mock('League\Glide\Responses\ResponseFactoryInterface'); + $responseFactory = \Mockery::mock('League\Glide\Responses\ResponseFactoryInterface'); $responseFactory ->shouldReceive('create') ->andReturn($response) @@ -27,16 +25,16 @@ public function setUp(): void }); $this->server = new Server( - Mockery::mock('League\Flysystem\FilesystemOperator'), - Mockery::mock('League\Flysystem\FilesystemOperator'), - Mockery::mock('League\Glide\Api\ApiInterface'), + \Mockery::mock('League\Flysystem\FilesystemOperator'), + \Mockery::mock('League\Flysystem\FilesystemOperator'), + \Mockery::mock('League\Glide\Api\ApiInterface'), $responseFactory ); } public function tearDown(): void { - Mockery::close(); + \Mockery::close(); } public function testCreateInstance() @@ -46,7 +44,7 @@ public function testCreateInstance() public function testSetSource() { - $this->server->setSource(Mockery::mock('League\Flysystem\FilesystemOperator')); + $this->server->setSource(\Mockery::mock('League\Flysystem\FilesystemOperator')); $this->assertInstanceOf('League\Flysystem\FilesystemOperator', $this->server->getSource()); } @@ -102,7 +100,7 @@ public function testGetSourcePathWithEncodedEntities() public function testSourceFileExists() { - $this->server->setSource(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setSource(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->with('image.jpg')->andReturn(true)->once(); })); @@ -122,7 +120,7 @@ public function testGetBaseUrl() public function testSetCache() { - $this->server->setCache(Mockery::mock('League\Flysystem\FilesystemOperator')); + $this->server->setCache(\Mockery::mock('League\Flysystem\FilesystemOperator')); $this->assertInstanceOf('League\Flysystem\FilesystemOperator', $this->server->getCache()); } @@ -315,7 +313,7 @@ public function testGetCachePathWithExtensionAndPjpgFmFromPreset() public function testCacheFileExists() { - $this->server->setCache(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setCache(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->with('image.jpg/75094881e9fd2b93063d6a5cb083091c')->andReturn(true)->once(); })); @@ -324,7 +322,7 @@ public function testCacheFileExists() public function testDeleteCache() { - $this->server->setCache(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setCache(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('deleteDirectory')->with('image.jpg')->andReturn(true)->once(); })); @@ -333,7 +331,7 @@ public function testDeleteCache() public function testDeleteCacheWithGroupCacheInFoldersDisabled() { - $this->expectException(InvalidArgumentException::class); + $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Deleting cached image manipulations is not possible when grouping cache into folders is disabled.'); $this->server->setGroupCacheInFolders(false); @@ -343,7 +341,7 @@ public function testDeleteCacheWithGroupCacheInFoldersDisabled() public function testSetApi() { - $api = Mockery::mock('League\Glide\Api\ApiInterface'); + $api = \Mockery::mock('League\Glide\Api\ApiInterface'); $this->server->setApi($api); $this->assertInstanceOf('League\Glide\Api\ApiInterface', $this->server->getApi()); } @@ -419,7 +417,7 @@ public function testGetAllParams() public function testSetResponseFactory() { - $this->server->setResponseFactory(Mockery::mock('League\Glide\Responses\ResponseFactoryInterface')); + $this->server->setResponseFactory(\Mockery::mock('League\Glide\Responses\ResponseFactoryInterface')); $this->assertInstanceOf( 'League\Glide\Responses\ResponseFactoryInterface', @@ -434,11 +432,11 @@ public function testGetResponseFactory() public function testGetImageResponse() { - $this->server->setResponseFactory(Mockery::mock('League\Glide\Responses\ResponseFactoryInterface', function ($mock) { - $mock->shouldReceive('create')->andReturn(Mockery::mock('Psr\Http\Message\ResponseInterface')); + $this->server->setResponseFactory(\Mockery::mock('League\Glide\Responses\ResponseFactoryInterface', function ($mock) { + $mock->shouldReceive('create')->andReturn(\Mockery::mock('Psr\Http\Message\ResponseInterface')); })); - $this->server->setCache(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setCache(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->andReturn(true); })); @@ -450,7 +448,7 @@ public function testGetImageResponse() public function testGetImageResponseWithoutResponseFactory() { - $this->expectException(InvalidArgumentException::class); + $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Unable to get image response, no response factory defined.'); $this->server->getImageResponse('image.jpg', []); @@ -458,7 +456,7 @@ public function testGetImageResponseWithoutResponseFactory() public function testGetImageAsBase64() { - $this->server->setCache(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setCache(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->andReturn(true); $mock->shouldReceive('mimeType')->andReturn('image/jpeg'); $mock->shouldReceive('read')->andReturn('content')->once(); @@ -475,7 +473,7 @@ public function testGetImageAsBase64WithUnreadableSource() $this->expectException(FilesystemException::class); $this->expectExceptionMessage('Could not read the image `image.jpg/75094881e9fd2b93063d6a5cb083091c`.'); - $this->server->setCache(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setCache(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->andReturn(true); $mock->shouldReceive('mimeType')->andReturn('image/jpeg'); $mock->shouldReceive('read')->andThrow('League\Flysystem\UnableToReadFile')->once(); @@ -489,7 +487,7 @@ public function testGetImageAsBase64WithUnreadableSource() */ public function testOutputImage() { - $this->server->setCache(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setCache(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->andReturn(true); $mock->shouldReceive('mimeType')->andReturn('image/jpeg'); $mock->shouldReceive('fileSize')->andReturn(0); @@ -509,19 +507,19 @@ public function testOutputImage() public function testMakeImageFromSource() { - $this->server->setSource(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setSource(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->andReturn(true)->once(); $mock->shouldReceive('read')->andReturn('content')->once(); })); - $this->server->setCache(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setCache(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->andReturn(false)->once(); - $mock->shouldReceive('write')->with('image.jpg/75094881e9fd2b93063d6a5cb083091c', 'content')->once(); + $mock->shouldReceive('write')->withArgs(['image.jpg/75094881e9fd2b93063d6a5cb083091c', 'content'])->once(); })); - $this->server->setApi(Mockery::mock('League\Glide\Api\ApiInterface', function ($mock) { - $tmpDirPattern = Matchers::matchesPattern('~^'.sys_get_temp_dir().'.*~'); - $mock->shouldReceive('run')->with($tmpDirPattern, [])->andReturn('content')->once(); + $this->server->setApi(\Mockery::mock('League\Glide\Api\ApiInterface', function ($mock) { + $tmpDirPattern = Matchers::matchesPattern('~\/?'.sys_get_temp_dir().'.*~'); + $mock->shouldReceive('run')->withArgs([$tmpDirPattern, []])->andReturn('content')->once(); })); $this->assertEquals( @@ -532,18 +530,18 @@ public function testMakeImageFromSource() public function testMakeImageFromSourceWithCustomTmpDir() { - $this->server->setSource(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setSource(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->andReturn(true)->once(); $mock->shouldReceive('read')->andReturn('content')->once(); })); - $this->server->setCache(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setCache(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->andReturn(false)->once(); $mock->shouldReceive('write')->with('image.jpg/75094881e9fd2b93063d6a5cb083091c', 'content')->once(); })); $this->server->setTempDir(__DIR__); - $this->server->setApi(Mockery::mock('League\Glide\Api\ApiInterface', function ($mock) { + $this->server->setApi(\Mockery::mock('League\Glide\Api\ApiInterface', function ($mock) { $tmpDirPattern = Matchers::matchesPattern('~^'.__DIR__.'.*~'); $mock->shouldReceive('run')->with($tmpDirPattern, [])->andReturn('content')->once(); })); @@ -556,7 +554,7 @@ public function testMakeImageFromSourceWithCustomTmpDir() public function testMakeImageFromCache() { - $this->server->setCache(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setCache(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->andReturn(true); })); @@ -571,11 +569,11 @@ public function testMakeImageFromSourceThatDoesNotExist() $this->expectException(FileNotFoundException::class); $this->expectExceptionMessage('Could not find the image `image.jpg`.'); - $this->server->setSource(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setSource(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->andReturn(false)->once(); })); - $this->server->setCache(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setCache(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->andReturn(false)->once(); })); @@ -587,12 +585,12 @@ public function testMakeImageWithUnreadableSource() $this->expectException(FilesystemException::class); $this->expectExceptionMessage('Could not read the image `image.jpg`.'); - $this->server->setSource(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setSource(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->andReturn(true)->once(); $mock->shouldReceive('read')->andThrow('League\Flysystem\UnableToReadFile')->once(); })); - $this->server->setCache(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setCache(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->andThrow('League\Flysystem\UnableToCheckFileExistence')->once(); })); @@ -604,17 +602,17 @@ public function testMakeImageWithUnwritableCache() $this->expectException(FilesystemException::class); $this->expectExceptionMessage('Could not write the image `image.jpg/75094881e9fd2b93063d6a5cb083091c`.'); - $this->server->setSource(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setSource(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->andReturn(true)->once(); $mock->shouldReceive('read')->andReturn('content')->once(); })); - $this->server->setCache(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { + $this->server->setCache(\Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { $mock->shouldReceive('fileExists')->andThrow('League\Flysystem\UnableToCheckFileExistence')->once(); $mock->shouldReceive('write')->andThrow('League\Flysystem\UnableToWriteFile')->once(); })); - $this->server->setApi(Mockery::mock('League\Glide\Api\ApiInterface', function ($mock) { + $this->server->setApi(\Mockery::mock('League\Glide\Api\ApiInterface', function ($mock) { $mock->shouldReceive('run')->andReturn('content')->once(); })); diff --git a/tests/files/red-pixel.png b/tests/files/red-pixel.png new file mode 100644 index 00000000..abda0180 Binary files /dev/null and b/tests/files/red-pixel.png differ