0

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.

DaveLV2
  • 167
  • 1
  • 3
  • 10

1 Answers1

1

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.

Bogdan
  • 43,166
  • 12
  • 128
  • 129
  • Can u help with next problem i have? I edited the question. Thanks for help! – DaveLV2 Mar 24 '15 at 22:35
  • @DaveLV2 I've updated my answer with a solution for you second issue. – Bogdan Mar 24 '15 at 22:38
  • I may be wrong , but I think you misunderstood what `intervention/imagecache` package does, or at least how it does it. It uses Laravel's Cache package which allows caching and retrieving of cached information. It does that by storing the data withing the `storage` directory (where it's not publicly available for the browser). The `$img = Image::cache(...` only runs the `resize` and other image manipulation within the closure, if does not find a cached version. So if you'd want to return a cached version you're have to use `return $img->response();` as the response for a defined Laravel route. – Bogdan Mar 24 '15 at 22:53
  • I thought that i could create a cached resized image that would get deleted if it dosnt get viewed anymore. And that this is how would i do this.. Maybe i could create a dataURL from this cached image? – DaveLV2 Mar 24 '15 at 23:07
  • I've updated the answer explaining how to create a custom route that would return the cached images. Mind you, Laravel will not automatically clean the expired cache files. So if you want to clear any expired cached images you'll have to manage that separately. – Bogdan Mar 24 '15 at 23:17
  • @Bogdan correct me if I'm wrong, but with this approach the images wouldn't be added to the browser cache, being downloaded from server every time the page is refreshed. Or would there be any workaround to force `cache-control` headers on the downloaded image? – jfoliveira Aug 03 '15 at 21:00