Unable to init from given url in intervention/image": "^2.3
Asked Answered
B

4

7

I want to save the images from google plus as below url it is work as well in local computer but I got below error when upload to ubuntu14.

$image = Image::make('https://lh6.googleusercontent.com/-Gcp_Wjj7yA0/AAAAAAAAAAI/AAAAAAAAAB8/hl1xcz4FnEI/photo.jpg')
            ->resize(100, 100)->save(public_path('image/userface/fuck.jpg'));

Errors

Unable to init from given url
Beatrizbeattie answered 21/2, 2017 at 10:2 Comment(1)
did you find the issue?Selinaselinda
S
8

I have found a workaround for this problem. When we re-fetch the image, 99% it works, so i modified saving function to try to save for 3 times if it fails even then, we abort.

Solution

    $flag = true;
    $try = 1;
    while ($flag && $try <= 3):
        try {
            Image::make($path)->save(public_path("{$public_app_path}/" . $filename));
            //Image migrated successfully
            $flag = false;
        } catch (\Exception $e) {
            //not throwing  error when exception occurs
        }
        $try++;
    endwhile;
Selinaselinda answered 29/8, 2018 at 11:34 Comment(1)
For me it was an issue with using https://, modifying it to use http:// urls fixed the issue. Think it's a certificate problem on my localhost.Teucer
G
3

In Image Intervention Package Parameters, that can be read by below URL.

http://image.intervention.io/api/make

2nd Point:- That URL of an image (allow_url_fopen must be enabled).

So please check that, the 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. It works !!

Griz answered 1/9, 2018 at 13:48 Comment(0)
R
0

Try it here:

php artisan storage:link

Reinhart answered 30/12, 2019 at 14:7 Comment(0)
S
0

This is most probably because you may be using a self-signed certificate and it can't be verified properly.

InterventionImage uses stream_context_create which by default verifies the certificate.

Since Intervention Image doesn't directly allow a workaround via options, open vendor/intervention/image/src/Intervention/Image/AbstractDecoder and inside the function initFromUrl() add this

'ssl' => array(
   'verify_peer'   => false,
)

It will look something like this:

$options = [
   'http' => [
      'method'=>"GET",
      'protocol_version'=>1.1, // force use HTTP 1.1 for service mesh environment with envoy
      'header'=>"Accept-language: en\r\n".
       "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36\r\n"
    ],
    'ssl' => array(
       // don't validate certificate
       'verify_peer'   => false,

       // alternatively you may add our certificate file path here and make
       // 'cafile'        => __DIR__ . '/cacert.pem',
       // 'verify_depth'  => 5,
       // 'CN_match'      => 'your-local-domain.com'
    )
 ];

$context  = stream_context_create($options);

It's not the perfect solution but it will allow you to continue to work locally.

Sinasinai answered 23/12, 2022 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.