Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 489
  • Last Modified:

Rotate images

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
dcass
Asked:
dcass
  • 4
2 Solutions
 
dcassAuthor Commented:
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
 
Mohamed AbowardaSoftware EngineerCommented:
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
 
dcassAuthor Commented:
Wrong use of rotate - I got it.
0
 
dcassAuthor Commented:
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
 
dcassAuthor Commented:
Did not get solution
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now