Skip to content

Commit 552226f

Browse files
authored
Merge pull request #60 from tamer-badawy/master
use curl insted of file_get_content if available
2 parents 3c6e46c + 64b3544 commit 552226f

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
"friendsofphp/php-cs-fixer": "~2",
2525
"phpunit/phpunit": "^9.5"
2626
},
27+
"suggest": {
28+
"ext-curl": "To download images from remote URLs if allow_url_fopen is disabled for security reasons"
29+
},
2730
"autoload": {
2831
"psr-4": {
2932
"League\\ColorExtractor\\": "src"

src/Palette.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,35 @@ public static function fromFilename($filename, $backgroundColor = null)
6363
return self::fromContents(file_get_contents($filename), $backgroundColor);
6464
}
6565

66+
/**
67+
* @param string $url
68+
* @param int|null $backgroundColor
69+
*
70+
* @return Palette
71+
*
72+
* @throws \RuntimeException
73+
*/
74+
public static function fromUrl($url, $backgroundColor = null)
75+
{
76+
if (!function_exists('curl_init')){
77+
return self::fromContents(file_get_contents($url));
78+
}
79+
80+
$ch = curl_init();
81+
try {
82+
curl_setopt($ch, CURLOPT_URL, $url);
83+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
84+
$contents = curl_exec($ch);
85+
if ($contents === false) {
86+
throw new \RuntimeException('Failed to fetch image from URL');
87+
}
88+
} finally {
89+
curl_close($ch);
90+
}
91+
92+
return self::fromContents($contents, $backgroundColor);
93+
}
94+
6695
/**
6796
* Create instance with file contents
6897
*

0 commit comments

Comments
 (0)