Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
sdebacker committed May 28, 2024
2 parents 69896df + 0adffb1 commit 98f01d0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 49 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"illuminate/console": "^9.0|^10.0|^11.0",
"illuminate/support": "^9.0|^10.0|^11.0",
"illuminate/routing": "^9.0|^10.0|^11.0",
"intervention/image": "^2.7",
"intervention/image": "^3.6",
"league/flysystem": "^3.0",
"symfony/http-foundation": "^6.0|^7.0",
"symfony/http-kernel": "^6.0|^7.0",
Expand Down
73 changes: 25 additions & 48 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Bkwld\Croppa;

use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\Interfaces\ImageInterface;

/**
* Wraps Intervention Image with the API used by Croppa to transform the src image.
Expand All @@ -12,7 +14,7 @@ class Image
/**
* @var \Intervention\Image\Image
*/
private $image;
private ImageInterface $image;

/**
* @var int
Expand All @@ -22,24 +24,24 @@ class Image
/**
* @var bool
*/
private $interlace;
private bool $interlace;

/**
* @var bool
*/
private $upsize;
private bool $upsize;

/**
* Image format (jpg, gif, png, webp).
*
* @var string
*/
private $format;
private string $format;

public function __construct(string $path, array $options = [])
{
$manager = new ImageManager(['driver' => 'gd']);
$this->image = $manager->make($path);
$manager = new ImageManager(new Driver());
$this->image = $manager->read($path);
$this->interlace = $options['interlace'];
$this->upsize = $options['upsize'];
if (isset($options['quality']) && is_array($options['quality'])) {
Expand All @@ -55,33 +57,9 @@ public function __construct(string $path, array $options = [])
*/
public function process(?int $width, ?int $height, array $options = []): self
{
$this->autoRotate()
->trim($options)
$this->trim($options)
->resizeAndOrCrop($width, $height, $options)
->applyFilters($options);
if ($this->interlace) {
$this->interlace();
}

return $this;
}

/**
* Turn on interlacing to make progessive JPEG files.
*/
public function interlace(): self
{
$this->image->interlace();

return $this;
}

/**
* Auto rotate the image based on exif data.
*/
public function autoRotate(): self
{
$this->image->orientate();

return $this;
}
Expand Down Expand Up @@ -183,11 +161,11 @@ public function cropQuadrant(?int $width, ?int $height, array $options): self
'R' => 'right',
'B' => 'bottom',
];
$this->image->fit($width, $height, function ($constraint) {
if (!$this->upsize) {
$constraint->upsize();
}
}, $positions[$quadrant]);
if (!$this->upsize) {
$this->image->coverDown($width, $height, $positions[$quadrant]);
} else {
$this->image->cover($width, $height, $positions[$quadrant]);
}

return $this;
}
Expand All @@ -197,12 +175,11 @@ public function cropQuadrant(?int $width, ?int $height, array $options): self
*/
public function resize(?int $width, ?int $height): self
{
$this->image->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
if (!$this->upsize) {
$constraint->upsize();
}
});
if (!$this->upsize) {
$this->image->scaleDown($width, $height);
} else {
$this->image->scale($width, $height);
}

return $this;
}
Expand All @@ -212,11 +189,11 @@ public function resize(?int $width, ?int $height): self
*/
public function crop(?int $width, ?int $height): self
{
$this->image->fit($width, $height, function ($constraint) {
if (!$this->upsize) {
$constraint->upsize();
}
});
if (!$this->upsize) {
$this->image->coverDown($width, $height);
} else {
$this->image->cover($width, $height);
}

return $this;
}
Expand Down Expand Up @@ -280,6 +257,6 @@ private function getFormatFromPath(string $path): string
*/
public function get(): string
{
return $this->image->encode($this->format, $this->quality);
return $this->image->encodeByExtension($this->format, progressive: $this->interlace, quality: $this->quality);
}
}

0 comments on commit 98f01d0

Please sign in to comment.