views:

23

answers:

1

I have a script that colors the requested country on a world map with GD and PHP. The PHP requests are called with checkboxes. It kind of looks like if you call the PHP scripts too fast then it returns an "Xed out" error image. Is there a way to queue the PHP requests with setTimeout or something else, so a new checking event never fails?

Here is the Javascript called by the onClick events:

    function onBoxClicked(frame, country){
var randomNumber = Math.floor(Math.random()*100000001);
if (document.getElementById(country).checked == true){
window.parent.document.getElementById('world_map').src=(country)+".php?r=" + randomNumber;
}else if (document.getElementById(country).checked == false){
window.parent.document.getElementById('world_map').src=(country)+"_unload.php?r=" + randomNumber;
}
}

Here is a typical country PHP file (I know there is some junk that can be removed):

<?php
session_cache_limiter('nocache');
$cache_limiter = session_cache_limiter();
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");// Date in the past
$ip=$_SERVER['REMOTE_ADDR'];
$oldImageName = 'world_map2_users_copy.png';
$newImageName = $ip.'.'.'world_map2_users_copy.png';
if (file_exists($newImageName)){
$im = imagecreatefrompng($newImageName);
}else{
copy($oldImageName, $newImageName);
$im = imagecreatefrompng($newImageName);
}
$syria_color = imagecolorallocate($im, 0, 158, 96);
imagefill($im, 780, 205, $syria_color);
ImagePNG($im, $newImageName);
ImagePNG($im);
ImageDestroy($im);
?>
A: 

If you do

ImagePNG($im, $newImageName);

there is the possibility of two PHP-script writing to the same file at the same time.

Why are you writing to disk anyway?

Just do:

$im = imagecreatefrompng($oldImageName);
$syria_color = imagecolorallocate($im, 0, 158, 96);
imagefill($im, 780, 205, $syria_color);
ImagePNG($im);
ImageDestroy($im);

The best solution is to generate all image files in advance, so in JavaScript you can just say:

window.parent.document.getElementById('world_map').src=country+".png";

In that case you'll also lose the caching issues.

edwin
Thanks. The purpose of the site is indicating visited countries, so there'll be multiple colorings.Precreating all the combinations of colored countries would produce a huge number of images. I tried to calculate the number I would need, but gave up because my head hurt. Maybe this is the best solution but I don't know how to generate the images programatically. Each image now is about 29KB.So the reason for the double writing is so the actual png file, can be retrieved as a starting point of further coloring by other country scripts.
socrtwo
I understand. Another alternative is creating transparent gifs with in each gif a highlighted country. In HTML and a bit of CSS, you can put them on top of each other and switch them on/off with JavaScript.
edwin