Friday, August 19, 2011

Défilement d'une image php/Javascript

Un bout de code pour défiler un repertoire d'images dans une page web en php:
(Ume image chaque 4 secondes)


 
<head>



<title>Defilement images</title>

<meta name="author" content="Wassim Jied coderspirit.blogspot.com" />



</head>

<body onLoad="rotate();">

<?php

$directory_name = "./themes/matrice/img/";

$extensions_ok = array ( ".jpg","jpeg",".gif",".png");

$folder = opendir ($directory_name);

$i = 0;

$photos = array();

while ($file = readdir ($folder))

{

if (in_array(strtolower(substr($file, -4)),$extensions_ok))

$photos[] = $directory_name."/".$file;

}

closedir ($folder);

$count_photos = count ($photos);

?>

<script type="text/javascript">

var rotate_delay = 4000; /*delais en microseconde (4000 = 4 secs)*/

var current = -1;

var photos = [<?php for($i=0;$i<$count_photos;$i++)

{

echo "\"".$photos[$i]."\"";

if($i!=$count_photos-1) echo ",";

}

?>];

function next() {

if (current<photos.length - 1)

++current;

else current = 0;

}

function rotate() {

if(photos.length > 0)

{

next();

document.images.slides.src = photos[current];

window.setTimeout("rotate()", rotate_delay);

}

}

  /*Original code from http://www.developpez.net/forums/d770199/webmasters-developpement-web/javascript/apercu-image-pop-up/ Thanks to the author , modified by WJ*/

function popImage(largeur,hauteur,options){



var top  = (screen.height-hauteur)/2;

var left = (screen.width-largeur)/2;

window.open(document.images.slides.src,"_blank","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);

}



</script>

<center>

<table>

<tr>

<td>

 <img src="/" id="slides"  onclick="javascript:popImage(730,620,'menubar=no,scrollbars=yes,statusbar=yes')" style="cursor:pointer;"/>

 

</td>

</tr>

</table>

</body>