图片转化
<!DOCTYPE html><html>
<head>
<meta charset="UTF-8">
<title>上传图片</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="上传">
</form>
<div id="image-container"></div>
<script>
function showImage(path) {
var container = document.getElementById('image-container');
var img = document.createElement('img');
img.src = path;
container.appendChild(img);
}
<?php
if (isset($_GET['path'])) {
echo 'showImage("' . $_GET['path'] . '");';
}
?>
</script>
</body>
</html>
…………
<?php
// 获取上传的文件
$file = $_FILES['file'];
// 判断上传是否成功
if ($file['error'] !== UPLOAD_ERR_OK) {
die('上传失败');
}
// 判断文件类型是否为webp
if ($file['type'] !== 'image/webp') {
die('文件类型不正确');
}
// 生成新的文件名
$newFileName = uniqid() . '.jpg';
// 转换图片格式并保存到新的文件夹
$sourceImg = imagecreatefromwebp($file['tmp_name']);
imagejpeg($sourceImg, 'img2/' . $newFileName);
// 保存图片路径到数据库
$pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
$stmt = $pdo->prepare('INSERT INTO images (path) VALUES (:path)');
$stmt->execute(['path' => 'img2/' . $newFileName]);
// 跳转到HTML页面并显示图片
header('Location: index.html?path=img2/' . $newFileName);
exit;
页:
[1]