Search code examples
phpimagejpegimagecreatefromjpg

PHP function to rotate jpeg only works once


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?


Solution

  • 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;' />";