上传时调整图像大小然后使用move_uploaded_file()

时间:2017-02-01 03:48:48

标签: php function file upload tmp

在我将它发送到move_uploaded_file()函数之前,有没有办法调整$ _FILES [" xfile1"] [" tmp_name"]的大小?我检查了每个地方,但是在调整图像大小后似乎没有人使用此功能。

添加*

// Create image from file
   switch(strtolower($_FILES['xfile1']['type']))
   {
case 'image/jpeg':
    $image = imagecreatefromjpeg($_FILES['xfile1']['tmp_name']);
    break;
case 'image/png':
    $image = imagecreatefrompng($_FILES['xfile1']['tmp_name']);
    break;
case 'image/gif':
    $image = imagecreatefromgif($_FILES['xfile1']['tmp_name']);
    break;
default:
    exit('Unsupported type: '.$_FILES['xfile1']['type']);
}

 // Delete original file
 @unlink($_FILES['image']['tmp_name']);

 // Target dimensions
 $max_width = 540;
 $max_height = 540;

 // Get current dimensions
 $old_width = imagesx($image);
 $old_height = imagesy($image);

// Calculate the scaling we need to do to fit the image inside our frame
$scale = min($max_width/$old_width, $max_height/$old_height);

// Get the new dimensions
 $new_width = ceil($scale*$old_width);
 $new_height = ceil($scale*$old_height);

 // Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);


// Resample old into new
imagecopyresampled($new, $image, 
    0, 0, 0, 0, 
    $new_width, $new_height, $old_width, $old_height);


 ob_start();
 imagejpeg($new,'saved.jpeg' , 90);
 $data = ob_get_clean();

所以到目前为止我有这个。我有这个$ data变量,我想用move_uploaded_file()函数中的$ _FILES [" xfile1"] [" tmp_name"]替换它。这可能吗?

先谢谢。

0 个答案:

没有答案