Basically I want to upload an image (which i've sorted) and scale it down to certain constraints such as max width and height but maintain the aspect ratio of the original image.
I don't have Imagick installed on the server - otherwise this would be easy.
Any help is appreciated as always. Thanks.
EDIT: I don't need the whole code or anything, just a push in the right direction would be fantastic.
I was thinking about how to achieve this and i came with a pretty nice solution that works in any case... Lets say you want to resize heavy images that users upload to your site but you need it to maintain the ratio. So i came up with this :
In this case if width is bigger than height, i wanted the width to be 600px and if the height was bigger than the width, i wanted the width to be 400px
I know you are looking for a divisor that will allow resize your image proportionally. Check this demo
How to get our divisor mathematically
lets assume our original image has width x and height y; x=300 and y = 700
Maximum height and maximum width is 200;
First, we will check which dimension of the image is greater than the other. Our height (y) is greater than width(x)
Secondly, we check if our height is greater than our maximum height. For our case, our height is greater than the maximum height. In a case where it less that the maximum height, we set our new height to our original height.
Finally, we look for our divisor as shown below
and vice versa for the width if it greater than height
See the full example and try it using the working demo .
I had written a peice of code like this for another project I've done. I've copied it below, might need a bit of tinkering! (It does required the GD library)
These are the parameters it needs:
It will scale down or up an image to the maximum width or height
Actually the accepted solution it is not the correct solution. The reason is simple: there will be cases when the ratio of the source image and the ratio of the destination image will be different. Any calculation should reflect this difference.
Please note the relevant lines from the example given on PHP.net website:
The full example may be found here: http://php.net/manual/en/function.imagecopyresampled.php
There are other answers (with examples) on stackoverflow to similar questions (the same question formulated in a different manner) that suffer of the same problem.
Example:
Let's say we have an image of 1630 x 2400 pixels that we want to be auto resized keeping the aspect ratio to 160 x 240. Let's do some math taking the accepted solution:
height = 240 width = 1630 * ( 160/2400 ) = 1630 * 0.0666666666666667 = 108.6666666666667 108.6 x 240 it's not the correct solution.
The next solution proposed is the following:
height = 240; width = 1630 / 2400 * 240 = 163 It is better (as it maintain the aspect ratio), but it exceeded the maximum accepted width.
Both fail.
We do the math according to the solution proposed by PHP.net: width = 160 height = 160/(1630 / 2400) = 160/0.6791666666666667 = 235.5828220858896 (the else clause). 160 x 236 (rounded) is the correct answer.
Also I have been using the accepted answer but it does not keep the ratio in some cases. I have found some good answers on the forum and have put them in together and finally created a Class which resizes an image. As extra function u can put a watermark text.
u can see what happens when choose to crop or not, if not a transparent area will be added to the new resized image.
This example is more than asked, but I think it is a good example.
I found a mathematical way to get this job done
Github repo - https://github.com/gayanSandamal/easy-php-image-resizer
Live example - https://plugins.nayague.com/easy-php-image-resizer/