上传前调整图像文件的大小;我们可以覆盖临时文件并上传吗?

Resize the image-file before uploading; Could we overwrite the temp file & upload?

我想在上传图像文件之前调整它的大小。我的想法是,调整 'temp' 文件的大小并上传它。但是代码给出了错误。下面我附上了我的代码和评论。

<input type="file" name="file" id="file" required />  <!-- HTML Front-end through which the file would be uploaded-->

php 代码开始...

$fileName = $_FILES['file']['tmp_name']; //get the uploaded file's name
$ext = strtolower(substr(strrchr(fileName, '.'), 1)); //get the file's extension
$tempFile = $_FILES['file']['tmp_name'];  //temporary file's path
$identityNumber="20150816";               //assigns an identity number and...
$targetPath = "uploads/".$identityNumber.'.'.$ext;  //rename file & upload it

$image_properties = getimagesize($tempFile);
$image_width = $image_properties[0];
$image_height = $image_properties[1];
$percent = 0.5; //Percentage by which the image is going to be resized

$newWidth = $image_width * $percent;
$newHeight = $image_height * $percent;

if ($ext == "jpeg" || $ext == "jpg") {
    header('Content-type: image/jpeg');
    $thumb = imagecreatefromjpeg($tempFile);
} elseif ($ext == "png") {
    header('Content-type: image/png');
    $thumb = imagecreatefrompng($tempFile);
}

$modifiedFile = imagecreatetruecolor($new_width, $new_height);

imagecopyresampled(
    $modifiedFile,
    $thumb, 
    0, 
    0,
    0, 
    0,
    $new_width, 
    $new_height, 
    $width,
    $height
);

这是发出警告的代码,如下所述... move_uploaded_file($modifiedFile,$targetPath);

警告:move_uploaded_file() 要求参数 1 为字符串,资源在...

还有 'echo is'

Resource id #10

为什么函数 move_uploaded_file 给出警告..?谁能帮助实现它?

我知道有几种方法可以调整图像大小,例如 "GD"、"ImageMajick" 等。但我只喜欢上面的代码...

http://us3.php.net/manual/en/function.imagejpeg.php

move_uploaded_file() 文件用于将上传的文件从临时文件夹移动到目标文件夹。它像复制一样工作。所以它希望两个参数都是字符串。

在您的情况下,您正在创建一个全新的图像文件,因此您需要使用 imageXXX() 函数将图像写入磁盘。对于 jpeg 图像你调用 imagejpeg() 为 png 你使用 imagepng() 等

您可以使用 imagepng 函数保存文件,与 move_uploaded_file 相反,它接受资源参数。 imagejpeg 功能也存在。

imagepng ($modifiedFile, $targetPath);