Search code examples
laravellaravel-8intervention

Laravel Intervention - Unable to init from given binary data


I am using "intervention/image": "^2.5" in one of my projects. It is working well except for one part of the code where im retrieving an image.

I keep getting a Unable to init from given binary data error and i cant figure out why.

The file exists but i cant figure it out.

My code is as follows;

$path = '/image-storage/492/1/testimage.jpg';
$file = Storage::get($path);
ob_end_clean();

return Image::make($file)->response();

Below is my filesystem.php config for local

'local' => [
    'driver' => 'local',
    'root' => storage_path('app'),
],

Solution

  • Storage::get($path) returns file contents as a string which may not be cast to valid binary data for Image::make() to be able to read.

    You can try by passing the path to the image to the make method

    //If testimage.jpg is located at storage/app/image-storage/492/1
    $path = storage_path('app/image-storage/492/1/testimage.jpg');
    
    //if testimage.jpg is located at storage/app/public/image-storage/492/1/, then
    //$path = storage_path('app/public/image-storage/492/1/testimage.jpg');
    
    return Image::make($path)->response();
    

    OR you can create a new Illuminate\Http\File instance and then pass it to the make method

    //If testimage.jpg is located at storage/app/image-storage/492/1
    $path = storage_path('app/image-storage/492/1/testimage.jpg');
    
    //if testimage.jpg is located at storage/app/public/image-storage/492/1/, then
    //$path = storage_path('app/public/image-storage/492/1/testimage.jpg');
    
    $file = new \Illuminate\Http\File($path);
    
    Image::make($file)->response();
    

    Intervention image accepts binary data or SplFileInfo instance. Illuminate\Http\File extends Symfony\Component\HttpFoundation\File\File which extends \SplFileInfo.

    Intervention Image - Reading Images