Skip to content

Commit

Permalink
Merge pull request #60 from tamer-badawy/master
Browse files Browse the repository at this point in the history
use curl insted of file_get_content if available
  • Loading branch information
colinodell authored Sep 24, 2022
2 parents 3c6e46c + 64b3544 commit 552226f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"friendsofphp/php-cs-fixer": "~2",
"phpunit/phpunit": "^9.5"
},
"suggest": {
"ext-curl": "To download images from remote URLs if allow_url_fopen is disabled for security reasons"
},
"autoload": {
"psr-4": {
"League\\ColorExtractor\\": "src"
Expand Down
29 changes: 29 additions & 0 deletions src/Palette.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,35 @@ public static function fromFilename($filename, $backgroundColor = null)
return self::fromContents(file_get_contents($filename), $backgroundColor);
}

/**
* @param string $url
* @param int|null $backgroundColor
*
* @return Palette
*
* @throws \RuntimeException
*/
public static function fromUrl($url, $backgroundColor = null)
{
if (!function_exists('curl_init')){
return self::fromContents(file_get_contents($url));
}

$ch = curl_init();
try {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($ch);
if ($contents === false) {
throw new \RuntimeException('Failed to fetch image from URL');
}
} finally {
curl_close($ch);
}

return self::fromContents($contents, $backgroundColor);
}

/**
* Create instance with file contents
*
Expand Down

0 comments on commit 552226f

Please sign in to comment.