Best practice with storing images on server - php

people! Please suggest me what is the best practice to store and retrieve images with my php application. My app uses different sizes of uploaded images (thumb, small, middle, big e.t.c). My current script creates all of the sizes after success image upload and stores 5 different version of every image. As the result, I'm running out of hosting storage space very fast.
So I want to ask: maybe better way to store only one original file and generate smaller sizes on the fly with php when image retrieves (maybe using some sort of cache)? Or which is the best practice by your opinion?

You can store 1 max size version of the image on the server and then use something like TimThumb to resize on load.

Related

Dynamic image resize system

I need a image resize system for a project so I decided to upload original images into DB encoded with with base64, I would like to keep stored only original images since they are easy to manage(add/edit/delete).
The output is made dynamically with php and IMagick atm.
The main problem I met is the output slow time, specifically the processing time needed it too long because I use resize+compress.
I need this compression because my visitors have slow internet connection and sometimes images are really big for just a preview.
Alternative solutions I though is to store in DB some resized images but won't be eficient because it will take more space and images size will change over time.
So, my question is: is there a method to deliver images dynamically faster? How?
You won't get around caching the resized images in some way. The thumbnails don't need to be stored in the data base; you could also write them to disk. Only recompute the resized versions if the original image has changed.

Caching using Joomla + PHP using images stores in MYSQL

Lets say I have a file called "getimage.php" to obtain an image all we need to do is getimage.php?imgid=1
My Server guy is warning me that pages with multiple images become very processor heavy. So would you suggest making a copies on the file server of theses image? and have a cron job delete files not being used after x amount of time?
or can I some how make 1 MYSQL query to obtain all the images? would this be less heavy then the getimage.php calling 1 SQL per image?
Any other suggestions?
I don't think you can get all the images in one shot from the database. In any case, is there any reason on why you're storing images in the database? This move has its benefits but, on the other hand, it has its drawbacks.
The only huge advantage I personally see from storing images in the database is when these images are subject to permissions (e.g. some users can see those images, some can't).

which method is faster to increase page speed by re sizing remote PNG images in php.?

Introduction: My Webpage shows the remote server PNG images of Size 90 by 90. I want to re-size it to 50 by 50. There are 300 of images on single webpage which make it slow. Normally My Webpage daily opens by 2k users.
By Searching a Lot I Found these methods but I don't Know Which is Better Please help..
Here Are My Approaches:
On The Fly.
re-size and Save it to database.
re-size and save it to my web directory.
I am using mysql to fetch rows each row has name, 'url to image', id , time, description. This 'url to image' is my problem.
I think Saving to MYSQL Database is good which make retrieving from it easier and maybe faster than others.
Please provide me any idea of coding where to start. I am Trying this image re-sizing for first time..
Thank You
Go with #3, don't store images in a database, and if you have much views and many images, doing it on the fly isn't very effective.
No, do it once, and save it to the filesystem, with a link in the database like you have now.
Also, if possible, do it as soon as a new image is uploaded.
I wouldn't personally save the image to the database, but that is more my personal choice.
I would probably opt for resizing them all and saving them out to the website, and returning the correct images than resizing on the fly. If you're always going to be returning the image in 50x50, why are they uploaded as 90x90 in the first place? Can't you resize them when you upload them?
Well :
On the fly : slow and incredibly ineficient, as you saw
Save on database : are you out of your mind, do NOT save images in DB
Save in web directory : that's my boy. Resize the image the first time it is accessed and cache it on your directory. Next time, you won't have to download it and resize, just serve it. Delete useless files every week or so.

Images upload with PHP: how to optimize space, bandwidth and performance

I'm planning to develop an area where the users can upload pictures. I know how to upload a picture on the server using PHP but the problem is what is the best practice to develop a performing system.
The idea is to display in different pages thumbs and I would like to know if it's a better idea to save two different images (thumb + original) on the server or if it's better to save just the original and create all the thumbs on the fly. Thumb + original means more space on the server, whereas the option "thumbs on the fly" means most likely a server overload.
I found couple of good scripts to resize and cropping on the fly but not sure if it's a good idea to use especially if the web site has few thousands visitor per day (or may be more in the future just to be optimistic/pessimistic).
Absolutely generate and save the thumbnails on disk. Storage is cheap.
You can generate some thumbnails and save them on disk but in the long term that's problematic due to different devices needing different sizes, different formats, etc.
If you are already saving the uploaded images on S3, Azure Storage, or Google Cloud I recommend to use some on the fly image processing service like imglab or cloudinary.
With these services you can generate many different types of cropping, and serving them in different (modern) formats like WebP or AVIF so you don't need to generate them before hand. SEO will be improved wit this option too.
Additionaly images will be behind a global CDN so users will get the images in a fast way independent or their location.

PHP image thumbnail performance?

I've recently started trying to increase my sites performance. One of the biggest obstacles I have are the number of thumbnails being displayed.
I currently use the full size image and scale it down by defining a height/width value in the img tag. This doesn't seem very efficient so my question is whats the recommended way to display thumbnails? Should I maintain a second table in the DB for thumbnails or is there a better solution?
Processing images takes its (cpu) toll, you should better avoid it where possible.
My advice:
Create the thumbnails while uploading the images into separate image files, this way you can determine when to create/resize them - and not during runtime.
If you want the links to the thumbnails in a separate database field or derive it from the original name, is entirely up to you - both ways work.
This makes additional performance boosters easier to implement too (p.e. caching).
I've implemented a similar process in a php based project, its a good way to scale out. In my case, I am creating the thumbnails nightly via cron, because system load is very low in that time.
If using an uploader to get the images on your website, have PHP resize the original image and upload both the original and a smaller thumb with some kind of prefix in the name.
This way you can easily get the images from your database and just use "filename.jpg" for your normal images and "thumb_filename.jpg" for your thumb.
Same can be done without an uploader of course but you'd have to manually create/upload the thumbnails.
For example create seperate folder in images call it thumnails (images/thumbnails) add there put files prefixed with file size for ex: "original_file_name_200X200.jpg", store on database "original_file_name" and extension "jpg" to seperate fields (name, ext) then when you need to display it select name add size prefix and add extension you get /images/thumbnails/file_200X200.jpg this way you can add later more sizes leaving original untouched.
You are looking for something like what can be found here: http://dtbaker.com.au/random-bits/php-simple-image-resize-thumbnail-generator.html (this turns all images into jpegs)
Like #martincarlin87 stated - Just need to add a check to see if it exists in the thumbnail directory and either send the information or create and send it through. This can be turned into a function as well.
I use phpTumb to create thumbnails on the fly.
You could also use it while uploading a picture to change its dimesions.
It has many other features. Check it out.
you could use a program like imagemagick to make proper thumbnails.
If its your personal site you could batch resize (theres a powertool for xp that does this) and then upload to a 200 directory and change your code. Obviously this relies on you uploading the images.
imagemagick will need to be installed on the server but will resize and allow you to play with the size of the images

Categories