Skip to content

Commit

Permalink
Merge remote-tracking branch 'konnng/upgrade-to-glide-3' into upgrade…
Browse files Browse the repository at this point in the history
…-to-glide-3
  • Loading branch information
Art4 committed Jan 25, 2024
2 parents eb917be + 6a96ab7 commit a819414
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 111 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ docs/_site
.php-cs-fixer.cache
.phpunit.result.cache
composer.lock
.history
33 changes: 1 addition & 32 deletions src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,37 +102,6 @@ public function run($source, array $params)
$image = $manipulator->run($image);
}

$encode = new Encode();
$encode->setParams($params);

switch ($encode->fm) {
case 'avif':
$encodedImage = $image->toAvif($encode->getQuality());
break;

case 'gif':
$encodedImage = $image->toGif($encode->getQuality());
break;

case 'png':
$encodedImage = $image->toPng($encode->getQuality());
break;

case 'webp':
$encodedImage = $image->toWebp($encode->getQuality());
break;

case 'tiff':
$encodedImage = $image->toTiff($encode->getQuality());
break;

case 'jpg':
case 'pjpg':
default:
$encodedImage = $image->toJpeg($encode->getQuality());
break;
}

return $encodedImage->toString();
return $image->encodeByMediaType()->toString();
}
}
14 changes: 10 additions & 4 deletions src/Manipulators/Background.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace League\Glide\Manipulators;

use Intervention\Image\ImageManager;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Origin;
use League\Glide\Manipulators\Helpers\Color;

/**
Expand All @@ -26,10 +28,14 @@ public function run(ImageInterface $image): ImageInterface
$color = (new Color($this->bg))->formatted();

if ($color) {
$new = $image->driver()->createImage($image->width(), $image->height())->fill($color);
// TODO: Find a way to communicate the mime
// $new->mime = $image->mime;
$image = $new->place($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;
Expand Down
38 changes: 23 additions & 15 deletions src/Manipulators/Encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace League\Glide\Manipulators;

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;

/**
Expand All @@ -19,26 +22,33 @@ class Encode extends BaseManipulator
*/
public function run(ImageInterface $image): ImageInterface
{
// TODO: Encoding was moved to Api::run()
return $image;

$format = $this->getFormat($image);
$quality = $this->getQuality();
$driver = $image->driver();

if (in_array($format, ['jpg', 'pjpg'], true)) {
$image = $image->driver()
->createImage($image->width(), $image->height())
->fill('#fff')
->place($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) {
// TODO: interlace() was removed
// $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()
);
}

/**
Expand All @@ -54,9 +64,7 @@ public function getFormat(ImageInterface $image)
return $this->fm;
}

// TODO: Image::mime() was removed
// return array_search($image->mime(), static::supportedFormats(), true) ?: 'jpg';
return 'jpg';
return array_search($image->origin()->mediaType(), static::supportedFormats(), true) ?: 'jpg';
}

/**
Expand Down
30 changes: 27 additions & 3 deletions src/Manipulators/Orientation.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,34 @@ class Orientation extends BaseManipulator
public function run(ImageInterface $image): ImageInterface
{
$orientation = $this->getOrientation();
$originalOrientation = $image->exif('Orientation');

if ('auto' === $orientation) {
// TODO: orientate() has been removed
// 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);
Expand Down
14 changes: 11 additions & 3 deletions tests/Manipulators/BackgroundTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Origin;
use PHPUnit\Framework\TestCase;

class BackgroundTest extends TestCase
Expand All @@ -23,11 +24,18 @@ public function testCreateInstance()
public function testRun()
{
$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('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('origin')->andReturn($originMock)->once();

$mock->shouldReceive('driver')->andReturn(\Mockery::mock(DriverInterface::class, function ($mock) use ($originMock) {
$mock->shouldReceive('createImage')->with(100, 100)->andReturn(\Mockery::mock(ImageInterface::class, function ($mock) use ($originMock) {
$mock->shouldReceive('fill')->with('rgba(0, 0, 0, 1)')->andReturn(\Mockery::mock(ImageInterface::class, function ($mock) use ($originMock) {
$mock->shouldReceive('setOrigin')->withArgs(function ($arg1) {
return $arg1 instanceof Origin;
})->andReturn($mock)->once();
$mock->shouldReceive('place')->andReturn($mock)->once();
}))->once();
}))->once();
Expand Down
Loading

0 comments on commit a819414

Please sign in to comment.