I have a competition website that uses WordPress allowing users to submit images. There are 700+ entries with up to 6 images each, with a maximum filesize of 3mb for each image.
I’ve then listed all the entries as posts, then on each post it displays the images.
Each image is displayed using the_field('image1');
, the_field('image2');
, the_field('image3');
etc.
The problem I have is that the page is then trying to load 6 images at 3mb’s a time. Is there a way to optimise the image on the fly and show it at 50% quality?
You can resize the picture on the fly after extracting it from a database (or file) without having to create another temporary file. Presumably you have a URL which gets an image from the database.
If you pass NULL as a file name to
imagejpeg
it will write the jpeg to standard out. The 3rd parameter is the quality level, in this case 50%.When the image is uploaded , you could save 2 images.
1. The original image
2. A low resolution copy of the image
So when the user views the page, the low res image is shown initially while the original image is being downloaded in the background.
You could easily find some ready code to resize images through php. Like this one.
http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/
There is no way to resize images on the fly. There is only straight way to make CPU choke with such a denial-of-service self-attack.
Images have to be pre-resized to reasonable size and quality right after upload. Every web-site in the world does it this way.