Search code examples
laravelintervention

Having problems with creating image cache with Intervention \ Image


So heres my code

public static function getImageThumb($link) {
    $domain = substr(Request::root(), 7);
    if(starts_with(Request::root(), 'http://')) {
        $domain = substr(Request::root(), 7);
    }
    $link = $domain.$link; // This is prety much something like this domain.name/uploads/image/13_0.jpeg
    $img = Image::cache(function ($image) use ($link) {
        return $image->make($link)->resize(230, 140);
    }, 5, true);
    return $img;
}

And it gives me this: Intervention \ Image \ Exception \ NotReadableException Image source not readable

I dont really know whats wrong here..

Thanks for help!

EDIT-------------------------

I fixed it like this:

public static function getImageThumb($link) {
    $link = trim($link, '/');

    $img = Image::cache(function ($image) use ($link) {
        return $image->make($link)->resize(230, 140);
    }, 5, true);

    return $img;
}

But how do i get the link to img now? So i can place it in src for img tag.


Solution

  • If you're going to use a URL as the source parameter for the make method, make sure it includes the scheme as well, or it will consider it to be a local file path. So get rid of the part where you strip the http:// from the URL, just use:

    public static function getImageThumb($link)
    {
        $link = Request::root() . $link;
    
        $img = Image::cache(function ($image) use ($link) {
            return $image->make($link)->resize(230, 140);
        }, 5, true);
    
        return $img;
    }
    

    Also, since the image not from a remote domain, it makes more sense to just read it from the filesystem, instead of making a HTTP request for it:

    public static function getImageThumb($link)
    {
        $path = public_path() . $link;
    
        $img = Image::cache(function ($image) use ($path) {
            return $image->make($path)->resize(230, 140);
        }, 5, true);
    
        return $img;
    }
    

    To return the cached version of a image, you have to have a dedicated route that retrieves the resized image. Something like this should do:

    Route::get('/images/{link}', function ($link)
    {
        // Repo will be the class implementing your getImageThumb method
        $img = Repo::getImageThumb($link);
    
        // This makes sure the HTTP response contains the necessary image headers
        return $img->response();
    });
    

    Now in your blade Blade template file you generate the URL like so:

    <img src="{{ asset('/images/' . $link) }}">
    

    By prepending /images to the actual link path you're hitting the route that is going to use the image cache if it is available. So your links would now look like this:

    http://domain.name/images/uploads/image/13_0.jpeg
    

    instead of

    http://domain.name/uploads/image/13_0.jpeg
    

    Of course you can use anything you like as the path prefix, not necessarily /images.