Skip to content
Advertisement

How to open an image at a URL with Intervention

I’m trying to open an image with Intervention

use InterventionImageImageManagerStatic as Image;

$image = Image::make('https://via.placeholder.com/300/09f/fff.png');

but when dumping it, there is just null returned.

If I dd the actual image

dd(file_get_contents('https://via.placeholder.com/300/09f/fff.png'));

I see

b"ëPNGrnx1Anx00x00x00rIHDRx00x00x01,x00x00x01,x04x03x00x00x00ïSôFx00x00x00ePLTEx00Ö    ▀‗ ƒÏ x1FÑ x7F╠ ┐Õ _┐ ?▓ ¹\trx00x00x03¸IDATx ▶"

I’ve tried

 $image = Image::make(file_get_contents('https://via.placeholder.com/300/09f/fff.png'));

But dd($image) also returns null.

I also tried

private function get_content($URL){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $URL);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
$image = Image::make($this->get_content('https://via.placeholder.com/300/09f/fff.png'));

dd($image) returns null for that too.

Do you have any ideas on how I can open the image with this library? I’m using Lumen, and have tried to run it using both Docker and php artisan serve.

Could it be anything to do with Composer?

Update

I also wondered whether it might be due to the driver. Gd is the default one for Image Intervention. I made sure that and imagick are enabled in the php.ini and tried both gd (default) and imagick as drivers

Image::configure(['driver' => 'imagick']);

but dd($image) stays null.

Also, checking storage/logs/laravel.log does not show anything.

Final Update

The solution turned out to be related to composer. I deleted the composer.lock and the vendors contents and performed a fresh composer install and $image now shows the required data.

Advertisement

Answer

Image::make(file_get_contents('https://via.placeholder.com/300/09f/fff.png') returns the Image object.

So you can return the image with ->response().

use InterventionImageImageManagerStatic as Image;

Route::get('/test', function() {
    return Image::make(file_get_contents('https://via.placeholder.com/300/09f/fff.png'))->response();
});
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement