File tree Expand file tree Collapse file tree 1 file changed +78
-0
lines changed
Expand file tree Collapse file tree 1 file changed +78
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+ declare (strict_types=1 );
3+
4+ namespace Yireo \Webp2 \Image ;
5+
6+ use Exception as ExceptionAlias ;
7+ use Yireo \NextGenImages \Config \Config ;
8+ use Yireo \NextGenImages \Logger \Debugger ;
9+
10+ class UrlReplacer
11+ {
12+ /**
13+ * @var Convertor
14+ */
15+ private $ convertor ;
16+
17+ /**
18+ * @var File
19+ */
20+ private $ file ;
21+
22+ /**
23+ * @var Debugger
24+ */
25+ private $ debugger ;
26+
27+ /**
28+ * @var Config
29+ */
30+ private $ config ;
31+
32+ /**
33+ * ReplaceTags constructor.
34+ *
35+ * @param Convertor $convertor
36+ * @param File $file
37+ * @param Debugger $debugger
38+ * @param Config $config
39+ */
40+ public function __construct (
41+ Convertor $ convertor ,
42+ File $ file ,
43+ Debugger $ debugger ,
44+ Config $ config
45+ ) {
46+ $ this ->convertor = $ convertor ;
47+ $ this ->file = $ file ;
48+ $ this ->debugger = $ debugger ;
49+ $ this ->config = $ config ;
50+ }
51+
52+ /**
53+ * @param string $imageUrl
54+ * @return string
55+ * @throws ExceptionAlias
56+ */
57+ public function getWebpUrlFromImageUrl (string $ imageUrl ): string
58+ {
59+ $ webpUrl = $ this ->file ->toWebp ($ imageUrl );
60+
61+ try {
62+ $ result = $ this ->convertor ->convert ($ imageUrl , $ webpUrl );
63+ } catch (ExceptionAlias $ e ) {
64+ if ($ this ->config ->isDebugging ()) {
65+ throw $ e ;
66+ }
67+
68+ $ result = false ;
69+ $ this ->debugger ->debug ($ e ->getMessage (), [$ imageUrl , $ webpUrl ]);
70+ }
71+
72+ if (!$ result && !$ this ->convertor ->urlExists ($ webpUrl )) {
73+ return '' ;
74+ }
75+
76+ return $ webpUrl ;
77+ }
78+ }
You can’t perform that action at this time.
0 commit comments