4

I making a web app where uploaded photos are stored in /app/storage. To show the file I am using a route ex: showphoto/{id} (Paths are stored in DB)

public function showphoto($id){
     $photo = Photo::findOrFail($id);
     return $this->getFile($photo);
}
private function getFile($f){
        if($f->path){
            $file = storage_path($f->path.'/'.$f->origin_name);
            if (File::exists($file)) {
                $contents = File::get($file);
                switch($f->ext) {
                    case "gif": $ctype="image/gif"; break;
                    case "png": $ctype="image/png"; break;
                    case "jpeg":
                    case "jpg": $ctype="image/jpeg"; break;
                    case "pdf": $ctype="application/pdf"; break;
                    default:
                }
                $response = Response::make($contents, 200);
                $response->header('Content-Type', $ctype);
                return $response;
            } 
        }
    }

To show image I am using

{{ HTML::image(route('showphoto', $photo->id), $photo->getName(), array('class'=>'img-thumbnail', 'width' => '100', 'height'=>'100')) }}

Question: Some file are more then 2 MB and when I have a list of them they are loading very slowly, So on the list I want to show just a Thumb of the photo. It is possible to create a temporary thumb that will not be stored anywhere? Or it is not a good idea to create a temporary thumb each time I load a page. Thank you in advance.

SergkeiM
  • 3,934
  • 6
  • 36
  • 69
  • Its never a good practice at all what you think. best practic is at the time of saving image try to cteate two image original and thumb. and thumb image path will store on databsae.show that thumb image and give an option of click orsomething that when he click on thumb then original image will shown. – Alive to die - Anant Apr 10 '15 at 09:20
  • Thank you for your comment, but if I want a sor of function like getThumb($width, $height): So I can get a thumb of original image with different size? – SergkeiM Apr 10 '15 at 09:22
  • Try to resize them with same height and width. If not happening also at the time of showing define their height and width, so that they look similar size. – Alive to die - Anant Apr 10 '15 at 09:26
  • Of course, you can create a temporary thumbnail. It's generally good to do it when uploading the photo, as this means you don't have to do extra processing when loading the page. However, if you want to do it when loading the page you could create a thumbnail and then cache it. Subsequent requests should load the cached thumbnail. A good package to use with Laravel when dealing with image manipulation is Intervention Image: http://image.intervention.io/ – Jonathon Apr 10 '15 at 11:40

1 Answers1

3

If you have a bunch of large images that you are going to be processing in some way when a page is loaded you're probably going to have a bad time. All that extra processing is going to really slow everything down.

What you could do is create a thumbnail for an image when it is uploaded and store that somewhere. That way, you need only load your thumbnails instead of the larger images.

Alternatively, if you prefer to be able to specify the size of the thumbnail in your page another solution could be to generate your thumbnails at your specified size when the page loads. You'll need to make sure that you cache these thumbnails so you can use just load them in going forward though. If you opt for this approach the first time you load your page will probably take a while, but after that, subsequent page loads will be much quicker, since it will be using pre-created, cached images.

A great package to use with Laravel when manipulating images is Intervention Image:

http://image.intervention.io/

Jonathon
  • 15,873
  • 11
  • 73
  • 92
  • 1
    Thank you for your answer, the think is if I load now the image Like: The image still loads slower because it is still 2 mb. Thats why I want to resize it before showing, I tried Intervation, but when making response() it return Content-type and etc... it doesnt return an image it self. – SergkeiM Apr 10 '15 at 13:53
  • No problem. The image should not be 2mb still because you should have previously resized it and saved that smaller version. You should use that image instead. – Jonathon Apr 10 '15 at 15:16