Solved

Rotate images

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

10 Experts available now in Live!

Get 1:1 Help Now