Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  179] [ 5]  / answers: 1 / hits: 18813  / 13 Years ago, mon, november 7, 2011, 12:00:00

I can't believe I can't find the formula for this. I am using a PHP script called SLIR to resize images. The script asks to specify an aspect ratio for cropping. I'd like to get the aspect ratio based on the width and height of the image being specified in a form I'm letting users enter these values in. For example, if a user enters a 1024x768 image, I would get an aspect ratio of 4:3. For the life of me, I can't find an example of the formula in PHP or Javascript I can use to get the aspect ratio values based on knowing the w,h and plug the aspect ratio into a variable.


More From » php

 Answers
63

There is no need for you to do any kind of calculation.


Just because it says aspect ratio doesn't mean it has to be one of a limited set of commonly used ratios. It can be any pair of numbers separated by a colon.


Quoting from the SLIR usage guide:



For example, if you want your image to be exactly 150 pixels wide by 100 pixels high, you could do this:


<img src="/slir/w150-h100-c150:100/path/to/image.jpg" alt="Don't forget your alt text" /> 

Or, more concisely:


<img src="/slir/w150-h100-c15:10/path/to/image.jpg" alt="Don't forget your alt text" />


Note that they didn't bother to reduce that even further to c3:2.


So, simply use the values as entered by the user: 1024:768.


If you want to be concise, calculate the greatest common divisor of the width and height and divide both of them by that. That would reduce your 1024:768 down to 4:3.


[#89254] Sunday, November 6, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
monetm

Total Points: 615
Total Questions: 103
Total Answers: 119

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
monetm questions
Fri, Feb 26, 21, 00:00, 3 Years ago
Wed, Sep 9, 20, 00:00, 4 Years ago
Sun, Jul 26, 20, 00:00, 4 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
;