Search code examples
phplumenintervention

How to open an image at a URL with Intervention


I'm trying to open an image with Intervention

use Intervention\Image\ImageManagerStatic 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"ëPNG\r\n\x1A\n\x00\x00\x00\rIHDR\x00\x00\x01,\x00\x00\x01,\x04\x03\x00\x00\x00ïSôF\x00\x00\x00\ePLTE\x00Ö    ▀‗ ƒÏ \x1FÑ \x7F╠ ┐Õ _┐ ?▓ ¹\\t\r\x00\x00\x03¸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.


Solution

  • 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 Intervention\Image\ImageManagerStatic as Image;
    
    Route::get('/test', function() {
        return Image::make(file_get_contents('https://via.placeholder.com/300/09f/fff.png'))->response();
    });