我有一个脚本;
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
// get the file extension first
$ext = substr(strrchr($fileName, "."), 1);
// make the random file name
$randName = md5(rand() * time());
// and now we have the unique file name for the upload file
$filePath = $imagesDir . $randName . '.' . $ext;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
if(!get_magic_quotes_gpc()) {
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
用于上传图片,但我想添加一个脚本,在图片上传之前将图片大小调整为特定尺寸。我该怎么办?
答案 0 :(得分:2)
编辑:我已更新此内容以包含您的脚本元素。我从你获得文件名的那一刻开始。
这是一个非常快速,简单的脚本:
$result = move_uploaded_file($tmpName, $filePath);
$orig_image = imagecreatefromjpeg($filePath);
$image_info = getimagesize($filePath);
$width_orig = $image_info[0]; // current width as found in image file
$height_orig = $image_info[1]; // current height as found in image file
$width = 1024; // new image width
$height = 768; // new image height
$destination_image = imagecreatetruecolor($width, $height);
imagecopyresampled($destination_image, $orig_image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// This will just copy the new image over the original at the same filePath.
imagejpeg($destination_image, $filePath, 100);
答案 1 :(得分:0)
嗯,在上传之前你不能改变它的大小,但你可以使用GD库在服务器上更改它的大小。查看GD and Image Functions listing 以了解处理图像的所有相关功能。
还有this tutorial会显示一个用于调整大小的自定义类,但除非你需要全部认为你可以专注于函数调整大小以查看它是如何完成的