寻找在分辨率数组与最近的纵横比最接近的较大分辨率 [英] Finding closest larger resolution with nearest aspect ratio in an array of resolutions

查看:208
本文介绍了寻找在分辨率数组与最近的纵横比最接近的较大分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组:

$resolutions = array(
    '480x640',
    '480x800',
    '640x480',
    '640x960',
    '800x1280',
    '2048x1536'
);

我想要检索与最近的高宽比(相同的方向)接近的更大的值。

I want to retrieve closest larger value with the nearest aspect ratio (same orientation).

所以,在 $针='768x1280'的情况下 - 800x1280 结果
而且,在 $针=320×240的情况下 - 640×480 。虽然这里的最接近的是 480×640 它不应该被匹配,因为它的长宽比不同太多。
等,等等。

So, in case of $needle = '768x1280' - 800x1280.
And, in case of $needle = '320x240' - 640x480. While the closest here is 480x640 it shouldn't be matched, because its aspect ratio differs too much. So on, and so forth.

我有一组分辨率的图像,作为在 $决议指定。这些图像将要被用于智能电话的壁纸​​。

I have a set of images with resolutions as specified in $resolutions. Those images are going to be used for smartphone wallpapers.

使用JavaScript的,我送过与 screen.width 的请求和 screen.height 确定 $针

With JavaScript, I am sending over a request with screen.width and screen.height to determine $needle.

在服务器端,我要取定分辨率最接近的较大值,其缩小以适合而preserving宽高比整个屏幕,如果事情重叠的尺寸,裁剪完美契合屏幕。

On the server side, I am going to fetch the closest larger value of the given resolution, scale it down to fit the whole screen while preserving aspect ratio, and if something overlaps the dimensions, crop it to perfectly fit the screen.

虽然一切都是pretty用简单的缩放和裁切,我不能想办法,找出最接近的较大值,加载参考图像。

While everything is pretty simple with scaling and cropping, I cannot think of a way to find out the closest larger value, to load the reference image.

在情况下,它可以帮助, $决议 $针可以在不同的格式,例如:阵列('宽'=> X,高度= GT; Y)。

In case it helps, $resolutions and $needle can be in a different format, ie.: array('width' => x, 'height' => y).

我试着用Levenshtein距离实验: HTTP://$c$cpad.viper- 7.com/e8JGOw 结果
显然,它只是工作的 768x1280 ,并导致 800x1280 。对于 320×240 它导致了 480×640 但是,这并不适合这个时候。

I tried to experiment with levenshtein distance: http://codepad.viper-7.com/e8JGOw
Apparently, it worked only for 768x1280 and resulted 800x1280. For 320x240 it resulted in 480x640 but that does not fit this time.

推荐答案

试试这个

echo getClosestRes('500x960');
echo '<br /> try too large to match: '.getClosestRes('50000x960');

function getClosestRes($res){
    $screens = array(
        'landscape'=>array(
            '640x480',
            '1200x800'
        ),
        'portrait'=>array(
            '480x640',
            '480x800',
            '640x960',
            '800x1280',
            '1536x2048'
        )
    );

    list($x,$y)=explode('x',$res);
    $use=($x>$y?'landscape':'portrait');

    // if exact match exists return original
    if (array_search($res, $screens[$use])) return $res; 

    foreach ($screens[$use] as $screen){
        $s=explode('x',$screen);
        if ($s[0]>=$x && $s[1]>=$y) return $screen;
    }
    // just return largest if it gets this far.
    return $screen; // last one set to $screen is largest
}

这篇关于寻找在分辨率数组与最近的纵横比最接近的较大分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆