0

I wrote this short function which I trigger from an email link which notifies me whenever a new photo has been uploaded to my server:

$fotofil = $_GET['fotofil'];

$image = imagecreatefromjpeg("media/$fotofil");
$image = imagerotate($image, 270, 0);
imagejpeg($image, "media/$fotofil");

echo "The photo has been rotated:<br />";
echo "<img src='media/$fotofil' style='max-height:90vh;' />";

The function rotates the image 90 degrees clockwise and saves it with the original filename. For some reason, this only works once. If I trigger the same link once more, it keeps the rotation from the first time it was triggered. Why?

aanders77
  • 620
  • 8
  • 22
  • Is it just a case of browser caching? The filename stays the same, so depending on cache headers/etc, your browser might not be requesting the new version. Try a hard refresh (Ctrl + F5, or similar) – iainn Apr 19 '20 at 15:00
  • Open inspector in browser and right click the refresh button and choose hard refresh. – dale landry Apr 19 '20 at 15:01
  • 1
    Disable the browser cache. Or append some random value after the src tag, so it changes for every call. – Markus Zeller Apr 19 '20 at 16:01
  • @Markus, right! That's probably it, I'll add some random value and see how it works. – aanders77 Apr 19 '20 at 20:03
  • Btw, good point that it needs to be in the src tag, and not in the request url. I didn't think of that. – aanders77 Apr 19 '20 at 20:20
  • @Markus, you were right, thanks! Make an answer of your comment, and I'll accept it :) – aanders77 Apr 20 '20 at 04:36

1 Answers1

1

You can disable the cache of the browser or trick the cache by adding a random number to the source. With that, the URL changes for every request and the browser will not find it.

$rand = time() . rand();
echo "<img src='media/{$fotofil}?r={$rand}' style='max-height:90vh;' />";
Markus Zeller
  • 8,516
  • 2
  • 29
  • 35