2015-11-19 76 views
0

有誰知道關聯下列代碼的數學公式嗎?代碼後面的數學公式

// aspect ratio < 
$src_pos = array(0, (($new_size[1] - $thumb_height) * ($src_size[1] /$new_size[1]))/2); 
// aspect ratio > 
$src_pos = array((($new_size[0] - $thumb_width) * ($src_size[0]/$new_size[0]))/2, 0); 

他們是從上傳的圖片創建拇指更寬的腳本中:

//variables 
$src_size = getimagesize($_FILES["file"]["name"]); 
$thumb_width = 250; 
$thumb_height = 200; 
$src_aspect = round(($src_size[0]/$src_size[1]), 1); 
$thumb_aspect = round(($thumb_width/$thumb_height), 1); 

if ($src_aspect < $thumb_aspect){ 
    //higher 
    $new_size = array($thumb_width,($thumb_width/$src_size[0]) * $src_size[1]); 
    $src_pos = array(0, (($new_size[1] - $thumb_height) * ($src_size[1] /$new_size[1]))/2); 
}else if($src_aspect > $thumb_aspect){ 
    //wider 
    $new_size = array(($thumb_width/$src_size[1]) * $src_size[0], $thumb_height); 
    $src_pos = array((($new_size[0] - $thumb_width) * ($src_size[0]/$new_size[0]))/2, 0); 
}else{ 
    //same shape 
    $new_size = array($thumb_width, $thumb_height); 
    $src_pos = array(0, 0); 
} 

if ($new_size[0] < 1) $new_size[0] = 1; 
if ($new_size[1] < 1) $new_size[1] = 1; 


//creation of thumb 
$thumb = imagecreatetruecolor($thumb_width, $thumb_height); 
imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $src_size[0], $src_size[1]); 

我STUDING這個劇本,但我無法理解的兩行代碼後面我在開始寫的邏輯這個問題,所以我想知道他們與哪個數學公式有關。

+0

看起來像PHP所以我添加了TAG來啓用語法高亮如果你有不同的語言改變你正在使用的語言的TAG – Spektre

+0

O對不起,它是PHP –

回答

1

您需要分別查看各個寬高比if部分,然後將它們混合在一起,這從起點開始有點混亂(在您的問題中添加了註釋)。

我看到它像這樣:

// load image 
$src_size = getimagesize($_FILES["file"]["name"]); 
// static fixed resolution for the thumb 
$thumb_width = 250; 
$thumb_height = 200; 
// aspects (if the x or y size is bigger for image and thumb 
$src_aspect = round(($src_size[0]/$src_size[1]), 1); 
$thumb_aspect = round(($thumb_width/$thumb_height), 1); 

// rescale height because the result will not exceeding thumb height in this case 
if ($src_aspect < $thumb_aspect){ 
    $new_size = array 
    (
    $thumb_width,         // thumb width stays as is 
    ($thumb_width/$src_size[0]) * $src_size[1] // thumb height is rescaled by image aspect 
    ); 
    // this just compute the distance to move the thumb after rescaling so it is still centered 
    $src_pos = array(0, (($new_size[1] - $thumb_height) * ($src_size[1] /$new_size[1]))/2); 
} 
// rescale width because the result will not exceeding thumb width in this case 
else if($src_aspect > $thumb_aspect){ 
    $new_size = array 
    (
    ($thumb_width/$src_size[1]) * $src_size[0], // thumb width is rescaled by image aspect 
    $thumb_height         // thumb height stays as is 
    ); 
    // this just compute the distance to move the thumb after rescaling so it is still centered 
    $src_pos = array((($new_size[0] - $thumb_width) * ($src_size[0]/$new_size[0]))/2, 0); 
}else{ 
    //same shape 
    $new_size = array($thumb_width, $thumb_height); 
    $src_pos = array(0, 0); 
} 

if ($new_size[0] < 1) $new_size[0] = 1; 
if ($new_size[1] < 1) $new_size[1] = 1; 


//creation of thumb 
$thumb = imagecreatetruecolor($thumb_width, $thumb_height); 
imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $src_size[0], $src_size[1]); 

我希望評論是夠你......但可以肯定,我們看一下:

  • ($thumb_width/$src_size[0]) * $src_size[1]

我會用這個代替(所以只需要整數數學)

  • ($thumb_width * $src_size[1])/$src_size[0]

所以$thumb_width是拇指指甲目標最大分辨率和$src_size[1]/$src_size[0]是圖像寬高比常數小於1.0。當您更仔細地觀察整條線時,它是無偏移的簡單線性插值(起始點爲(0,0)),因此結果將小於縮略圖分辨率(將適合於內部),並仍然與原始圖像寬高比匹配。

數學公式:

  • 讓有2個點(x0,y0)(x1,y1)表示的線的端點。
  • 和一些任意點(x,y)內/上線

所以,如果我們知道線上的點的一個座標,我們可以通過利用三角形相似這樣計算其他:

  • x=x0+(y-y0)*(x1-x0)/(y1-y0)
  • y=y0+(x-x0)*(y1-y0)/(x1-x0)

這就是所謂的線性內插。至於你的情況(x0,y0)=(0,0)那麼等式變成了:

  • x=y*x1/y1
  • y=x*y1/x1

哪個是你的代碼表示......而x,ynew_sizex1,y1src_size

現在位置偏移量

比如你計算這樣的:

  • ($new_size[0] - $thumb_width) * ($src_size[0]/$new_size[0]))/2 這樣的條件是:
  • ($new_size[0] - $thumb_width)之間新的和原有的縮略圖分辨率空白
  • ($new_size[0] - $thumb_width)/2是你需要多少轉移調整縮略圖將其放在原始縮略圖分辨率的中間
  • ($src_size[0]/$new_size[0])只是將此偏移量從縮略圖座標縮放轉換爲圖像座標系

當你把所有的位置放在圖像的外面(所以缺失的數據被填充背景顏色)所以當從這個位置將圖像複製到縮略圖時,縮放圖像的縮放圖像居中(除非我缺少一些東西)

+0

感謝spektre,我正在研究你的回答。 –

+0

如果 X = Y * X1/Y1 Y = X * Y1/X1 這意味着: Y =(($ thumb_width * $ src_size [1])/ $ src_size [0]) X =( $ thumb_height * $ src_size [0])/ $ src_size [1]) 弗里斯特情況下返回 y的samee結果=($ thumb_width/$ src_size [0])* $ src_size [1]) 但第二種情況不會返回相同的結果 x =($ thumb_width/$ src_size [1])* $ src_size [0]) –

+0

在等式中: x = y * x1/y1 y = x * y1/x1 你的意思是說,x,y是thumb_size和x1,y1是src_siz e,對嗎?或者x,y是new_size? –