Skip to content

Commit a97ed39

Browse files
committed
GdDriver and improvements
1 parent 629876d commit a97ed39

12 files changed

Lines changed: 371 additions & 63 deletions

src/Data/Flip.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ public function __construct(
1515
public function getEnum(): ?FlipDirection
1616
{
1717
if ($this->vertical && $this->horizontal) {
18-
return FlipDirection::BOTH;
18+
return FlipDirection::Both;
1919
} elseif ($this->vertical) {
20-
return FlipDirection::VERTICAL;
20+
return FlipDirection::Vertical;
2121
} elseif ($this->horizontal) {
22-
return FlipDirection::HORIZONTAL;
22+
return FlipDirection::Horizontal;
2323
}
2424

2525
return null;

src/Drivers/Gd/GdDriver.php

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
3+
namespace Yuges\Image\Drivers\Gd;
4+
5+
use GdImage;
6+
use Yuges\Image\Data\Area;
7+
use Yuges\Image\Data\Flip;
8+
use Yuges\Image\Data\Size;
9+
use Yuges\Image\Enums\Orientation;
10+
use Yuges\Image\Enums\FlipDirection;
11+
use Yuges\Image\Drivers\ImageDriver;
12+
use Yuges\Image\Enums\ResizeConstraint;
13+
14+
class GdDriver implements ImageDriver
15+
{
16+
const string NAME = 'gd';
17+
18+
protected GdImage $image;
19+
20+
protected array $exif = [];
21+
22+
protected int $quality = -1;
23+
24+
public static function create(int $width, int $height, ?string $backgroundColor = null): static
25+
{
26+
return new static;
27+
}
28+
29+
public function loadFile(string $path): static
30+
{
31+
return $this;
32+
}
33+
34+
public function getName(): string
35+
{
36+
return self::NAME;
37+
}
38+
39+
public function getImage(): GdImage
40+
{
41+
return $this->image;
42+
}
43+
44+
public function setImage(GdImage $image): static
45+
{
46+
$this->image = $image;
47+
48+
return $this;
49+
}
50+
51+
public function getArea(): Area
52+
{
53+
return new Area($this->getSize());
54+
}
55+
56+
public function getSize(): Size
57+
{
58+
return new Size($this->getWidth(), $this->getHeight());
59+
}
60+
61+
public function getWidth(): int
62+
{
63+
return imagesx($this->image);
64+
}
65+
66+
public function getHeight(): int
67+
{
68+
return imagesy($this->image);
69+
}
70+
71+
72+
73+
74+
public function resize(int $width, int $height, ?ResizeConstraint $constraints = null): static
75+
{
76+
return $this;
77+
}
78+
79+
public function width(int $width, ?ResizeConstraint $constraints = ResizeConstraint::PreserveAspectRatio): static
80+
{
81+
return $this;
82+
}
83+
84+
public function height(int $height, ?ResizeConstraint $constraints = ResizeConstraint::PreserveAspectRatio): static
85+
{
86+
return $this;
87+
}
88+
89+
public function flip(Flip|FlipDirection|null $flip = null): static
90+
{
91+
return $this;
92+
}
93+
94+
public function crop(int $width, int $height, ?int $x = null, ?int $y = null): static
95+
{
96+
return $this;
97+
}
98+
99+
public function rotate(?float $degrees = null, ?string $background = null): static
100+
{
101+
return $this;
102+
}
103+
104+
public function orientate(?Orientation $orientation = null): static
105+
{
106+
return $this;
107+
}
108+
109+
public function blur(int $blur): static
110+
{
111+
return $this;
112+
}
113+
114+
115+
116+
117+
public function base64(string $format = 'png', bool $prefix = true): string
118+
{
119+
ob_start();
120+
121+
switch (strtolower($format)) {
122+
case 'jpg':
123+
case 'jpeg':
124+
case 'jfif':
125+
\imagejpeg($this->image, null, $this->quality);
126+
break;
127+
case 'png':
128+
\imagepng($this->image, null, $this->quality);
129+
break;
130+
case 'gif':
131+
\imagegif($this->image, null);
132+
break;
133+
case 'webp':
134+
\imagewebp($this->image, null);
135+
break;
136+
case 'avif':
137+
\imageavif($this->image, null);
138+
break;
139+
default:
140+
// throw UnsupportedImageFormat::make($format);
141+
}
142+
143+
$imageData = ob_get_contents();
144+
ob_end_clean();
145+
146+
if ($prefix) {
147+
return 'data:image/'.$format.';base64,'.base64_encode($imageData);
148+
}
149+
150+
return base64_encode($imageData);
151+
}
152+
153+
public function save(?string $path = null): static
154+
{
155+
return $this;
156+
}
157+
}

src/Drivers/ImageDriver.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace Yuges\Image\Drivers;
44

5+
use Yuges\Image\Data\Area;
6+
use Yuges\Image\Data\Size;
57
use Yuges\Image\Data\Flip;
68
use Yuges\Image\Enums\Orientation;
79
use Yuges\Image\Enums\FlipDirection;
10+
use Yuges\Image\Enums\ResizeConstraint;
811

912
interface ImageDriver
1013
{
@@ -16,13 +19,23 @@ public function getName(): string;
1619

1720
public function getImage(): mixed;
1821

22+
public function getArea(): Area;
23+
24+
public function getSize(): Size;
25+
1926
public function getWidth(): int;
2027

2128
public function getHeight(): int;
2229

23-
public function isAnimated(): bool;
2430

2531

32+
33+
public function resize(int $width, int $height, ?ResizeConstraint $constraints = null): static;
34+
35+
public function width(int $width, ?ResizeConstraint $constraints = ResizeConstraint::PreserveAspectRatio): static;
36+
37+
public function height(int $height, ?ResizeConstraint $constraints = ResizeConstraint::PreserveAspectRatio): static;
38+
2639
public function flip(Flip|FlipDirection|null $flip = null): static;
2740

2841
public function crop(int $width, int $height, ?int $x = null, ?int $y = null): static;
@@ -31,6 +44,12 @@ public function rotate(?float $degrees = null, ?string $background = null): stat
3144

3245
public function orientate(?Orientation $orientation = null): static;
3346

47+
public function blur(int $blur): static;
48+
49+
50+
51+
52+
public function base64(string $format = 'png', bool $prefix = true): string;
3453

3554
public function save(?string $path = null): static;
3655
}

src/Drivers/Imagick/ImagickColor.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ class ImagickColor implements Color
99
{
1010
public ImagickPixel $pixel;
1111

12+
public static function create(string $color): static
13+
{
14+
$instance = new static();
15+
16+
$instance->pixel = new ImagickPixel('none');
17+
18+
return $instance;
19+
}
20+
1221
public static function createFromRgba(
1322
int|float $red,
1423
int|float $green,

src/Drivers/Imagick/ImagickDriver.php

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
namespace Yuges\Image\Drivers\Imagick;
44

55
use Imagick;
6-
use Yuges\Image\Data\Flip;
76
use Yuges\Image\Data\Area;
87
use Yuges\Image\Data\Size;
8+
use Yuges\Image\Data\Flip;
99
use Yuges\Image\Enums\Orientation;
1010
use Yuges\Image\Drivers\ImageDriver;
1111
use Yuges\Image\Enums\AlignPosition;
1212
use Yuges\Image\Enums\FlipDirection;
13+
use Yuges\Image\Enums\ResizeConstraint;
1314

1415
class ImagickDriver implements ImageDriver
1516
{
@@ -49,14 +50,9 @@ public function getName(): string
4950
return self::NAME;
5051
}
5152

52-
public function getArea(): Area
53-
{
54-
return new Area($this->getSize());
55-
}
56-
57-
public function getSize(): Size
53+
public function getImage(): Imagick
5854
{
59-
return new Size($this->getWidth(), $this->getHeight());
55+
return $this->image;
6056
}
6157

6258
public function setImage(Imagick $image): static
@@ -66,9 +62,14 @@ public function setImage(Imagick $image): static
6662
return $this;
6763
}
6864

69-
public function getImage(): Imagick
65+
public function getArea(): Area
7066
{
71-
return $this->image;
67+
return new Area($this->getSize());
68+
}
69+
70+
public function getSize(): Size
71+
{
72+
return new Size($this->getWidth(), $this->getHeight());
7273
}
7374

7475
public function getWidth(): int
@@ -88,6 +89,35 @@ public function isAnimated(): bool
8889

8990

9091

92+
public function resize(int $width, int $height, ?ResizeConstraint $constraints = null): static
93+
{
94+
// $resized = $this->getSize()->resize($width, $height, $constraints);
95+
96+
foreach ($this->image as $image) {
97+
$image->scaleImage($width, $height);
98+
}
99+
100+
return $this;
101+
}
102+
103+
public function width(int $width, ?ResizeConstraint $constraints = ResizeConstraint::PreserveAspectRatio): static
104+
{
105+
$height = (int) round($width / $this->getSize()->aspectRatio());
106+
107+
$this->resize($width, $height, $constraints);
108+
109+
return $this;
110+
}
111+
112+
public function height(int $height, ?ResizeConstraint $constraints = ResizeConstraint::PreserveAspectRatio): static
113+
{
114+
$width = (int) round($height * $this->getSize()->aspectRatio());
115+
116+
$this->resize($width, $height, $constraints);
117+
118+
return $this;
119+
}
120+
91121
public function flip(Flip|FlipDirection|null $flip = null): static
92122
{
93123
if ($flip instanceof Flip) {
@@ -100,14 +130,14 @@ public function flip(Flip|FlipDirection|null $flip = null): static
100130

101131
foreach ($this->image as $image) {
102132
switch ($flip) {
103-
case FlipDirection::VERTICAL:
133+
case FlipDirection::Both:
104134
$image->flipImage();
105-
break;
106-
case FlipDirection::HORIZONTAL:
107135
$image->flopImage();
108136
break;
109-
case FlipDirection::BOTH:
137+
case FlipDirection::Vertical:
110138
$image->flipImage();
139+
break;
140+
case FlipDirection::Horizontal:
111141
$image->flopImage();
112142
break;
113143
}
@@ -139,11 +169,12 @@ public function crop(int $width, int $height, ?int $x = null, ?int $y = null): s
139169
public function rotate(?float $degrees = null, ?string $background = null): static
140170
{
141171
if (! $background) {
142-
$background = ImagickColor::createFromString('none');
172+
$background = ImagickColor::create('none');
143173
}
144174

145175
foreach ($this->image as $image) {
146176
$image->rotateImage($background->getPixel(), $degrees);
177+
$image->setImagePage(0, 0, 0, 0);
147178
}
148179

149180
return $this;
@@ -158,8 +189,32 @@ public function orientate(?Orientation $orientation = null): static
158189
return $this->rotate($orientation->degrees());
159190
}
160191

192+
public function blur(int $blur): static
193+
{
194+
foreach ($this->image as $image) {
195+
$image->blurImage(0.5 * $blur, 0.1 * $blur);
196+
}
197+
198+
return $this;
199+
}
200+
201+
202+
161203

162204

205+
206+
public function base64(string $format = 'png', bool $prefix = true): string
207+
{
208+
$image = clone $this->image;
209+
$image->setFormat($format);
210+
211+
if ($prefix) {
212+
return 'data:image/'.$format.';base64,'.base64_encode($image->getImageBlob());
213+
}
214+
215+
return base64_encode($image->getImageBlob());
216+
}
217+
163218
public function save(?string $path = null): static
164219
{
165220
if ($this->isAnimated()) {

0 commit comments

Comments
 (0)