Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement missing functions #14

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,41 @@ $manager = new ImageManager(['driver' => 'vips']);
- [x] BackupCommand
- [x] BlurCommand
- [x] BrightnessCommand
- [x] ColorizeCommand
- [x] ContrastCommand
- [x] CropCommand
- [x] DestroyCommand
- [x] FillCommand
- [x] FitCommand
- [x] FlipCommand
- [x] GammaCommand
- [x] GetSizeCommand
- [x] GreyscaleCommand
- [x] HeightenCommand
- [x] InsertCommand
- [x] InterlaceCommand
- [x] InvertCommand
- [x] LimitColorsCommand
- [x] MaskCommand
- [x] OpacityCommand
- [x] PickColorCommand
- [x] PixelCommand
- [x] PixelateCommand
- [x] ResetCommand
- [x] ResizeCommand
- [x] ResizeCanvasCommand
- [x] RotateCommand
- [x] SharpenCommand
- [x] TrimCommand
- [x] WidenCommand
- [ ] ColorizeCommand
- [ ] ContrastCommand
- [ ] FillCommand
- [ ] InterlaceCommand
- [ ] LimitColorsCommand
- [ ] MaskCommand
- [ ] PixelateCommand
- [ ] ResizeCanvasCommand
- [ ] TrimCommand

### Shapes

- [ ] CircleShape
- [ ] EllipseShape
- [ ] LineShape
- [ ] PolygonShape
- [ ] RectangleShape
- [x] CircleShape
- [x] EllipseShape
- [x] LineShape
- [x] PolygonShape
- [x] RectangleShape

## Credits

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"require": {
"php": "^7.1",
"intervention/image": "^2.4",
"jcupitt/vips": "^1.0"
"jcupitt/vips": "^1.0",
"phenx/php-font-lib": "^0.5.2"
},
"autoload": {
"psr-4": {
Expand Down
9 changes: 5 additions & 4 deletions src/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

namespace Intervention\Image\Vips;

use ImagickPixel;
use ImagickPixelException;
use Intervention\Image\AbstractColor;
use Intervention\Image\Exception\NotSupportedException;

class Color extends AbstractColor
{
Expand Down Expand Up @@ -88,13 +89,13 @@ public function initFromString($value)
/**
* Initiate the color from the ImagickPixel object.
*
* @param \ImagickPixel $value
* @param ImagickPixel $value
* @return void
* @throws \Intervention\Image\Exception\NotSupportedException
* @throws ImagickPixelException
*/
public function initFromObject($value)
{
throw new NotSupportedException('VIPS color cannot be initiated from the ImagickPixel object.');
$this->initFromArray($value->getColor());
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Intervention\Image\Vips\Commands;

use Closure;
use Jcupitt\Vips\Image;
use Jcupitt\Vips\Exception;
use Intervention\Image\Commands\AbstractCommand as BaseAbstractCommand;
use Jcupitt\Vips\Exception;
use Jcupitt\Vips\Image;

abstract class AbstractCommand extends BaseAbstractCommand
{
Expand Down Expand Up @@ -61,7 +61,7 @@ protected function extractAlphaChannel(Image $image): Image
/**
* Handle the command.
*
* @param \Closure $command
* @param Closure $command
* @return bool
*/
protected function handleCommand(Closure $command): bool
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/BlurCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Intervention\Image\Vips\Commands;

use Jcupitt\Vips\Image;

class BlurCommand extends AbstractCommand
{
/**
Expand All @@ -19,6 +21,7 @@ public function execute($image): bool
->value(1);

return $this->handleCommand(function () use ($image, $amount) {
/** @var Image $core */
$core = $image->getCore();

$core = $core->gaussblur($amount * 0.53);
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/BrightnessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Intervention\Image\Vips\Commands;

use Jcupitt\Vips\Image;

class BrightnessCommand extends AbstractCommand
{
/**
Expand All @@ -20,6 +22,7 @@ public function execute($image): bool
->value() * 2.55;

return $this->handleCommand(function () use ($image, $level) {
/** @var Image $core */
$core = $image->getCore();

if ($core->hasAlpha()) {
Expand Down
54 changes: 50 additions & 4 deletions src/Commands/ColorizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,65 @@

namespace Intervention\Image\Vips\Commands;

use Intervention\Image\Exception\NotSupportedException;
use Jcupitt\Vips\Image;

class ColorizeCommand extends AbstractCommand
{
/**
* Execute the command.
*
* @param \Intervention\Image\Image $image
* @return void
* @throws \Intervention\Image\Exception\NotSupportedException
* @return bool
*/
public function execute($image)
{
throw new NotSupportedException('Colorize command is not supported by VIPS driver.');
$red = $this->argument(0)
->between(-100, 100)
->required()
->value();
$green = $this->argument(1)
->between(-100, 100)
->required()
->value();
$blue = $this->argument(2)
->between(-100, 100)
->required()
->value();

return $this->handleCommand(
function () use ($image, $red, $green, $blue) {
/** @var Image $core */
$core = $image->getCore();

// calculate a and b for colors linear transformation
$a = [1, 1, 1];
$b = [0, 0, 0];
[$a[0], $b[0]] = $this->normalizeLevel($red);
[$a[1], $b[1]] = $this->normalizeLevel($green);
[$a[2], $b[2]] = $this->normalizeLevel($blue);

if ($core->hasAlpha()) {
$flatten = $this->flattenImage($core);

$mask = $this->extractAlphaChannel($core);

$core = $flatten->linear($a, $b)
->bandjoin($mask);
} else {
$core = $core->linear($a, $b);
}

$image->setCore($core);
}
);
}

private function normalizeLevel($level)
{
if ($level > 0) {
return [1 - $level / 100, $level * 2.55];
}

return [1 + $level / 100, 0];
}
}
31 changes: 27 additions & 4 deletions src/Commands/ContrastCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,42 @@

namespace Intervention\Image\Vips\Commands;

use Intervention\Image\Exception\NotSupportedException;
use Jcupitt\Vips\Image;

class ContrastCommand extends AbstractCommand
{
/**
* Execute the command.
*
* @param \Intervention\Image\Image $image
* @return void
* @throws \Intervention\Image\Exception\NotSupportedException
* @return bool
*/
public function execute($image)
{
throw new NotSupportedException('Contrast command is not supported by VIPS driver.');
$level = $this->argument(0)->between(-100, 100)->required()->value();

return $this->handleCommand(
function () use ($image, $level) {
/** @var Image $core */
$core = $image->getCore();

// calculate a and b for linear
$a = 1 + $level / 100;
$b = 255 * (1 - $a);

if ($core->hasAlpha()) {
$flatten = $this->flattenImage($core);

$mask = $this->extractAlphaChannel($core);

$core = $flatten->linear([$a, $a, $a], [$b, $b, $b])
->bandjoin($mask);
} else {
$core = $core->linear([$a, $a, $a], [$b, $b, $b]);
}

$image->setCore($core);
}
);
}
}
25 changes: 9 additions & 16 deletions src/Commands/CropCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace Intervention\Image\Vips\Commands;

use Jcupitt\Vips\Image;
use Intervention\Image\Size;
use Intervention\Image\Point;
use Intervention\Image\Exception\InvalidArgumentException;
use Jcupitt\Vips\Image;
use Jcupitt\Vips\Interesting;

class CropCommand extends AbstractCommand
{
Expand Down Expand Up @@ -42,21 +41,15 @@ public function execute($image): bool
->type('digit')
->value();

$size = new Size($width, $height);

$position = new Point($x, $y);

return $this->handleCommand(function () use ($image, $width, $height, $x, $y, $size, $position) {
if (is_null($x) && is_null($y)) {
$position = $image
->getSize()
->align('center')
->relativePosition($size->align('center'));
}

return $this->handleCommand(function () use ($image, $width, $height, $x, $y) {
/** @var Image $core */
$core = $image->getCore();

$core = $core->crop($position->x, $position->y, $size->width, $size->height);
if (is_null($x) || is_null($y)) {
$core = $core->smartcrop($width, $height, ['interesting' => Interesting::CENTRE]);
} else {
$core = $core->crop($x, $y, $width, $height);
}

$image->setCore($core);
});
Expand Down
Loading