Skip to content

Commit

Permalink
Intervention Image v3
Browse files Browse the repository at this point in the history
  • Loading branch information
sdebacker committed May 28, 2024
1 parent c162d2b commit 0adffb1
Showing 1 changed file with 25 additions and 48 deletions.
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 0adffb1

Please sign in to comment.