1 | <?php |
||||
2 | /** |
||||
3 | * This file is part of the O2System Framework package. |
||||
4 | * |
||||
5 | * For the full copyright and license information, please view the LICENSE |
||||
6 | * file that was distributed with this source code. |
||||
7 | * |
||||
8 | * @author Steeve Andrian Salim |
||||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
10 | */ |
||||
11 | |||||
12 | // ------------------------------------------------------------------------ |
||||
13 | |||||
14 | namespace O2System\Image\Optimizers; |
||||
15 | |||||
16 | // ------------------------------------------------------------------------ |
||||
17 | |||||
18 | use O2System\Spl\Exceptions\RuntimeException; |
||||
19 | |||||
20 | /** |
||||
21 | * Class Optimus |
||||
22 | * @package O2System\Image\Optimizers |
||||
23 | */ |
||||
24 | class Optimus |
||||
25 | { |
||||
26 | /** |
||||
27 | * Optimus::$apiUrl |
||||
28 | * |
||||
29 | * @var string |
||||
30 | */ |
||||
31 | private $apiUrl = 'https://api.optimus.io'; |
||||
32 | |||||
33 | /** |
||||
34 | * Optimus::$apiKey |
||||
35 | * |
||||
36 | * @var string |
||||
37 | */ |
||||
38 | private $apiKey; |
||||
39 | |||||
40 | // ------------------------------------------------------------------------ |
||||
41 | |||||
42 | /** |
||||
43 | * Optimus::__construct |
||||
44 | * |
||||
45 | * @param string|null $apiKey |
||||
46 | */ |
||||
47 | public function __construct($apiKey = null) |
||||
48 | { |
||||
49 | if (isset($apiKey)) { |
||||
50 | $this->setApiKey($apiKey); |
||||
51 | } |
||||
52 | } |
||||
53 | |||||
54 | // ------------------------------------------------------------------------ |
||||
55 | |||||
56 | /** |
||||
57 | * Optimus::setApiKey |
||||
58 | * |
||||
59 | * @param string $apiKey |
||||
60 | * |
||||
61 | * @return static |
||||
62 | */ |
||||
63 | public function setApiKey($apiKey) |
||||
64 | { |
||||
65 | $this->apiKey = (string)$apiKey; |
||||
66 | |||||
67 | return $this; |
||||
68 | } |
||||
69 | |||||
70 | // ------------------------------------------------------------------------ |
||||
71 | |||||
72 | /** |
||||
73 | * Imageoptim::optimize |
||||
74 | * |
||||
75 | * @param string $image |
||||
76 | * @param string|null |
||||
77 | * |
||||
78 | * @return string |
||||
79 | * @throws \Exception |
||||
80 | */ |
||||
81 | public function optimize($image, $option = null) |
||||
82 | { |
||||
83 | // optimize: image optimization in the same format |
||||
84 | // webp: converts the image to the WebP image format |
||||
85 | if ($option === null) { |
||||
86 | $option = 'optimize'; |
||||
87 | } |
||||
88 | |||||
89 | $callApiUrl = $this->apiUrl . '/' . $this->apiKey . '?' . $option; |
||||
90 | |||||
91 | $headers = [ |
||||
92 | 'User-Agent: Optimus-API', |
||||
93 | 'Accept: image/*', |
||||
94 | ]; |
||||
95 | |||||
96 | $handle = curl_init(); |
||||
97 | curl_setopt_array($handle, [ |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
98 | CURLOPT_URL => $callApiUrl, |
||||
99 | CURLOPT_HTTPHEADER => $headers, |
||||
100 | CURLOPT_POSTFIELDS => file_get_contents($image), |
||||
101 | CURLOPT_BINARYTRANSFER => true, |
||||
102 | CURLOPT_RETURNTRANSFER => true, |
||||
103 | CURLOPT_HEADER => true, |
||||
104 | CURLOPT_SSL_VERIFYPEER => true, |
||||
105 | ]); |
||||
106 | |||||
107 | $response = curl_exec($handle); |
||||
0 ignored issues
–
show
It seems like
$handle can also be of type false ; however, parameter $ch of curl_exec() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
108 | $curlError = curl_error($handle); |
||||
0 ignored issues
–
show
It seems like
$handle can also be of type false ; however, parameter $ch of curl_error() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
109 | $header_size = curl_getinfo($handle, CURLINFO_HEADER_SIZE); |
||||
0 ignored issues
–
show
It seems like
$handle can also be of type false ; however, parameter $ch of curl_getinfo() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
110 | $body = substr($response, $header_size); |
||||
111 | // error catching |
||||
112 | if ( ! empty($curlError) || empty($body)) { |
||||
113 | throw new RuntimeException("Optimus-Error: {$curlError}, Output: {$body}"); |
||||
114 | } |
||||
115 | |||||
116 | return $body; |
||||
117 | } |
||||
118 | } |