2013-07-29 29 views
0

基本上我試圖做的是爲我的個人網站創建一個封面,就像Facebook一樣。基本上,我的封面佈局與Facebook上的佈局相同,因此用戶可以在我的網站以及臉書上使用相同的封面時獲得相同的結果。如何調整像Facebook封面的圖像大小

我被卡住的部分是「拖動圖像定位封面」的東西。 Facebook使用一些算法來將封面圖片尺寸轉換爲拖動東西時不同的東西。例如,如果原始圖像尺寸是920x720,即在Facebook設置封面頁面(將圖像拖動到位置覆蓋物)時的相同圖像的尺寸,則圖像的尺寸爲851x638。

我只是想知道哪種算法Facebook的使用設置圖像尺寸(從720到638)

注:封面必須是像素完美

我知道,蓋尺寸臉譜是851x315,所以這裏是我在做什麼:

//$x = X origin cordinate variable obtained by dragging image 
    //$y = Y origin cordinate variable obtained by dragging image 
    list($k, $l) = getimagesize($src); // $src == image source 
    //$w = Needs to be calculated 
    //$h = Needs to be calculated 
    $img_r = imagecreatefromjpeg($src); 
    $dst_r = ImageCreateTrueColor(854,316); 
    imagecopyresampled($dst_r,$img_r,0,0,$x,$y,$w,$h,$k,$l); 
    imagejpeg($dst_r,$src,$jpeg_quality); 
    $img_name = writeToImage($src, $des); //writeToImage() is just a demo function created by me to do some other things with them which do not affect this part of code 

    echo $img_name; 

我需要弄清楚的Facebook如何計算從以前的一個圖像的新維度。它取決於圖像的實際(原始)大小還是取決於其他一些因素?

+0

你寫了851x638。你的意思是815x638? – Herbert

+0

@赫伯特:不,我的意思是851x638。我很確定我在這種情況下寫的像素。所以沒有尺寸/像素錯誤 – ashutosh

+0

jquery拖動事件可能會幫助你。我通過弄亂Jcrop事件來克服這個問題。 [Jcrop概述](http://deepliquid.com/content/Jcrop.html) –

回答

0

縮放圖像的公式非常簡單。

$aspect_ratio = $original_width/$original_height; 

$new_width = $new_height * $aspect_ratio; 

$new_height = $new_width/$aspect_ratio;