views:

243

answers:

4

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);
}
+5  A: 

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 and Expires 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.

Andrew Moore
A: 

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.

Andrejs Cainikovs
A: 

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.

Richy C.
A: 

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

Jacob Wyke