使用Ajax的XMLHttpRequest向服务器发送POST请求,将数据传递给PHP脚本,然后旋转图像。
代码示例:
HTML:
JS:
const fileInput = document.getElementById("fileInput");
const rotateBtn = document.getElementById("rotateBtn");
const image = document.getElementById("image");
let rotationAngle = 0;
fileInput.addEventListener("change", () => {
const file = fileInput.files[0];
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
image.src = reader.result;
};
});
rotateBtn.addEventListener("click", () => {
rotationAngle += 90;
rotateImage(rotationAngle);
const data = new FormData();
data.append("imageData", image.src);
data.append("angle", rotationAngle);
const xhr = new XMLHttpRequest();
xhr.open("POST", "rotate-image.php");
xhr.onload = () => {
image.src = xhr.responseText;
};
xhr.send(data);
});
function rotateImage(angle) {
image.style.transform = `rotate(${angle}deg)`;
}
PHP: