Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php自动调整上传图像的大小_Php_File Upload_Autoresize - Fatal编程技术网

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

如何使用php自动调整上传图像的大小,php,file-upload,autoresize,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

我有剧本

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

    }

这是我用来上传图像,但我想添加一个脚本,以调整图像大小到一个特定的大小之前,它的上传。我该怎么做呢?

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


还有一个将向您显示一个用于调整大小的自定义类,但除非您需要整个类,否则您可以将注意力集中在函数resize上,以查看它是如何完成的编辑:我已将其更新为包含脚本元素。我从获取文件名的点开始

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

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

这对你来说很容易。。。。我会确保文件名是唯一的,虽然可能性低得离谱,但不是唯一的impossible@Baba谢谢,但太复杂了!英雄联盟有没有更简单的方法??@gosukiwi你指的是我的脚本还是Baba的?你的,md5可以将多个字符串路径到同一个散列,我知道几率低得离谱,但我不知道,考虑到这些小东西是让程序更健壮的原因@davidethell看起来不错,但我如何将其与上面的脚本融合呢???如果能做到这一点,那就太好了@JaySmoke,我已经更新了这个示例,以包含$filePath变量。很好,但是脚本有一个错误。$img_source从哪里获取数据???谢谢,我修复了这个示例。我错过了那个。我同意。但是服务器应该同时为多个用户服务。尽快给别人留下更多的记忆总是更好的