GD - 旋转图像在 IE 和 Opera 中不起作用
我创建了一个旋转定义图像的函数。 它在 Firefox 中完美运行,但在 IE 和 Opera 中没有任何反应 - 图像被重新加载但不旋转。 有人知道为什么吗? 代码如下:
function rotateImage($direction, $id, $angle) {
$dir = opendir($direction);
if ($img = imagecreatefromjpeg($_SESSION['files'][$id]['large'])) {
$width = imagesx ( $img );
$height = imagesy ( $img );
$rotate = imagerotate($img, $angle, 0);
imagejpeg($rotate, $_SESSION['files'][$id]['large'], 100);
}
else {
echo '<p>Error: image cannot be rotated</p>';
}
closedir($dir);
}
I've created a function that rotates defined image. It works perfect in firefox, but in IE and Opera nothing happens - the image is reloaded but not rotated. Does anybody know why? Here goes the code:
function rotateImage($direction, $id, $angle) {
$dir = opendir($direction);
if ($img = imagecreatefromjpeg($_SESSION['files'][$id]['large'])) {
$width = imagesx ( $img );
$height = imagesy ( $img );
$rotate = imagerotate($img, $angle, 0);
imagejpeg($rotate, $_SESSION['files'][$id]['large'], 100);
}
else {
echo '<p>Error: image cannot be rotated</p>';
}
closedir($dir);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题绝对不在于您使用的浏览器,因为轮换是在服务器端完成的。
您可能遇到缓存问题或用于调用该函数的代码问题。
您是否:
使用 JavaScript 启动重新加载?
您的 JavaScript 代码可能是这里的问题。
发送正确的无缓存标头?
如果没有,您可能会遇到图像缓存在浏览器上的情况,这就是您看不到更改的原因。 发送正确的
Cache-control
和Expires
标头,或者将随机标识符附加到图像 URL (?_=$x
其中$x
=time()
可以正常工作...标头是首选)。发送正确的
Content-type
标头?不发送正确的标头可能会导致某些浏览器出现不稳定的行为。 您可能想尝试使用
header('Content-type: image/jpeg')
仅发送图像数据而无需任何额外字符?
确保除了图像之外不输出任何其他内容。 您的输出流不得包含任何额外字符,包括空格。
The problem is definitely not with the browser you are using as the rotation is done server-side.
You might be running into a caching issue or an issue with the code used to call that function.
Are you:
Using JavaScript to initiate a reload?
Your JavaScript code might be the issue here.
Sending the proper no-cache headers?
If not, you might be running into a situation where the image is cached on the browser, which is why you are not seeing your changes. Either send the proper
Cache-control
andExpires
headers, or append a random identifier to the image url (?_=$x
where$x
=time()
will work fine... Headers are preferred).Sending the proper
Content-type
header?Not sending the proper headers might cause erratic behavior in some browsers. You might want to try using
header('Content-type: image/jpeg')
Sending only the image data without any extra characters?
Make sure you don't output anything else than the image. Your output stream must not have any extra characters, including whitespaces.
尝试点击刷新! 或者清除缓存并重新加载。
这是因为图像保存在浏览器缓存中,浏览器知道它有它,但不知道它已被更改。 技巧之一是使用随机生成的名称将图像保存在服务器端。
Try hit refresh! Or clear cache and reload.
This is because the image is saved in browsers cache, and browser know it has it, but it doesn't know it has been changed. One of the tricks is to save the image on the server side with randomly generated name.
我怀疑您没有为图像发送适当的 Content-Type 标头。 或者,图像可能会稍微损坏(通常是由源代码中 php 标签之前/之后的空格引起的)。 将 Firefox 中的图像保存到硬盘上,在文本编辑器(例如 Editplus)中打开它,并检查它不以空格开头或结尾。
I would suspect you aren't sending an appropriate Content-Type header for the image. Alternatively, the image may be slightly corrupted (commonly caused by spaces before/after the php tags in your source code). Save the image from Firefox on your hard drive, open it in a text editor (such as Editplus) and check it doesn't start or end with a space.
PHP 是服务器端的,因此如果它可以在一种浏览器上运行,则代码可以正常运行,问题在于浏览器。 我假设 IE 和 Opera 正在缓存图像。 如果可能,请设置图像的标题,以便它们不会被缓存:
header("缓存控制:无缓存,必须重新验证"); // HTTP/1.1
header("过期时间:1997 年 7 月 26 日星期六 05:00:00 GMT"); // 过去的日期
PHP is server side, so if it works on one browser the code works fine and the issue lies with the browser. I would assume that IE and opera are caching the image. If possible set the headers for the images so that they don't get cached:
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past