我正在尝试在上传多个图像时生成随机名称。我在上传单个图像时已经这样做了,并且图像上传后图像大小也被压缩。我在上传多张图片时尝试做同样的事情。
这是我上传多张图片但名称相同的代码,
$birtimage=$_FILES['birtimage']['name'];
$birtimage = implode(",",$birtimage);
$pic = "upload/";
for($i=0;$i<count($_FILES['birtimage']['name']);$i++){
$target_file = $pic.basename($_FILES["birtimage"]["name"][$i]);
$imageFiletype= pathinfo($target_file,PATHINFO_EXTENSION);
if(move_uploaded_file($_FILES["birtimage"]["tmp_name"][$i], $target_file))
{
$msg="The file has been successfully uploaded";
}
else{
$msg="not found image";
}
}
这是我的代码,非常适合单个图像,
$extension = pathinfo($_FILES['bday_banner'] ['name'], PATHINFO_EXTENSION);
$bday_banner = rand(10000,99999) . '.' . $extension;
$location = "assets/birthday/banner/".$bday_banner;
if(in_array(strtolower($extension), [ 'png', 'jpeg', 'jpg', ])){
compressImage($_FILES['bday_banner']['tmp_name'],$location,60);
}else{
echo "Invalid file type.";
}
这里还有在上传单个图像时压缩我的图像的功能,
function compressImage($source, $destination, $quality) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
imagejpeg($image, $destination, $quality);
}
我试过这个:
$extension = pathinfo($_FILES['birtimage'] ['name'], PATHINFO_EXTENSION);
$birtimage = rand(10000,99999) . '.' . $extension;
$birtimage = implode(",",$birtimage);
$location = "upload/".$birtimage;
for($i=0;$i<count($_FILES['birtimage']['name']);$i++){
if(in_array(strtolower($extension), [ 'png', 'jpeg', 'jpg', ])){
compressImage($_FILES['birtimage']['tmp_name'][$i],$location,60);
}else{
echo "Invalid file type.";
}
}
慕少森
开心每一天1111
繁星点点滴滴