I have an application that allows users to upload images. The test case I use is a jpeg of 1.6MB with dimensions 3872 x 2592px. The upload script in the back-end will resize the uploaded image into 6 additional formats:
- square (small 75 x 75)
- Small Thumb (50 x 38)
- Thumb (100 x 76)
- Small (240 x 161)
- Medium (500 x 378)
- Large (1024 x 774)
I know it’s a lot but trust me, I need this. I do the resizing using Code Igniter’s Image Manipulation class, which uses either GD, GD2 or ImageMagick to do the resizing. I first configured it to use GD2 and noticed that the total resize process takes 11 secs.
Since the user has to wait for this process, it is not acceptable. After a lot of reading I learned that ImageMagick is a much faster and efficient manipulation library, so I switched to that:
$sourceimage = $data['filedata']['file_path'] . $data['imagedata']['user_id'] . "/" . $imageid . $data['filedata']['file_ext'];
$resize_settings['image_library'] = 'imagemagick';
$resize_settings['library_path'] = '/usr/bin';
$resize_settings['source_image'] = $sourceimage;
$resize_settings['maintain_ratio'] = false;
$resize_settings['quality'] = "100%";
$this->load->library('image_lib', $resize_settings);
Much to my surprise, the resize process now takes longer: 15 secs to be specific.
Having a look at my log I see that each resize action takes 2 seconds, no matter the file format it is resizing to. I guess this is because I always resize from the original, which is very large.
I would hate to offload the resizing process to a scheduled process, because that would decrease the usability of the site. It would mean that users have to wait a few minutes before they can start seeing/working with the image.
So, are there any smart ways to dramatically speed up this resizing process so that I can keep it in real-time? Just be clear: allowing for smaller resolutions is not an option, this is a photography site I’m building. Also, I really need the six formats mentioned.
As an idea, you could resize from the uploaded size to a more sensible intermediate size and then use this as the basis for further operations.
Alternatively, you could exec out to the command line version of ImageMagick and carry out (at least the bulk of) the image transformations in the background using the process described within Asynchronous shell exec in PHP.
Finally, whilst it’s a bit off topic, are you going to allow for portrait orientation, or isn’t this likely to be a factor?