Skip to content

Commit 0ab3e7c

Browse files
committed
Re-add file
1 parent dbdd7ff commit 0ab3e7c

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

Image/UrlReplacer.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

0 commit comments

Comments
 (0)