Skip to content

Commit

Permalink
Add New Image Driver - VIPS
Browse files Browse the repository at this point in the history
Add New Image Driver - VIPS
  • Loading branch information
CodeWithKyrian authored Apr 2, 2024
2 parents 4dbd34e + ae30fa0 commit a0d86e0
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 16 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"guzzlehttp/guzzle": "^7.0",
"ankane/onnxruntime": "^0.2.0",
"rindow/rindow-math-matrix": "^1.2",
"spatie/fork": "^1.2",
"codewithkyrian/jinja-php": "^1.0",
"symfony/console": "^6.4|^7.0",
"imagine/imagine": "^1.3"
"imagine/imagine": "^1.3",
"rokka/imagine-vips": "^0.31.0"
},
"require-dev": {
"pestphp/pest": "^2.31",
Expand Down
3 changes: 2 additions & 1 deletion examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
.transformers-cache/*
composer.lock
composer.lock
paddleocr
Binary file added examples/images/typed-text2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/images/typed-text3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/images/typed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/Transformers.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function apply(): void
Image::$imagine = match (self::$imageDriver) {
ImageDriver::IMAGICK => new \Imagine\Imagick\Imagine(),
ImageDriver::GD => new \Imagine\GD\Imagine(),
ImageDriver::VIPS => new \Imagine\Vips\Imagine(),
};
}

Expand Down
64 changes: 51 additions & 13 deletions src/Utils/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public function __construct(public ImageInterface $image, public int $channels =
{
}

public static function read(string $input): static
public static function read(string $input, array $options = []): static
{
if (filter_var($input, FILTER_VALIDATE_URL)) {
// get from a remote url
}

$image = self::$imagine->open($input);
$image = self::$imagine->open($input, $options);

return new self($image);
}
Expand Down Expand Up @@ -69,9 +69,9 @@ public function clone(): static
* Convert the image to grayscale format.
* @return $this
*/
public function grayscale(): static
public function grayscale(bool $force = false): static
{
if ($this->channels === 1) {
if ($this->channels === 1 && !$force) {
return $this;
}

Expand All @@ -85,38 +85,64 @@ public function grayscale(): static
* Convert the image to RGB format.
* @return $this
*/
public function rgb(): static
public function rgb(bool $force = false): static
{
if ($this->channels === 3) {
// If the image is already in RGB format, return it as is
if ($this->channels === 3 && !$force) {
return $this;
}

$this->channels = 3;

// If it's a Vips image, we can extract the RGB channels
if($this->image instanceof \Imagine\Vips\Image){
$this->channels = 3;

$vipImage = $this->image->getVips()->extract_band(0, ['n' => 3]);

$this->image = $this->image->setVips($vipImage);

return $this;
}

return $this;
}

/**
* Convert the image to RGBA format.
* @return $this
*/
public function rgba(): static
public function rgba(bool $force = false): static
{
if ($this->channels === 4) {
// If the image is already in RGBA format, return it as is
if ($this->channels === 4 && !$force) {
return $this;
}

$this->channels = 4;

// If it's a Vips image, we can handle the RGBA channels
if($this->image instanceof \Imagine\Vips\Image){
$this->channels = 4;

$vipImage = $this->image->getVips();

$vipImage = $vipImage->hasAlpha() ? $vipImage->extract_band(0, ['n' => 4]) : $vipImage->bandjoin([255]);

$this->image = $this->image->setVips($vipImage);

return $this;
}

return $this;
}

/**
* Resize the image to the given dimensions.
*/
public function resize(int $width, int $height, int $resample = 2): static
public function resize(int $width, int $height, int|Resample $resample = 2): static
{
$resampleMethod = Resample::tryFrom($resample) ?? Resample::NEAREST;
$resampleMethod = $resample instanceof Resample ? $resample : Resample::from($resample) ?? Resample::NEAREST;

$this->image = $this->image->resize(new Box($width, $height), $resampleMethod->toString());

Expand All @@ -135,14 +161,21 @@ public function pad(int $left, int $right, int $top, int $bottom): static
$newWidth = $originalWidth + $left + $right;
$newHeight = $originalHeight + $top + $bottom;

$palette = new RGB();

$canvas = self::$imagine->create(new Box($newWidth, $newHeight), $palette->color('FFFFFF', 100));
$canvas = self::$imagine->create(new Box($newWidth, $newHeight));

$canvas->paste($this->image, new Point($left, $top));

$this->image = $canvas;

// Convert to the same format as the original image
if ($this->channels === 1) {
$this->grayscale(true);
} elseif ($this->channels === 3) {
$this->rgb(true);
} elseif ($this->channels === 4) {
$this->rgba(true);
}

return $this;
}

Expand Down Expand Up @@ -220,6 +253,11 @@ public function save(string $path): void
*/
public function pixelData(): array
{
// If it's a Vips image, we can extract the pixel data directly
if($this->image instanceof \Imagine\Vips\Image){
return $this->image->getVips()->writeToArray();
}

$width = $this->image->getSize()->getWidth();
$height = $this->image->getSize()->getHeight();

Expand Down
1 change: 1 addition & 0 deletions src/Utils/ImageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ enum ImageDriver
{
case IMAGICK;
case GD;
case VIPS;
}

0 comments on commit a0d86e0

Please sign in to comment.