图片改变
<?phpif(isset($_FILES['images'])) {
$images = $_FILES['images'];
$base64_images = array();
foreach($images['tmp_name'] as $key => $tmp_name) {
$image_data = file_get_contents($tmp_name);
$base64_image = base64_encode($image_data);
$base64_images[] = $base64_image;
}
}
if(isset($_POST['format'])) {
$format = $_POST['format'];
$images = array();
foreach($_POST['images'] as $base64_image) {
$image_data = base64_decode($base64_image);
$image = imagecreatefromstring($image_data);
if($format == 'jpeg') {
$filename = uniqid() . '.jpg';
imagejpeg($image, $filename);
} else {
$filename = uniqid() . '.png';
imagepng($image, $filename);
}
imagedestroy($image);
$images[] = $filename;
}
}
if(isset($_POST['save'])) {
$db_host = 'localhost';
$db_user = 'root';
$db_password = 'password';
$db_name = 'database';
$conn = mysqli_connect($db_host, $db_user, $db_password, $db_name);
if(!$conn) {
die('连接数据库失败:' . mysqli_connect_error());
}
$images = $_POST['images'];
$sql = \"INSERT INTO images (base64_image) VALUES\";
foreach($images as $base64_image) {
$sql .= \" ('$base64_image'),\";
}
$sql = rtrim($sql, ',') . ';';
if(mysqli_query($conn, $sql)) {
echo '保存成功!';
} else {
echo '保存失败:' . mysqli_error($conn);
}
}
if(isset($_POST['delete'])) {
$db_host = 'localhost';
$db_user = 'root';
$db_password = 'password';
$db_name = 'database';
$conn = mysqli_connect($db_host, $db_user, $db_password, $db_name);
if(!$conn) {
die('连接数据库失败:' . mysqli_connect_error());
}
$sql = \"DELETE FROM images WHERE created_at < DATE_SUB(NOW(), INTERVAL 24 HOUR)\";
if(mysqli_query($conn, $sql)) {
echo '删除成功!';
} else {
echo '删除失败:' . mysqli_error($conn);
}
}
?>
页:
[1]