Faster rotate and/or resize options
Performing image rotations and resizing in PHP is very slow. It might be a good idea to add an option to the config pages that allows an external program to be specified to perform the operations (eg: imagemagick).
Both command will need to be specified as a full command with arguments - which means we'll have to provide placeholder variables for the following:
Both command will need to be specified as a full command with arguments - which means we'll have to provide placeholder variables for the following:
- It's available on all the major OSes supported by Swisscenter
- Original image (absolute path)
- Location to write resized/rotated image to (absolute path)
- For rotate: to rotate by (two variables, one clockwise, the other anti-clockwise).
- For resize: and of the image to create. We may need to be able to specify if the image must constrain it's aspect ratio or not.
Leave a comment
Keep in mind, that those commands will modify the original file - so it's not enough to simply call them, but we must create a copy of that image first. So I'ld recommend the following procedure, even if it slows down things a little bit again:
1. make a copy of the image-to-rotate in the temp folder
1. run the command against that file using exec()
1. use imageCreateFromJpeg() against the rotated file to send to the browser
1. delete the temporary (rotated) file
1. send the image created in step 3 to the requesting browser
As some tests showed, the rotation itself is about 8 times faster using convert with the exec() function, compared to rotating it within PHP. Even if the copy plus imageCreateFromJpeg will slow it down a little bit, speed improvement would still fire it up to factor 6 (guessed - not yet tested), making it the fastest available method.
1. make a copy of the image-to-rotate in the temp folder
1. run the command against that file using exec()
1. use imageCreateFromJpeg() against the rotated file to send to the browser
1. delete the temporary (rotated) file
1. send the image created in step 3 to the requesting browser
As some tests showed, the rotation itself is about 8 times faster using convert with the exec() function, compared to rotating it within PHP. Even if the copy plus imageCreateFromJpeg will slow it down a little bit, speed improvement would still fire it up to factor 6 (guessed - not yet tested), making it the fastest available method.
Keep in mind, that those commands will modify the original file - so it's not enough to simply call them, but we must create a copy of that image first. So I'ld recommend the following procedure, even if it slows down things a little bit again:
1. make a copy of the image-to-rotate in the temp folder
1. run the command against that file using exec()
1. use imageCreateFromJpeg() against the rotated file to send to the browser
1. delete the temporary (rotated) file
1. send the image created in step 3 to the requesting browser
As some tests showed, the rotation itself is about 8 times faster using convert with the exec() function, compared to rotating it within PHP. Even if the copy plus imageCreateFromJpeg will slow it down a little bit, speed improvement would still fire it up to factor 6 (guessed - not yet tested), making it the fastest available method.
1. make a copy of the image-to-rotate in the temp folder
1. run the command against that file using exec()
1. use imageCreateFromJpeg() against the rotated file to send to the browser
1. delete the temporary (rotated) file
1. send the image created in step 3 to the requesting browser
As some tests showed, the rotation itself is about 8 times faster using convert with the exec() function, compared to rotating it within PHP. Even if the copy plus imageCreateFromJpeg will slow it down a little bit, speed improvement would still fire it up to factor 6 (guessed - not yet tested), making it the fastest available method.
I've found a very small program (~200k per executable) available for Windows, Linux and Mac that can resize and (optionally) colour correct images.
I contacted the author and he is happy for it to be distributed with SwissCenter and as a bonus he will add the functionality to rotate an image in steps of 90 degress within the next week or so as a special request.
Example conversion times for resizing a * 12.6 Mbyte (10 megapixel)* image to 10% of it's original size on my laptop are as follows:
8.02 sec for loading
0.06 sec for resizing
0.10 sec for saving
8.19 sec for all operations
If automatic colour correction is also enabled this takes an additional 12.42 seconds (obviously this would be faster for much smaller images).
I believe that this would be a good utility to include with Swisscenter for the following reasons:
I contacted the author and he is happy for it to be distributed with SwissCenter and as a bonus he will add the functionality to rotate an image in steps of 90 degress within the next week or so as a special request.
Example conversion times for resizing a * 12.6 Mbyte (10 megapixel)* image to 10% of it's original size on my laptop are as follows:
8.02 sec for loading
0.06 sec for resizing
0.10 sec for saving
8.19 sec for all operations
If automatic colour correction is also enabled this takes an additional 12.42 seconds (obviously this would be faster for much smaller images).
I believe that this would be a good utility to include with Swisscenter for the following reasons:
- It doesn't require any form of installation
- The binaries are very small
- It's quick
- We can include an additional configuration option on the 'Image Options' screen to enable/disable the auto colour correction for any users that would like that feature.
Since* 67 was closed as duplicate of this one (which it not exactly was), I add the remainings here: The process of thumbnail creation with PHP internal functionality could be sped up by changing the order of 'first rotate then resize' to 'first resize then rotate'. The time needed for resizing will be the same in both cases, but rotation would be much faster.
Tried that by changing the order in thumb.php - but unfortunately that breaks the aspect ratio. So it's not that easy - but must be something like that.
Tried that by changing the order in thumb.php - but unfortunately that breaks the aspect ratio. So it's not that easy - but must be something like that.
Code added to optimise the resize/rotate and handle the aspect ratio correctly. revision:703