1

how can I force the browsers to refresh an image which is created with imagecreatefromjpeg?

I know this SO question (PHP force refresh image) for if I have a real image, but I need to refresh the image I just created because regarding to the user actions (to highlight different positions on the image) the image is not refreshed probably and I have to manually force the browser to reaload it via STRG + F5

For example:

  • Precondition: Browser has nothing in cache
  • I login into account of userA and open the page in which the image is included and highlight some points on it -> image is created correctly
  • I logout and login into userB and open the page in which the image is included and highlight some points on it -> I see the old image from userA
  • I have to hit STRG + F5 to see the correct image of mine
  • Conclusuion: Browser caches the image instead of recreating it

Mainpage (which is opend by user) contains this part:

<img src="http://<URL>/<dir>/draw.php">

The draw.php file creates the image via:

ob_start();

// get $results (= points to highlight) from database - works fine

$img = draw_stars_into_small_starmap($results);

ob_end_clean();

header("Content-type: image/jpeg");
imagejpeg($img, null, 85);
imagedestroy($img);

And this functions highlights some points on the image

// $smallimg is the image from 
// Results containing the points to highlight

function draw_stars_into_small_starmap($results) {
  $smallimg = dirname(__FILE__) . "/nameofpicture.jpg";

  // Which color for highlighting
  $colormap = array(
      'a' => array(240, 120, 120),
      'o' => array(255, 128, 0),
      'm' => array(0, 255, 0)
    );

  $width = 450;
  $height = 100;

  while ($row = od_mysql_fetch_array ($results)) {
    $x = $row['xkoord'] / (610 / $width);
    $z = $row['zkoord'] / (610 / $height);
    $y = $row['ykoord'] / 5;

    list($r, $g, $b) = $colormap[$row['type']];
    $color = imagecolorallocate($smallimg, $r, $g, $b);

    $x = floor($x);
    $y = floor($y) + 50;

    imagearc($smallimg, $x, $y, 9, 9, 0, 270, $color);
    imagecolordeallocate($smallimg, $color);
  }

  return $smallimg;
}

Anyone has an idea?

P.S. I ONLY want to always force the browser to refresh this particular image (or the draw.php the image is placed in) but NOT all images on the website / server!

Community
  • 1
  • 1
bish
  • 3,381
  • 9
  • 48
  • 69
  • possible duplicate of [Making sure a web page is not cached, across all browsers](http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers) – Jeremiah Winsley Jul 30 '15 at 17:12
  • The first link you posted on your question is the correct method of doing this, it doesn't matter what the file is that the random number is attached to, it will still recall it from the browser because the contents of the `src` string is different. – Martin Jul 30 '15 at 17:28

1 Answers1

1

What you do is add a random number generator to the image, you want to be refreshed:

<?php
$rand = mt_rand(11111111,99999999);
?>
<img src="http://<URL>/<dir>/draw.php?<?php print $rand;?>">

This will force the HTML document to refresh the browser address because the string of the image it is calling, has changed due to the random number.

You should also possibly use:

  • PHP clearstatcache(); in your <img> containing page at the top of the page so that every time the page loads it clears its file cache. This would be used if the actual content of the image draw.php is not refreshing properly.

  • There are other various ways (.htaccess, headers, etc.) to also set your server up to force browsers not to cache image files in general (although this could increase your bandwidth significantly)

Martin
  • 22,212
  • 11
  • 70
  • 132
  • Ah I tried that during my testing to fix the problem but I obviously took the wrong file as I added the random value to the basis file which crashed the script - sometimes you don't see the forest because of trees (translation of a german saying. – bish Jul 30 '15 at 18:47
  • that is often the way, and in English there is a very similar saying, you "can't see the wood for the trees". @bish – Martin Jul 30 '15 at 18:48
  • That's probably the right translation ^^ Gratz zu 2k rep. And now we should stop writing offtopic – bish Jul 30 '15 at 18:51