Solved

Rotate images

Posted on 2011-03-13
5
466 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

861 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now