Solved

Rotate images

Posted on 2011-03-13
5
479 Views
Last Modified: 2013-12-13
I'm new to this, so I don't understand how this works.
I just need an explanation on how I can call this code multiple times.
What it does is display the ads (which will have to be a variable, but I'm hardcoding 6 for now),
rotates them (imgs 2 - 6 - misses #1), and then stops.  I need it to keep going until user leaves the page.
I need to know
1) how to make it rotate indefinitely (I tried putting the RotateImages(0) multiple times and also changing the 0 to 1, 2, etc. - it did not work).
2) include the first image in the rotation sequence.
I also tried getting the random number (rn) outside the function and passing it in, but that didn't work.



<html>
<body>
<?php
// read file and load array with image names
$account="account";
$adImage="Images";
$directory = "C:/inetpub/wwwroot/Planroom/DataBase/account/Ads/Images";
$handler = opendir($directory);

if(file_exists($directory)) {

    $results = Array();
    $x=0;

    while ($file = readdir($handler)) {
       if ($file != "." && $file != "..") {
        $results[$x] = $file;
        $x+=1;
      }
    }
    closedir($handler);
}
// Get random number for starting image  
   $rn = rand(0, $x);
   $top = $rn+6;
   $high = $x-6;
   $rnn = $rn-7;
   if ($rn >= $high) {
     $rn = $rnn;
   }
   echo "<table align=center cellpadding=0 cellspacing=0>";
   $r=1;
   
   echo("rn=$rn");  
   
   for ($i=$rn; $i<$top; $i++) {
     $rx = "r$r";
     if ($r < 7) {  
     echo "<tr><td valign=top align=center><a id='imageurl' ><img id='$rx' border='0' src='Database/account/Ads/Images/$results[$i]'></img></a><BR><BR></td></tr>";
     $r+=1;
     }
   }
   
   echo "<tr><td valign=bottom align=center><font style=font-size:10pt color=black face=Verdana><b>Click on the image<br>for more information.</b></font></td></tr></table>";
 
?>
<script language="JavaScript">
 function RotateImages(Start) {
      var path = 'Database/account/Ads/Images/';
      var sResults = ["<?php echo implode ('","', $results); ?>"];  // Images from folder
      var a = Array();
      var x = <?php echo $x; ?>;  // Total images
      x=x-1; // There are 50 but x had 51, so adjust 
      var rn = <?php echo $rn; ?>; // random number generated
      rn+=6; // add 6 because 6 are originally displayed  
      if (rn > x-6) { // if random number is greater than the total # images, reset to 0
          rn=0;   
      } 
        
      // Load display array (6 images) starting at random #                        
      for (i=0;i<7;i++)
      {
        a[i] = (path+sResults[rn]);
        rn+=1;
        if (rn > x) { // if random number is greater than the total # images, reset
          rn=0;   
        } 
      }
       // load URL from database
        var y = Start+1;
        var sID = "r"+y;
        var b = document.getElementById(sID);
        if (Start >= a.length)
            Start = 0;
         b.src = a[Start];
        window.setTimeout("RotateImages(" + (Start+1) + ")", 1000);
  } 
   RotateImages(0);
</script>

Open in new window

0
Comment
Question by:dcass
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 

Author Comment

by:dcass
ID: 35122294
Plus this code seems extremely inefficient because it reloads the display array each time through, so 6 times it goes through loading several hundred ads - very slow.
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35124608
Please refer to this:
http://php.net/manual/en/function.imagerotate.php
<?php
// File and rotation
$filename = 'test.jpg';
$degrees = 180;

// Content type
header('Content-type: image/jpeg');

// Load
$source = imagecreatefromjpeg($filename);

// Rotate
$rotate = imagerotate($source, $degrees, 0);

// Output
imagejpeg($rotate);
?>

Open in new window

0
 

Assisted Solution

by:dcass
dcass earned 0 total points
ID: 35140385
Wrong use of rotate - I got it.
0
 

Accepted Solution

by:
dcass earned 0 total points
ID: 35140414
I'm posting the code for others.
<html>
<body>
<?php

$account="account";
$adImage="Images";
$directory = "C:/inetpub/wwwroot/Planroom/DataBase/account/Ads/Images";
$handler = opendir($directory);

if(file_exists($directory)) {

    $results = Array();
    $x=0;

    while ($file = readdir($handler)) {
       if ($file != "." && $file != "..") {
        $results[$x] = $file;
        $x+=1;
      }
    }
    closedir($handler);
echo($x);
}
?>

<input type='hidden' name='x' value="<?php echo $x; ?>"> 

<?php

// Get random number for starting image 
   $x=$x-7;
   if ($x < 0)
     $x=0; 
   $rn = rand(0, $x);
   $top = $rn+6;
   if ($top > 50) {
     $top=50;
   }
   echo "<table align=center cellpadding=0 cellspacing=0>";
   $r=1;
       
   for ($i=$rn; $i<$top; $i++) {
     $rx = "r$r";
     //if ($r < 7) {  
     echo "<tr><td valign=top align=center><a id='imageurl' ><img id='$rx' border='0' src='Database/account/Ads/Images/$results[$i]'></img></a><BR><BR></td></tr>";
     $r+=1;
     //}
   }
   
   echo "<tr><td valign=bottom align=center><font style=font-size:10pt color=black face=Verdana><b>Click on the image<br>for more information.</b></font></td></tr></table>";
 
?>

<script language="JavaScript">
  var x = <?php echo $x; ?>;  // Total images    
  var sResults = ["<?php echo implode ('","', $results); ?>"];  // Images from folder
  x=x-1; // There are 50 but x had 51, so adjust 
  var imgArr = Array();
  var path = 'Database/account/Ads/Images/';
  var rn = <?php echo $rn; ?>; // random number generated
  rn=rn+6; // add because 6 are originally displayed  
  if (rn > x-6) { // if random number is greater than the total # images, reset to 0
      rn=0;   
  } 
   // Load display array starting at random number                       
      for (i=0;i<x;i++)
      {
        imgArr[i] = (path+sResults[i]);
      }
  var ImageHolder1 = document.getElementById('r1');
  var ImageHolder2 = document.getElementById('r2');
  var ImageHolder3 = document.getElementById('r3');
  var ImageHolder4 = document.getElementById('r4');
  var ImageHolder5 = document.getElementById('r5');
  var ImageHolder6 = document.getElementById('r6');
       var xy = rn;

 function RotateImages(holder,Start) {
       if (xy >= x)
          xy = 0;
  	   var a = eval("imgArr["+xy+"]");
  	   xy+=1;
       //if (Start >= a.length)
       //     Start = 0;
       var b = eval("ImageHolder"+holder);
	   b.src = a;
       // load URL from database
       // var c = new Array("url1", "url2", "url3", "url4");
       // var d = document.getElementById('imageurl');
       // d.href = c[Start];
       window.setTimeout("RotateImages("+holder+","+(Start+1)+")",3000);
 } 
   RotateImages(1,0);
   RotateImages(2,0);
   RotateImages(3,0);
   RotateImages(4,0);
   RotateImages(5,0);
   RotateImages(6,0);

</script>


  

</body>

</html>

Open in new window

0
 

Author Closing Comment

by:dcass
ID: 35174548
Did not get solution
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question