33namespace Yuges \Image \Drivers \Imagick ;
44
55use Imagick ;
6- use Yuges \Image \Data \Flip ;
76use Yuges \Image \Data \Area ;
87use Yuges \Image \Data \Size ;
8+ use Yuges \Image \Data \Flip ;
99use Yuges \Image \Enums \Orientation ;
1010use Yuges \Image \Drivers \ImageDriver ;
1111use Yuges \Image \Enums \AlignPosition ;
1212use Yuges \Image \Enums \FlipDirection ;
13+ use Yuges \Image \Enums \ResizeConstraint ;
1314
1415class 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