|
5 | 5 | use Imagick; |
6 | 6 | use ImagickDraw; |
7 | 7 | use ImagickPixel; |
| 8 | +use Intervention\Image\Geometry\Rectangle; |
8 | 9 | use Intervention\Image\ImageManager; |
9 | 10 | use Intervention\Image\Interfaces\ImageInterface; |
| 11 | +use SimonHamp\TheOg\Theme\PicturePlacement; |
10 | 12 |
|
11 | | - class PictureBox extends Box |
| 13 | +class PictureBox extends Box |
12 | 14 | { |
13 | | - public string $path; |
14 | | - |
15 | 15 | /** |
16 | 16 | * @var array<callable<Imagick>> |
17 | 17 | */ |
18 | 18 | public array $maskQueue; |
19 | 19 |
|
| 20 | + public string $path; |
| 21 | + |
| 22 | + public PicturePlacement $placement = PicturePlacement::Natural; |
| 23 | + |
20 | 24 | protected ImageInterface $picture; |
21 | 25 |
|
22 | 26 | public function render(ImageInterface $image): void |
@@ -71,16 +75,33 @@ public function circle(): static |
71 | 75 | return $this; |
72 | 76 | } |
73 | 77 |
|
74 | | - public function path(string $path): self |
| 78 | + public function path(string $path): static |
75 | 79 | { |
76 | 80 | $this->path = $path; |
77 | 81 | return $this; |
78 | 82 | } |
79 | 83 |
|
| 84 | + public function placement(PicturePlacement $placement): static |
| 85 | + { |
| 86 | + $this->placement = $placement; |
| 87 | + return $this; |
| 88 | + } |
| 89 | + |
80 | 90 | protected function getPicture(): ImageInterface |
81 | 91 | { |
82 | | - return $this->picture ??= ImageManager::imagick() |
83 | | - ->read(file_get_contents($this->path)) |
84 | | - ->cover($this->box->width(), $this->box->height()); |
| 92 | + $this->picture ??= ImageManager::imagick() |
| 93 | + ->read(file_get_contents($this->path)); |
| 94 | + |
| 95 | + match ($this->placement) { |
| 96 | + PicturePlacement::Cover => $this->picture->cover($this->box->width(), $this->box->height()), |
| 97 | + PicturePlacement::Natural => $this->picture->scaleDown(min($this->box->width(), $this->box->height())), |
| 98 | + }; |
| 99 | + |
| 100 | + return $this->picture; |
| 101 | + } |
| 102 | + |
| 103 | + protected function getPrerenderedBox(): Rectangle|null |
| 104 | + { |
| 105 | + return $this->getPicture()->size(); |
85 | 106 | } |
86 | 107 | } |
0 commit comments