Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.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
Javascript 上传时调整图像大小,然后移动上传文件功能_Javascript_Php_Image_File_Upload - Fatal编程技术网

Javascript 上传时调整图像大小,然后移动上传文件功能

Javascript 上传时调整图像大小,然后移动上传文件功能,javascript,php,image,file,upload,Javascript,Php,Image,File,Upload,在我将文件发送到移动上传的文件()函数之前,是否有办法调整$\u文件[“xfile1”][“tmp\u name”]的大小?我检查了所有地方,但似乎没有人在调整图像大小后使用此功能 // Create image from file switch(strtolower($_FILES['xfile1']['type'])) { case 'image/jpeg': $image = imagecreatefromjpeg($_FILES['xfile1']['tmp_na

在我将
文件发送到
移动上传的文件()函数之前,是否有办法调整
$\u文件[“xfile1”][“tmp\u 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\u uploaded\u file()函数中用
$\u FILES[“xfile1”][“tmp\u name”]
替换它。可能吗?
提前谢谢。

你不应该换一种方式吗?从是否上载文件()开始?如果是这样,从tmp收缩。删除tmp。无需移动上传的文件即可保存到任何位置。
imagejpeg($new,'saved.jpeg',90)
将实际在磁盘上创建副本,之后实际上不需要使用
移动上传的文件()。你可以检查一下
是否上传了文件()
,以防万一。
移动上传的文件()
是一个沙盒函数。出于安全考虑,除非你调用这个函数,否则你无论如何都无法访问上传的文件(你可以在你的tmp路径中搜索,但那是另一回事)