|
12 | 12 | */
|
13 | 13 | class Image
|
14 | 14 | {
|
| 15 | + |
15 | 16 | const ALIGN_LEFT = 'left';
|
16 | 17 | const ALIGN_CENTER = 'center';
|
17 | 18 | const ALIGN_RIGHT = 'right';
|
@@ -284,27 +285,41 @@ public function base64(string $base64): Image
|
284 | 285 | * (Static method) Open image from URL with cURL.
|
285 | 286 | *
|
286 | 287 | * @param string $url Url of the image file
|
| 288 | + * @param array $curlOptions cURL options |
| 289 | + * @param bool $failOnError If true, throw an exception if the url cannot be loaded |
287 | 290 | * @return Image Return Image instance
|
288 | 291 | */
|
289 |
| - public static function fromCurl(string $url): Image |
| 292 | + public static function fromCurl(string $url, array $curlOptions = [], bool $failOnError = false): Image |
290 | 293 | {
|
291 |
| - return (new Image)->curl($url); |
| 294 | + return (new Image)->curl($url, $curlOptions, $failOnError); |
292 | 295 | }
|
293 | 296 |
|
294 | 297 | /**
|
295 | 298 | * Open image from URL with cURL.
|
296 | 299 | *
|
297 | 300 | * @param string $url Url of the image file
|
| 301 | + * @param array $curlOptions cURL options |
| 302 | + * @param bool $failOnError If true, throw an exception if the url cannot be loaded |
298 | 303 | * @return $this Fluent interface
|
299 | 304 | */
|
300 |
| - public function curl(string $url): Image |
| 305 | + public function curl(string $url, array $curlOptions = [], bool $failOnError = false): Image |
301 | 306 | {
|
| 307 | + $defaultCurlOptions = [ |
| 308 | + CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0', |
| 309 | + CURLOPT_RETURNTRANSFER => 1, |
| 310 | + CURLOPT_TIMEOUT => 5, |
| 311 | + ]; |
| 312 | + |
302 | 313 | $curl = curl_init();
|
303 | 314 | curl_setopt($curl, CURLOPT_URL, $url);
|
304 |
| - curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0'); |
305 |
| - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
306 |
| - curl_setopt($curl, CURLOPT_TIMEOUT, 5); |
| 315 | + curl_setopt_array($curl, $defaultCurlOptions + $curlOptions); |
| 316 | + |
307 | 317 | $image = curl_exec($curl);
|
| 318 | + |
| 319 | + if($failOnError && curl_errno($curl)){ |
| 320 | + throw new \Exception(curl_error($curl)); |
| 321 | + } |
| 322 | + |
308 | 323 | curl_close($curl);
|
309 | 324 |
|
310 | 325 | if ($image === false) {
|
|
0 commit comments