2011-05-25 55 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网络服务器上配置它们。