-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathResizeCanvasModifier.php
More file actions
62 lines (55 loc) · 1.88 KB
/
ResizeCanvasModifier.php
File metadata and controls
62 lines (55 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types=1);
namespace Intervention\Image\Modifiers;
use Intervention\Image\Alignment;
use Intervention\Image\Drivers\SpecializableModifier;
use Intervention\Image\Exceptions\InvalidArgumentException;
use Intervention\Image\Exceptions\StateException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SizeInterface;
use Intervention\Image\Size;
class ResizeCanvasModifier extends SpecializableModifier
{
/**
* Create new modifier object.
*/
public function __construct(
public ?int $width = null,
public ?int $height = null,
public null|string|ColorInterface $background = null,
public string|Alignment $alignment = Alignment::CENTER,
) {
//
}
/**
* Build the crop size to be used for the ResizeCanvas process.
*
* @throws InvalidArgumentException
*/
protected function cropSize(ImageInterface $image, bool $relative = false): SizeInterface
{
$size = match ($relative) {
true => new Size(
is_null($this->width) ? $image->width() : $image->width() + $this->width,
is_null($this->height) ? $image->height() : $image->height() + $this->height,
),
default => new Size(
is_null($this->width) ? $image->width() : $this->width,
is_null($this->height) ? $image->height() : $this->height,
),
};
return $size->alignPivotTo($image->size(), $this->alignment);
}
/**
* Return color to fill the newly created areas after rotation.
*
* @throws StateException
*/
protected function backgroundColor(): ColorInterface
{
return $this->driver()->decodeColor(
$this->background ?? $this->driver()->config()->backgroundColor,
);
}
}