Testing if the image exists

Asked

Viewed 712 times

0

I use Intervention in Laravel 5.5 and use its routes with filters to generate 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, in case I don’t carry a default image from the site. Thank you

  • Only php: http://php.net/manual/en/function.file-exists.php or http://php.net/manualen/function.is-file.php

  • @Miguel I tried these 2 methods, but as they are on different servers they do not work. The images are on the backend server and the frontend is on another server.

  • You have to change your question by saying that the images are in different places and that you access from.seguinte.forma has how to edit?

  • @Virgilionovic then I think the question is correct. Because the filter is on the same server as the images. But the server that will access the images via URL is on another machine. This answer I gave below was the way I found on the Frontend server. But I’m going to post now the way I was able to do right on the image server.

2 answers

0


I was able to solve by creating a new route where I check if the file exists and if it does not exist I put the default file and then forward it to the default Intervention route. It worked out that way.
Route definition:

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);
}

0

I don’t 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 certainly has the cost of 1 more connection with the server per image. If anyone knows a better way to know. Especially if you have how to do this directly on the image server would be TOP. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.