Created
July 9, 2020 15:22
-
-
Save wgroenewold/3bb0e1f61eea49e5da46591e4d29c71c to your computer and use it in GitHub Desktop.
Calculate aspectratio from ACF oEmbed Field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function get_aspect_ratio_from_acf($fieldname, $postid){ | |
$url = get_field($fieldname, $postid, false); | |
$instance = new WP_oEmbed(); | |
$data = $instance->get_data($url); | |
$ratio = $this->float2rat(($data->width/$data->height)); | |
switch($ratio){ | |
case '21/9': | |
case '16/9': | |
case '4/3': | |
case '1/1': | |
$ratio = str_replace('/', 'by', $ratio); | |
break; | |
default: | |
$ratio = '16by9'; | |
} | |
return $ratio; | |
} | |
public function float2rat($n, $tolerance = 1.e-6) { | |
$h1=1; $h2=0; | |
$k1=0; $k2=1; | |
$b = 1/$n; | |
do { | |
$b = 1/$b; | |
$a = floor($b); | |
$aux = $h1; $h1 = $a*$h1+$h2; $h2 = $aux; | |
$aux = $k1; $k1 = $a*$k1+$k2; $k2 = $aux; | |
$b = $b-$a; | |
} while (abs($n-$h1/$k1) > $n*$tolerance); | |
return "$h1/$k1"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment