Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6813657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:35:45+00:00 2026-05-26T20:35:45+00:00

So I have this script that will rotate images located in a directory, which

  • 0

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]);
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:35:45+00:00Added an answer on May 26, 2026 at 8:35 pm

    Update: this is very basic but test it out and see if it works;

     <script>
      function changeImage(o){
       o.src = 'http://www.domain.com/testtest.php?' + (new Date()).getTime(); // time to help stop caching
        }
     </script>
     <img src="http://www.domain.com/testtest.php" onclick="changeImage(this)" />
    

    and in your php add a content header to send the image eg if it is a gif or png…

     <?php
    // see link at bottom for getting file extention
    
     switch ($selected_random_file_extention) {
      case "gif":
         header('Content-type: image/gif');
         $file_ext = "gif"; break;
       case "jpg":
         header('Content-type: image/jpg')
         $file_ext = "jpg"; break;
       case 3:
         header('Content-type: image/png');
         $file_ext = "png"; break;
      }
    
      //header('Content-type: image/gif'); // send as image 
    
    
    
       //$random_image_name = rand(1,5);
    
         // you would add $folder.$files[$rand]; and include you script above
         $random_image_name = $folder.$files[$rand]; 
    
    
    
                                // adjust paths as needed
           $img_file = "http://www.domain.com/".$random_image_name.".gif";
                                                                   // .$file_ext;
    
    
       readfile($img_file);  // not sure how memory will go
    
        ?>
    

    The idear is to set the header content type and to read and write the file to the output buffer using readfile, armed with this you could use ajax as another means to request the image and using the status to 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 the
    approprate Content-type

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this script that displays all the users images which i will display
I have this RewriteRule that works too well :-) RewriteRule ^([^/]*)/$ /script.html?id=$1 [L] The
I have this script that displays a max of 5 images for each row,
I have this script that will let the user download the data from the
I have this script that basically will show a product or products based on
i have this script below that will resize the .content on window resize it
I have this script that collects data from users and I want to check
Well basically I have this script that takes a long time to execute and
I have this line in a useful Bash script that I haven't managed to
I have a Python script that is running a few ls commands. This script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.