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

时间:2015-08-16 10:54:14

标签: php image-uploading

我希望在上传之前调整图片文件的大小。我的想法是,调整'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为字符串,资源在......

中给出

'回声是'

  

资源ID#10

为什么函数move_uploaded_file会发出警告..?任何人都可以帮助实现它吗?

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

0 个答案:

没有答案