Skip to content

Instantly share code, notes, and snippets.

@zhoumengkang
Created January 19, 2015 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhoumengkang/ea07e13afe806634f31d to your computer and use it in GitHub Desktop.
Save zhoumengkang/ea07e13afe806634f31d to your computer and use it in GitHub Desktop.
尺寸匹配
// 先判定宽高比,再判定宽高
$sizeArr = explode("x", $p['screen']);
$screenWidth = $sizeArr['0'];
$screenHeight = $sizeArr['1'];
if ($screenHeight)
$wh = $screenWidth / $screenHeight;
$tmpArr = array();
// 根据 宽高比最小差值,宽最小差值,高最小差值 进行排序,顶部为最合适的尺寸
foreach ($splashArr as $info) {
$tmp = abs($wh - $info['icon_width'] / $info['icon_height']) * 1000000;
$tmp += abs($screenWidth - $info['icon_width']);
$tmp += abs($screenHeight - $info['icon_height']);
$tmpArr[$tmp] = $info;
}
ksort($tmpArr);
// 最合适的尺寸列表,在前三条记录里,需要按宽再进行一次判定,防止出现上面宽的差值与高的差值相反的情况
$cou = 0;
$tmp = null;
$useScreen = array();
foreach ($tmpArr as $key => $info) {
if (++$cou > 3 ) {
break;
}
if (is_null($tmp) || $tmp > abs($screenWidth - $info['icon_width'])) {
$tmp = abs($screenWidth - $info['icon_width']);
$useScreen = $info;
}
}
$splashArr = $useScreen;
$op['splash']['url'] = $splashArr['icon_url'];
$op['splash']['start_ts'] = strtotime($splashArr['start_ts']);
$op['splash']['end_ts'] = strtotime($splashArr['end_ts']);
$op['splash']['display_time'] = $splashArr['display_time'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment