我和一所大學正試圖建立一種方法,可以找到圖像中默認顯示的選區的最大尺寸。選擇有一個比例。PHP:在尊重比例的同時找到最大的放大尺寸
到現在爲止,我們已經試過這樣:
public function findBestSizeForRatio($inputW, $inputH, $ratioW, $ratioH)
{
if($inputW/$ratioW > $ratioH/$inputH){
$w = floor($inputW/$ratioW) * $ratioW;
$h = $w * $ratioH/$ratioW;
}
else{
$h = floor($inputH/$ratioH) * $ratioH;
$w = $h * $ratioW/$ratioH;
}
return array($w, $h);
}
但它失敗我們的單元測試。
我們也嘗試過這樣的:
public function findBestSizeForRatio($inputW, $inputH, $ratioW, $ratioH)
{
return $this->findBestSizeInBox($ratioW*10000, $ratioH*10000, $inputW, $inputH);
}
public function findBestSizeInBox($inputW, $inputH, $boxW, $boxH)
{
if($inputW/$boxW > $inputH/$boxH){
return array($boxW, round($inputH * $boxW/$inputW));
}
else{
return array(round($inputW * $boxH/$inputH), $boxH);
}
}
它似乎工作,但它不是像素完美。由於我們的比例是由少數人組成的,我們需要一種真正尊重比例的方法,即使圖像不是100%填充。硬編碼常數(如* 10000)也很愚蠢,因爲我們無法找出一個好的數學公式。 ;)
我們還建立這些PHPUnit測試,這似乎是代表我們(因此,我們可能會忘記某些情況下)
/**
* @covers Img_GD::findBestSizeInBox
*/
public function testfindBestSizeForRatioReturnValidValueForEasyInput()
{
$img = new Img_GD();
//1
$iW = 400; $iH = 300; //Input image size
$rW = 4; $rH = 3; //ratio
$eW = 400; $eH = 300; //Expected
$this->assertEquals(array($eW, $eH), $img->findBestSizeForRatio($iW, $iH, $rW, $rH), "Img: {$iW} x {$iH} Ratio: {$rW} x {$rH}");
//2
$iW = 400; $iH = 3000; //Input image size
$rW = 4; $rH = 3; //ratio
$eW = 400; $eH = 300; //Expected
$this->assertEquals(array($eW, $eH), $img->findBestSizeForRatio($iW, $iH, $rW, $rH), "Img: {$iW} x {$iH} Ratio: {$rW} x {$rH}");
//3
$iW = 4000; $iH = 300; //Input image size
$rW = 4; $rH = 3; //ratio.
$eW = 400; $eH = 300; //Expected
$this->assertEquals(array($eW, $eH), $img->findBestSizeForRatio($iW, $iH, $rW, $rH), "Img: {$iW} x {$iH} Ratio: {$rW} x {$rH}");
//4
$iW = 400; $iH = 3000; //Input image size
$rW = 3; $rH = 4; //ratio
$eW = 399; $eH = 532; //Expected
$this->assertEquals(array($eW, $eH), $img->findBestSizeForRatio($iW, $iH, $rW, $rH), "Img: {$iW} x {$iH} Ratio: {$rW} x {$rH}");
//5
$iW = 4000; $iH = 300; //Input image size
$rW = 3; $rH = 4; //ratio.
$eW = 225; $eH = 300; //Expected
$this->assertEquals(array($eW, $eH), $img->findBestSizeForRatio($iW, $iH, $rW, $rH), "Img: {$iW} x {$iH} Ratio: {$rW} x {$rH}");
//6
$iW = 4000; $iH = 300; //Input image size
$rW = 3; $rH = 4; //ratio.
$eW = 225; $eH = 300; //Expected
$this->assertEquals(array($eW, $eH), $img->findBestSizeForRatio($iW, $iH, $rW, $rH), "Img: {$iW} x {$iH} Ratio: {$rW} x {$rH}");
}
/**
* @covers Img_GD::findBestSizeInBox
*/
public function testfindBestSizeForRatioReturnValidValueForNonExactInput()
{
$img = new Img_GD();
//7
$iW = 403; $iH = 302; //Input image size
$rW = 4; $rH = 3; //ratio
$eW = 400; $eH = 300; //Expected
$this->assertEquals(array($eW, $eH), $img->findBestSizeForRatio($iW, $iH, $rW, $rH), "Img: {$iW} x {$iH} Ratio: {$rW} x {$rH}");
//8
$iW = 403; $iH = 3000; //Input image size
$rW = 4; $rH = 3; //ratio
$eW = 400; $eH = 300; //Expected
$this->assertEquals(array($eW, $eH), $img->findBestSizeForRatio($iW, $iH, $rW, $rH), "Img: {$iW} x {$iH} Ratio: {$rW} x {$rH}");
//9
$iW = 4000; $iH = 302; //Input image size
$rW = 4; $rH = 3; //ratio.
$eW = 400; $eH = 300; //Expected
$this->assertEquals(array($eW, $eH), $img->findBestSizeForRatio($iW, $iH, $rW, $rH), "Img: {$iW} x {$iH} Ratio: {$rW} x {$rH}");
//10
$iW = 403; $iH = 3000; //Input image size
$rW = 3; $rH = 4; //ratio
$eW = 402; $eH = 536; //Expected
$this->assertEquals(array($eW, $eH), $img->findBestSizeForRatio($iW, $iH, $rW, $rH), "Img: {$iW} x {$iH} Ratio: {$rW} x {$rH}");
//11
$iW = 4000; $iH = 302; //Input image size
$rW = 3; $rH = 4; //ratio.
$eW = 225; $eH = 300; //Expected
$this->assertEquals(array($eW, $eH), $img->findBestSizeForRatio($iW, $iH, $rW, $rH), "Img: {$iW} x {$iH} Ratio: {$rW} x {$rH}");
//12
$iW = 4000; $iH = 302; //Input image size
$rW = 3; $rH = 4; //ratio.
$eW = 225; $eH = 300; //Expected
$this->assertEquals(array($eW, $eH), $img->findBestSizeForRatio($iW, $iH, $rW, $rH), "Img: {$iW} x {$iH} Ratio: {$rW} x {$rH}");
}
那會通過了所有測試的任何解決方案?
如果您的計算精度與您有關,您可能需要查看http://php.net/manual/en/book.bc.php或http://php.net/manual/en/ book.gmp.php – Mikk 2011-05-11 23:04:40
@Mikk這不是我需要精確度像浮點精度。這是我不想四捨五入。如果看看測試#4。我不想要400 x 533.333,我想要399 x 532,因爲它不可能保持400的理想寬度的比例。我只想要在我的結果中的整數,總是,而不是四捨五入。 – FMaz008 2011-05-12 00:01:30