E
E
Evgeny Yakushov2018-02-24 18:44:26
PHP
Evgeny Yakushov, 2018-02-24 18:44:26

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

3 answer(s)
E
Evgeny Yakushov, 2018-02-24
@yevgenyyakushov

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;
}

F
fl3nkey, 2018-02-24
@FLUNKEY

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;

A
Alexander, 2018-02-24
@AlexZaw

Take any graphic editor and reduce while maintaining proportions, what's the problem then?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question