【发布时间】:2021-11-08 17:41:09
【问题描述】:
我知道我在哪里遇到了大问题,但我似乎无法解决它们。我正在尝试通过 Ajax 使用 PHP 旋转图像。我只想在每次按下按钮时将图像旋转 90 度。有人可以帮忙吗。我知道我正在告诉我的 PHP 脚本检索 $_POST['image'] 变量,但我没有发送一个,这就是我收到 php 错误的原因。我知道 php 脚本有效。我不知道我是否可以在我的脚本中使用 xhttp.open("GET") 。我知道我的 html 和重新加载标签中的图像还有其他问题。非常感谢任何帮助。
这是我的 JS:
<script>
function rotateImage(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("rotate").innerHTML = this.responseText;
}
};
xhttp.open("POST", "rotate.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xhttp.send("<?php echo $fileName; ?>");
}
</script>
这是我的 PHP:
<?php
$image = $_POST['image'];
$degree = 90;
$source = imagecreatefromjpeg($image);
// Rotate
$rotate = imagerotate($source, $degree, 0);
$image = imagejpeg($rotate, realpath($image), 100);
//echo $image;
imagedestroy($source);
imagedestroy($rotate);
?>
还有我的 HTML:
<div class="row justify-content-center mb-3">
<div class="col-lg col-auto">
<?php list($width, $height) = getimagesize($fileName); ?>
<div class="mb-1" id="rotate"><img class="rounded img-fluid" src="<?php echo $fileName; ?>" alt="Your Profile Photo" width="<?php echo $width; ?>" height="<?php echo $height; ?>" /></div>
<div class="text-center"><button onClick="rotateImage()" class="btn btn-outline-primary btn-sm"><i class="fas fa-sync-alt"></i> Rotate</button></div>
</div>
【问题讨论】:
-
如果不了解预期的策略就很难说。试着问自己两个问题 (1) PHP 脚本返回给客户端的是什么? (2)
... .innerHTML = this.responseText是正确的做法吗? -
好吧,我想出了这部分:xhttp.send("image="+);
-
只需要重新加载页面上的图像,而不需要重新加载页面以反映旋转后的图像。
标签: php jquery ajax xmlhttprequest rotation