Question

How i can generate a list with four items which should all have the same aspect ratio all depends on the original resolution of 480x360 ? The original aspect ratio 480x360 Also belonging to the list.

Youtube has an example on it open any video click on share and then on embedd

Now you see the list that i want as same as here described.

1. 420x315

    2. 480x360

3. 640x480
4. 960x720

*Edit i hope you now understand my question

Was it helpful?

Solution

<?php

// create class to easily add and store resolutions
class Resolution
{
public $x;
public $y;
}

// create our original resolution and calculate its aspect ratio
$origRes = new Resolution();
$origRes->x = 640;
$origRes->y = 480;
$originalRatio = ($origRes->x / $origRes->y);

// create valid resolution
$firstRes = new Resolution();
$firstRes->x = 1280;
$firstRes->y = 720;

// create another resolution
$secondRes = new Resolution();
$secondRes->x = 320;
$secondRes->y = 240;

// ...and so on; two are enough for the example
// you must learn about arrays and came up with proper solution how to populate them

// create array of resolutions from existing resolutions
$arrayOfRes = array($firstRes, $secondRes);

// another way of adding could be: $arrayOfRes[] = $thirdRes

// because floating point values aren't perfect on computers,
// we need some range value to check if the values are "close enough"
// in other words: it's our precision
$epsilon = 0.00001;

// iterate over all elements in array
foreach ($arrayOfRes as $res)
{
// calculate ratio
$ratio = $res->x / $res->y;

// abs - absolute value, thus always positive
// we check if difference is precise enough and print some text
if(abs($ratio - $originalRatio) < $epsilon)
echo'print it here or do something';
}
?>

That's my ten-minute-answer code as non-PHP programmer. I assume this code is valid, but there might be some errors.

[EDIT]

Checked with ideone.com, fixed minor bugs, and now it works as it should. https://ideone.com/lpW9DM

OTHER TIPS

900 / 1600 = 0.5625 get half 800 * 0.5625 = 450

To get aspectratio, width * 0.5625 = height

Code

$width = 640;
$height = 480;
$ratio = $height / $width;

function ratio($width){
   return $width * $ratio;
}

echo ratio(1280);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top