Answer the question
In order to leave comments, you need to log in
How to find the aspect ratio for an image?
In general, the problem is this, I can not figure out what to do with the sides of the picture. Let's say we have an image 780x1040, we need to reduce it, leaving the proportions of the image, but there is a condition, its width must be less than 660, and its height must be less than 500.
Answer the question
In order to leave comments, you need to log in
Krch, everything is decided:
// Выбор оптимального размера
$initialSizeWidth = 660;
$initialSizeHeight = 500;
// $image[0] - ширина, $image[1] - высота
if( $image[1] > $image[0] ){
$adaptedWidth = $initialSizeWidth;
$coefficienWidth = $image[0] / $initialSizeWidth;
$prevHeight = $image[1] / $coefficienWidth;
$adaptedHeight = $initialSizeHeight;
$coefficienHeight = $prevHeight / $initialSizeHeight;
$adaptedWidth = $initialSizeWidth / $coefficienHeight;
}else{
$adaptedHeight = $initialSizeHeight;
$coefficienHeight = $image[1] / $initialSizeHeight;
$prevWidth = $image[0] / $coefficienHeight;
$adaptedWidth = $initialSizeWidth;
$coefficienWidth = $prevWidth / $initialSizeWidth;
$adaptedHeight = $initialSizeHeight / $coefficienWidth;
}
By dividing the width by the height, we get the aspect ratio relative to the width.
If we want to change the height while maintaining proportions, to get the width we need to multiply the new height by the aspect ratio.
If we want to change the width while maintaining proportions, we need to divide the new width by the aspect ratio to get the height.
var width = 1280;
var height = 720;
var ratio = width / height;
//новая ширина относительно новой высоты
var new_height = 500;
var new_width = new_height * ratio;
//новая высота относительно новой ширины
var new_width = 800;
var new_height = new_width / ratio;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question