2929class Image
3030{
3131 public function __construct (
32- protected string $ src ,
32+ protected string | Responsive $ src ,
3333 protected string $ alt ,
3434 protected ?int $ width = null ,
3535 protected ?int $ height = null ,
@@ -39,34 +39,46 @@ public function __construct(
3939 ) {
4040 }
4141
42- public static function attributes ( string | Responsive $ src , $ alt , $ width = null , $ height = null , $ format = ' webp ' , $ loading = ' lazy ' , $ decoding = ' async ' ): string
42+ public static function fromObject ( object $ image , int $ width, int $ height, ? string $ alt = null ): self
4343 {
44- if ($ src instanceof Responsive) {
45- $ targetSrc = $ src ->src ;
46- } else {
47- $ targetSrc = $ src ;
44+ if (!property_exists ($ image , 'source ' ) || empty ($ image ->source )) {
45+ throw new \InvalidArgumentException ('Image object must have a non-empty "source" property. ' );
4846 }
4947
50- $ image = new Image ($ targetSrc , $ alt , $ width , $ height , $ format , $ loading , $ decoding );
48+ return new self (
49+ $ image ->source ,
50+ (property_exists ($ image , 'caption ' ) && !empty ($ image ->caption )) ? $ image ->caption : $ alt ,
51+ $ width ,
52+ $ height
53+ );
54+ }
55+
56+ public static function attributes (string |Responsive $ src , $ alt , $ width = null , $ height = null , $ format = 'webp ' , $ loading = 'lazy ' , $ decoding = 'async ' ): string
57+ {
5158
59+ return (new self ($ src , $ alt , $ width , $ height , $ format , $ loading , $ decoding ))->toAttributes ();
60+ }
61+
62+ public function toAttributes (): string
63+ {
5264 $ attributes = [
53- sprintf ('src="%s" ' , $ image ->getSrc ()),
54- sprintf ('alt="%s" ' , $ image ->getAlt ()),
55- sprintf ('loading="%s" ' , $ image ->getLoading ()),
56- sprintf ('decoding="%s" ' , $ image ->getDecoding ()),
65+ sprintf ('src="%s" ' , $ this ->getSrc ()),
66+ sprintf ('alt="%s" ' , $ this ->getAlt ()),
67+ sprintf ('loading="%s" ' , $ this ->getLoading ()),
68+ sprintf ('decoding="%s" ' , $ this ->getDecoding ()),
5769 ];
5870
59- if ($ image ->getwidth ()) {
60- $ attributes [] = sprintf ('width="%s" ' , $ image ->getwidth ());
71+ if ($ this ->getwidth ()) {
72+ $ attributes [] = sprintf ('width="%s" ' , $ this ->getwidth ());
6173 }
6274
63- if ($ image ->getHeight ()) {
64- $ attributes [] = sprintf ('height="%s" ' , $ image ->getHeight ());
75+ if ($ this ->getHeight ()) {
76+ $ attributes [] = sprintf ('height="%s" ' , $ this ->getHeight ());
6577 }
6678
67- if ($ src instanceof Responsive) {
68- $ attributes [] = sprintf ('srcset="%s" ' , $ src ->getSrcset ($ image ));
69- $ attributes [] = sprintf ('sizes="%s" ' , $ src ->getSizes ($ image ));
79+ if ($ this -> src instanceof Responsive) {
80+ $ attributes [] = sprintf ('srcset="%s" ' , $ this -> src ->getSrcset ($ this ));
81+ $ attributes [] = sprintf ('sizes="%s" ' , $ this -> src ->getSizes ($ this ));
7082 }
7183
7284 return implode (" " , $ attributes );
@@ -79,6 +91,18 @@ public static function tag(string|Responsive $src, $alt, $width = null, $height
7991 foreach ($ options as $ key => $ value ) {
8092 $ attributes .= sprintf (' %s="%s" ' , $ key , $ value );
8193 }
94+
95+ return sprintf ('<img %s /> ' , $ attributes );
96+ }
97+
98+ public function toTag (array $ options = []): string
99+ {
100+ $ attributes = $ this ->toAttributes ();
101+
102+ foreach ($ options as $ key => $ value ) {
103+ $ attributes .= sprintf (' %s="%s" ' , $ key , $ value );
104+ }
105+
82106 return sprintf ('<img %s /> ' , $ attributes );
83107 }
84108
@@ -150,16 +174,17 @@ public function setHeight($height): self
150174
151175 public function getSrc (): string
152176 {
177+ $ src = $ this ->src instanceof Responsive ? $ this ->src ->src : $ this ->src ;
153178 // If the URL starts with 'http://' or 'https://' and is not from 'storage.flyo.cloud', return it directly
154- if (preg_match ('/ ^https?:\/\// ' , $ this -> src ) && !str_contains ($ this ->src , 'storage.flyo.cloud ' )) {
155- return $ this -> src ;
179+ if (preg_match ('# ^https?:\/\/# ' , $ src ) && !str_contains ($ this ->src , 'storage.flyo.cloud ' )) {
180+ return $ src ;
156181 }
157182
158- if (str_starts_with ($ this -> src , '/ ' )) {
159- return $ this -> src ;
183+ if (str_starts_with ($ src , '/ ' )) {
184+ return $ src ;
160185 }
161186
162- $ url = str_contains ($ this -> src , 'https://storage.flyo.cloud ' ) ? $ this -> src : 'https://storage.flyo.cloud/ ' . $ this -> src ;
187+ $ url = str_contains ($ src , 'https://storage.flyo.cloud ' ) ? $ src : 'https://storage.flyo.cloud/ ' . $ src ;
163188
164189 // If either width or height are defined, we add the /thumb/$widthx$height path to it.
165190 $ width = $ this ->getWidth ();
@@ -173,7 +198,7 @@ public function getSrc(): string
173198
174199 // if the original file name is already in the requested format, we don't add the format to the url.
175200 $ orginalFormat = pathinfo ($ url , PATHINFO_EXTENSION ) ?: '' ;
176- if ($ orginalFormat == $ this ->getFormat ()) {
201+ if ($ orginalFormat === $ this ->getFormat ()) {
177202 return $ url ;
178203 }
179204
0 commit comments