php - 如何使用php自动调整上传图片的大小

标签 php file-upload autoresize

我有一个脚本;

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

    }

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

最佳答案

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

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

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

关于php - 如何使用php自动调整上传图片的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10130915/

相关文章:

php - 使用 PHP 打开或创建文件时权限被拒绝

php - Zend Studio 中 PHP 扩展的代码完成?

php - mysql如何进行多表操作?

python - FTP 库错误 : got more than 8192 bytes

php - 通过从mysql数据库获取图像来显示php文件中的一些图像

c++ - 使用 libcurl 上传文件

javascript - 如何使我的 HTTP 请求与表单的行为相同

java - 在 JavaFX 中调整窗口大小时如何调整图像大小

iphone - 如何设置 iPhone 5 的边框尺寸

css - 模态动态调整大小,但其他 div 不随之移动