phplaravelnginxgdlib

Resize and cache images on the fly vs saving different sizes on server


I've been reading about resizing images on the fly using some php scripts with URL rewriting (/img/500x500/image-name.jpg), but a lot of people say it's a bad practice as it's a RAM and CPU consuming (1Mb image uses about 3Mb of RAM). So I thought I would just generate these images once and then save them in a cache folder, that way I can keep the original img and also the resized ones in this folder.

I'm currently saving 3 different sizes in the server when the image is being uploaded (100x100, 200x200, 500x500). Few time ago I used to display some images in 500x500, but now I have changed a little bit the design to display these images in only 300x300, So I'm now stuck with large images which could slow a little bit the page load!

So what I'm thinking to do is to save the original image (1500x1500), and when it's being viewed for the first time, i'll generate the appropriate thumbnails and save them in a folder on the server. That way, if I ever change the design again, I can simply delete the image cache folder to create the new thumbnails with the new sizes...

I still don't have any codes as the website is still being developed and I'm just trying to anticipate these facts. it's an ecommerce website

Do you think this is a wise way to deal with images??

I'm using php7, nginx, laravel 5 and also a CDN service (maxCDN)

Thanks in advance


Solution

  • I do this for thumbnails... pretty easy with Intervention image library... I mean, simple... http://image.intervention.io/

    // open an image file
    $img = Image::make('public/foo.jpg');
    
    // now you are able to resize the instance
    $img->resize(320, 240);
    
    // finally we save the image as a new file
    $img->save('public/bar.jpg');
    

    That's it... and yes, resizing on the fly is a bad idea...