Test if the image exists

0

Personal I use Intervention in Laravel 5.5 and use its routes with the filters to generate the images dynamically. I would like to know how I can test in the filter class and the image I passed it exists on the disk, otherwise I will upload a default image of the site. Thanks

    
asked by anonymous 18.04.2018 / 21:18

2 answers

0

I was able to solve this by creating a new route where I checked if the file exists and if it does not exist I put the default file and then go to the Intervention default route. You gave it right this way. Route setting:

Route::get('/imagecache/{template}/{filename}', 'ApiController@imagecache')->where('filename','[ \w\.\/\-\@\(\)]+');

Route Controller:

public function imagecache($template, $filename)
{
    if(!Storage::disk('public')->exists($filename)){
        $filename='images/noimage.jpg';
    }
    return response()->redirectTo('/imagecache/'.$template.'/'.$filename);
}
    
23.04.2018 / 15:30
0

I do not know if it's the best way but I did it like this:

@if(strtoupper($file_headers = @get_headers('http://www.meuservidor.com.br/miniatura/'.$item['imagem'])[0])=='HTTP/1.1 200 OK')

So it worked perfectly, but it sure has the cost of 1 more connection to the server per image. If anyone knows a better way I'd like to know. Especially if you have how to do this direct on the image server would be TOP. Thanks

    
19.04.2018 / 15:33