图片转换
<?phpif(isset($_POST['submit'])){
$file_name = $_FILES['file']['name'];
$file_tmp = $_FILES['file']['tmp_name'];
$file_ext = strtolower(end(explode('.',$_FILES['file']['name'])));
$extensions = array("jpeg","jpg","png");
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" />
<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]