Image: Unable to init from given url since updating to Laravel 5.5

Created on 3 Sep 2017  路  4Comments  路  Source: Intervention/image

I started getting this error suddenly after updating to Laravel 5.5. Any ideas what might be causing this?

Most helpful comment

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!!

All 4 comments

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 ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kivivuori picture kivivuori  路  8Comments

DrDeath72 picture DrDeath72  路  4Comments

txusramone picture txusramone  路  6Comments

ludo237 picture ludo237  路  8Comments

ctf0 picture ctf0  路  4Comments