2011-05-25 52 views
1

我是jQuery的新手,但我喜歡它!我有一個問題,我不能得到回合。jQuery在保存前調整圖像大小

我使用http://www.zurb.com/playground/ajax_upload

這我有使用工作,則下列upload.php的

<? 
$time= time(); 
$uploaddir = 'users/'; //<-- Changed this to my directory for storing images 
$uploadfile = $uploaddir.$time.basename($_FILES['userfile']['name']); //<-- IMPORTANT 

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
    echo $uploaddir.$time.$_FILES['userfile']['name']; // IMPORTANT 
    #print_r($_FILES); 
} else { 
    // WARNING! DO NOT USE "FALSE" STRING AS A RESPONSE! 
    // Otherwise onSubmit event will not be fired 
    echo "error"; 
} 
?> 

我已經添加了時間變量,以確保每個圖像是獨一無二的。我遇到的問題是我想調整大小並優化圖像,我不知道如何做到這一點。

調整大小是我需要的最重要的特徵 - 例如,我希望最大寬度爲300px,即使原始寬度爲1000px,也會保存圖像。我需要調整比例(這是一個字?:))

任何幫助將是偉大的。

問候 中號

回答

2

要調整,你需要像GD

庫的標準功能做,這是GD的imagecopyresampled圖像。

example顯示重新調整的一種方式,並保持比例:

//> MAx 
$width = 200; 
$height = 200; 

// Get new dimensions 
list($width_orig, $height_orig) = getimagesize($filename); 

$ratio_orig = $width_orig/$height_orig; 

if ($width/$height > $ratio_orig) { 
    $width = $height*$ratio_orig; 
} else { 
    $height = $width/$ratio_orig; 
} 
+0

謝謝我在服務器上安裝了GD,並且相當容易實現,再次感謝。 – madmatuk 2011-05-27 15:56:53

0

PHP中,GD或ImageMagick的兩個主要的圖像處理事情。兩者都可以做你需要的。您將需要在您的PHP網絡服務器上配置它們。