做了一个快速的类。应该能够找到您指定的任何两个数字的最小分辨率。我已经用您指定的分辨率预加载了它,但$_resolutions
阵列可以设置为您喜欢的任何标准,也可以即时更改。
class Resolution {
/**
* Standard resolutions
*
* Ordered by smallest to largest width, followed by height.
*
* @var array
*/
private $_resolutions = array(
array('480', '640'),
array('480', '800'),
array('640', '480'),
array('640', '960'),
array('800', '1280'),
array('2048', '1536')
);
/**
* Width
*
* @var int
*/
private $_width;
/**
* Height
*
* @var int
*/
private $_height;
/**
* Constructor
*
* @param int $width
* @param int $height
* @return void
*/
public function __construct($width, $height) {
$this->setSize($width, $height);
}
/**
* Find the minimum matched standard resolution
*
* @param bool $revertToLargest (OPTIONAL) If no large enough resolution is found, use the largest available.
* @param bool $matchAspectRatio (OPTIONAL) Attempt to get the closest resolution with the same aspect ratio. If no resolutions have the same aspect ratio, it will simply use the minimum available size.
* @return array The matched resolution width/height as an array. If no large enough resolution is found, FALSE is returned, unless $revertToLargest is set.
*/
public function getMinimumMatch($revertToLargest = false, $matchAspectRatio = true) {
if ($matchAspectRatio) {
$aspect = $this->_width/$this->_height;
foreach ($this->_resolutions as $res) {
if ($res[0]/$res[1] == $aspect) {
if ($this->_width > $res[0] || $this->_height > $res[1]) {
return ($revertToLargest ? $res : false);
}
return $res;
}
}
}
foreach ($this->_resolutions as $i => $res) {
if ($this->_width <= $res[0]) {
$total = count($this->_resolutions);
for ($j = $i; $j < $total; $j++) {
if ($this->_height <= $this->_resolutions[$j][1]) {
return $this->_resolutions[$j];
}
}
}
}
return ($revertToLargest ? end($this->_resolutions) : false);
}
/**
* Get the resolution
*
* @return array The resolution width/height as an array
*/
public function getSize() {
return array($this->_width, $this->_height);
}
/**
* Set the resolution
*
* @param int $width
* @param int $height
* @return array The new resolution width/height as an array
*/
public function setSize($width, $height) {
$this->_width = abs(intval($width));
$this->_height = abs(intval($height));
return $this->getSize();
}
/**
* Get the standard resolutions
*
* @return array
*/
public function getStandardResolutions() {
return $this->_resolutions;
}
/**
* Set the standard resolution values
*
* @param array An array of resolution width/heights as sub-arrays
* @return array
*/
public function setStandardResolutions(array $resolutions) {
$this->_resolutions = $resolutions;
return $this->_resolutions;
}
}
示例用法
$screen = new Resolution(320, 240);
$screen->getMinimumMatch();
// Returns 640 x 480 (aspect ratio matched)
$screen = new Resolution(1280, 960);
$screen->getMinimumMatch();
// Returns 640 x 480 (aspect ratio matched)
$screen = new Resolution(400, 960);
$screen->getMinimumMatch();
// Returns 640 x 960 (aspect ratio not matched, so uses closest fit)
$screen = new Resolution(5000, 5000);
$screen->getMinimumMatch();
// Returns FALSE (aspect ratio not matched and resolution too large)
$screen = new Resolution(5000, 5000);
$screen->getMinimumMatch(true);
// Returns 2048 x 1536 (aspect ratio not matched and resolution too large, so uses largest available)