2

我有一个脚本;

        $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);

    }

用于上传图像,但我想添加一个脚本以在上传之前将图像调整为特定大小。我怎么做???

4

2 回答 2

4

编辑:我已更新此内容以包含您的脚本元素。我从您获得文件名的地方开始。

这是一个非常快速、简单的脚本:

$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);
于 2012-04-13T10:34:35.233 回答
0

好吧,在它上传之前你不能改变它的大小,但是你可以在它在服务器上之后使用 GD 库来改变它的大小。查看GD 和图像函数列表,了解处理图像的所有相关函数。

还有本教程将向您展示一个用于调整大小的自定义类,但除非您需要整体认为您可以专注于函数调整大小以查看它是如何完成的

于 2012-04-12T20:10:44.417 回答