Unable to init from given url since updating to Laravel 5.5
I started getting this error suddenly after updating to Laravel 5.5. Any ideas what might be causing this?
The stack trace points to this line on the AbstractDecoder:
public function initFromUrl($url)
{
$options = array(
'http' => array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n".
"User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2\r\n"
)
);
$context = stream_context_create($options);
if ($data = @file_get_contents($url, false, $context)) {
return $this->initFromBinary($data);
}
throw new \Intervention\Image\Exception\NotReadableException(
"Unable to init from given url (".$url.")."
);
}
Who ever put @ before file_get_contents can shot himself in the head multiple times. In my case, PHP says:
file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name does not resolve
Remove manually the @ and check what PHP tells you - in the case above, my PHP Container has no access to a DNS so it won't find anything unless I grant access to. If it doesn't work, check Laravel MediaLibrary as it has less issues and works on Laravel 5.5.
I has the same issue. But it was happening only in production (dev and local were OK).
I tested a simple script with file_get_contents() on the same image URL and it was working.
Looking at Intervention documentation about make(), Image::make() accept a lot of various different source types.
So I fixed the issue updating this:
if ( ! Image::make( $url)->save( $appPath . $newFilename . '.' . $extension ) ) {
return false;
}
to this:
$file_data = file_get_contents( $url, false, stream_context_create( [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
] ) );
if ( ! Image::make( $file_data )->save( $appPath . $newFilename . '.' . $extension ) ) {
return false;
}
#283
please check Image you are dealing with hosted on server that have allow_url_fopen PHP.INI directive enabled.
You can check whether it enabled or not by simple a single php statement as below.
echo ini_get('allow_url_fopen') ? 'Enabled' : 'Disabled';
If It is found disabled, please enabled it and then check. Hope it work!!
Environment :
- allow_url_fopen = true
- Laravel 6.x
- PHP 7.4.1
- php -r "echo file_get_contents('url');" -> worked
- using on development env.
- echo file_get_contents('url'); from Laravel,
file_get_contents(url): failed to open stream: HTTP request failed!
- check url via browser : showed image
May I know what kind happen ?
Environment :
- allow_url_fopen = true
- Laravel 6.x
- PHP 7.4.1
- php -r "echo file_get_contents('url');" -> worked
- using on development env.
- echo file_get_contents('url'); from Laravel,
file_get_contents(url): failed to open stream: HTTP request failed!
- check url via browser : showed image
May I know what kind happen ?
I have the same issue, the file is retrievable and I've also tried http and not https.
I saw someone recommending storage_path, but that too generated a URL link
storage_path('/app/public/images/team_test.png')
The integration of the option to read data directly via URLs was a mistake. The task of this library is image manipulation. Reading data via HTTP should be done by a corresponding client. For this reason, Intervention Image Version 3 does not support reading URLs.