So I have this script that will rotate images located in a directory, which works great. However, it requires a refresh of the page in order to rotate the image. Is there a way I can modify this to have it rotate the image when the image itself is clicked?
Thanks in advance
HTML:
<img src="rotate.php" alt="random image" />
PHP:
<?php
$folder = 'images/';
$exts = 'jpg jpeg png gif';
$files = array(); $i = -1;
if ('' == $folder) $folder = './';
$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) {
if (preg_match('/\.'.$ext.'$/i', $file, $test)) {
$files[] = $file;
++$i;
}
}
}
closedir($handle);
mt_srand((double)microtime()*1000000);
$rand = mt_rand(0, $i);
header('Location: '.$folder.$files[$rand]);
?>
Update: this is very basic but test it out and see if it works;
and in your php add a content header to send the image eg if it is a
gifor png…The idear is to set the
headercontent type and to read and write the file to the output buffer usingreadfile, armed with this you could use ajax as another means to request the image and using thestatusto easly display a loading indicator.If you where to Rewrite the URL you could change the extension of .php for the src or not even have an extention.
http://php.net/manual/en/function.pathinfo.php
using
pathinfo 'extension'you can get the extention and then set theapproprate
Content-type