转换图片
<?phpif(isset($_POST['submit'])){
$extensions = array("jpeg","jpg","png");
$files = $_FILES['file'];
$file_count = count($files['name']);
for($i=0; $i<$file_count; $i++){
$file_name = $files['name'][$i];
$file_tmp = $files['tmp_name'][$i];
$file_ext = strtolower(end(explode('.',$file_name)));
if(in_array($file_ext,$extensions)=== false){
echo "只能上传jpeg、jpg或png格式的图片";
}else{
$new_file_name = uniqid('', true) . '.' . $file_ext;
$upload_path = "uploads/" . $new_file_name;
if(move_uploaded_file($file_tmp, $upload_path)){
if($file_ext == "webp"){
$img = imagecreatefromwebp($upload_path);
imagejpeg($img, "uploads/" . substr($new_file_name, 0, -4) . ".jpg", 100);
imagedestroy($img);
echo "转换成功!";
}else{
echo "上传成功!";
}
}else{
echo "上传失败!";
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>图片上传下载</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file[]" multiple />
<input type="submit" name="submit" value="上传" />
</form>
<?php
$dir = "uploads/";
if(is_dir($dir)){
if($dh = opendir($dir)){
while(($file = readdir($dh)) !== false){
if($file != "." && $file != ".."){
echo "<a href='uploads/$file' download>$file</a><br>";
}
}
closedir($dh);
}
}
?>
</body>
</html>
页:
[1]