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路径中搜索,但那是另一回事)