Jump to content

Converting an image Rotate.php to making a pic to pic link structure


Recommended Posts

Is there a way to use php to incorporate a folder of pictures as a clickable slideshow.

 

For instance.... using a pic as a button... to go to the next pic.... except in quantities of 100... without having to manually write the code for each page...?

In HTML:

<a href="index3.html"><img src="2.jpg" /></a>
<a href="index4.html"><img src="3.jpg" /></a>
<a href="index5.html"><img src="4.jpg" /></a>
<a href="index6.html"><img src="5.jpg" /></a>
<a href="index7.html"><img src="6.jpg" /></a>
<a href="index8.html"><img src="7.jpg" /></a>
<a href="index9.html"><img src="8.jpg" /></a>

 

Something like using a basic rotating script activated on mouse click? How do i convert this into a button without making 100 pages?

<img src="/path/to/images/
rotate.php?img=my_static_image.jpg" />

 

I have come up with the rotate.php file as following:

 

<?php


$folder = '/oprah/is/sofuckingfat.com/images/';

$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';


/* I believe that most of the following can be omitted once it hits the timer / countdown script */

$img = null;

if (substr($folder,-1) != '/') {
$folder = $folder.'/';
}

if (isset($_GET['img'])) {
$imageInfo = pathinfo($_GET['img']);
if (
    isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
        file_exists( $folder.$imageInfo['basename'] )
    ) {
	$img = $folder.$imageInfo['basename'];
}
} else {
$fileList = array();
$handle = opendir($folder);
while ( false !== ( $file = readdir($handle) ) ) {
	$file_info = pathinfo($file);
	if (
	    isset( $extList[ strtolower( $file_info['extension'] ) ] )
	) {
		$fileList[] = $file;
	}
}
closedir($handle);

if (count($fileList) > 0) {
	$imageNumber = time() % count($fileList);
	$img = $folder.$fileList[$imageNumber];
}
}

if ($img!=null) {
$imageInfo = pathinfo($img);
$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
header ($contentType);
readfile($img);
} else {
if ( function_exists('imagecreate') ) {
	header ("Content-type: image/png");
	$im = @imagecreate (100, 100)
	    or die ("Cannot initialize new GD image stream");
	$background_color = imagecolorallocate ($im, 255, 255, 255);
	$text_color = imagecolorallocate ($im, 0,0,0);
	imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
	imagepng ($im);
	imagedestroy($im);
}
}

?>

 

 

 

Would be more or less converting the rotate.php script into the base for the link structure. I assume the timing element in this script would be omitted with a replacement or nothing. I can't figure out what will be the quickest way to go about making the href follow the folder contents. I just want a basic click on the pic to go to the next one until it ends. Thoughts anyone?

 

I suppose you are thinking something like this:

<?php
session_start();

$dir = 'yorkwood/images'; //<<<---- Path to your images, NO TRAILING SLASH.


if(!isset($_SESSION['images'])) { //If the image array IS NOT saved in sessions
if ($handle = opendir($dir)) { //Open the image directory.
	while (false !== ($file = readdir($handle))) { //while there are files to read.
		if($file != "." && $file != "..") { //if the file isn't current dir, or above dir.
		   $parts = explode('.',$file); //split the file on a period.			   
		   $c = count($parts); //count the parts array.
		   $ext = $parts[$c-1]; //ext will reside as the last value of the array.
		   if(strtolower($ext) == 'png' || strtolower($ext) == 'jpg' || strtolower($ext) == 'gif') { //if the ext is image type.				
				$images[] = $file; //save it to the image array.
			}
		}
	}
	closedir($handle); //close the directory.
}
$_SESSION['images'] = $images; //write the image array to a session variable.
} //this closes the if block, the above code will only run when the page is first open.

//Below is the displaying of the images, if you add images to the directory, you MUST close the browser window for
//this script to pick them up.

if(!isset($_SESSION['nextimage'])) { //if next image is NOT in the session array.
$next = $_SESSION['images'][1]; //next image will be the second image in the image array.
$current = $_SESSION['images'][0]; //current image will be the first image in the array.
}
else { //if next image is in the session array.
if(isset($_GET['next'])) { //and next is in the url bar.
	$current = $_SESSION['nextimage']; //current image is changed to the one held in Sessions nextimage.
}
elseif(isset($_GET['prev'])) { //or if prev is in the url bar,
	$current = $_SESSION['previousimage']; //we go to the previousimage in our sessions array.
}	
}
$keys = array_keys($_SESSION['images'],$current); //find our array key of the current pic.
$n = $keys[0]; //the key will reside in the first place of the keys array.
$next = (array_key_exists($n+1,$_SESSION['images'])) ? $_SESSION['images'][$n+1] : $_SESSION['images'][0]; //if the next array key exists in the images array, set the next image, if it don't the first image is next.
$previous = (array_key_exists($n-1,$_SESSION['images'])) ? $_SESSION['images'][$n-1] : end($_SESSION['images']); //if the previous key exists in the images array, set the previous image to it, if it don't set previous to the last value of the image array.

$_SESSION['nextimage'] = $next; //write the next to the session array.
$_SESSION['previousimage'] = $previous; //write the previous to the session array.
echo '<img src="' . $dir . '/' . $current . '" alt="images" /><br />' . "\n"; // print the image element to the page.

echo (isset($previous)) ? '<a href="?prev=1">Previous</a><br />' : NULL; //print the previous link to the page.
echo (isset($next)) ? '<a href="?next=1">Next</a><br />' : NULL; //print the next link to the page.
?>

 

Except the previous and next links would be images of 1 <> 100?

nice. i was just having problems associating the array to the session. or vis versa. i was trying to avoid using direction keys by using each image as the button. im certain i can adjust this myself. Many many thanks.

Ok Made Minor Change and now all hell has broke loose. Tried to make adjustment to eliminate the buttons using the pic as the button...

 

Using This Code:

 

<?php 
session_start(); 

$dir = '/Test/images'; //<<<---- Path to your images,  


if(!isset($_SESSION['images'])) { //If the image array IS NOT saved in sessions 
     

if ($handle = opendir($dir)) { //Open the image directory. 

     
while (false !== ($file = readdir($handle))) { //while there are files to read. 

     
if($file != "." && $file != "..") { //if the file isn't current dir, or above dir. 

     
   $parts = explode('.',$file); //split the file on a period. 

   $c = count($parts); //count the parts array. 

   $ext = $parts[$c-1]; //ext will reside as the last value of the array. 

   if(strtolower($ext) == 'png' || strtolower($ext) == 'jpg' || strtolower($ext) == 'gif') { //if the ext is image type. 

$images[] = $file; //save it to the image array. 

} 
} 
} 

closedir($handle); //close the directory. 

} 

$_SESSION['images'] = $images; //write the image array to a session variable. 

} //this closes the if block, the above code will only run when the page is first open. 

//Below is the displaying of the images, if you add images to the directory, you MUST close the browser window for 
//this script to pick them up. 

if(!isset($_SESSION['nextimage'])) { //if next image is NOT in the session array. 

$next = $_SESSION['images'][1]; //next image will be the second image in the image array. 

$current = $_SESSION['images'][0]; //current image will be the first image in the array. 

} 

else { //if next image is in the session array. 

if(isset($_GET['next'])) { //and next is in the url bar. 

$current = $_SESSION['nextimage']; //current image is changed to the one held in Sessions nextimage. 

}     

elseif(isset($_GET['prev'])) { //or if prev is in the url bar, 

$current = $_SESSION['previousimage']; //we go to the previousimage in our sessions array. 
} 
} 

$keys = array_keys($_SESSION['images'],$current); //find our array key of the current pic. 

$n = $keys[0]; //the key will reside in the first place of the keys array. 

$next = (array_key_exists($n+1,$_SESSION['images'])) ? $_SESSION['images'][$n+1] : $_SESSION['images'][0]; //if the next array key exists in the images array, set the next image, if it don't the first image is next. 

$previous = (array_key_exists($n-1,$_SESSION['images'])) ? $_SESSION['images'][$n-1] : end($_SESSION['images']); //if the previous key exists in the images array, set the previous image to it, if it don't set previous to the last value of the image array. 

$_SESSION['nextimage'] = $next; //write the next to the session array. 
$_SESSION['previousimage'] = $previous; //write the previous to the session array. 

$photo=$dir."/".$current;
echo "<a href=\"?next=1\"><img src=\"$photo\" alt=\"images\" /></a>"; // print the image element to the page. 

?> 

 

 

Displaying:

 

HelpExample1.png

 

With this in the folder.

 

HelpExample2.png

 

Any Thoughts?

I didn't bother to see what you changed, but this script works on my end.  Click the picture to go to the next one.  There is no previous button now.

 

<?php
session_start();

$dir = 'yorkwood/images'; //<<<---- Path to your images, NO TRAILING SLASH.


if(!isset($_SESSION['images'])) { //If the image array IS NOT saved in sessions
if ($handle = opendir($dir)) { //Open the image directory.
	while (false !== ($file = readdir($handle))) { //while there are files to read.
		if($file != "." && $file != "..") { //if the file isn't current dir, or above dir.
		   $parts = explode('.',$file); //split the file on a period.			   
		   $c = count($parts); //count the parts array.
		   $ext = $parts[$c-1]; //ext will reside as the last value of the array.
		   if(strtolower($ext) == 'png' || strtolower($ext) == 'jpg' || strtolower($ext) == 'gif') { //if the ext is image type.				
				$images[] = $file; //save it to the image array.
			}
		}
	}
	closedir($handle); //close the directory.
}
$_SESSION['images'] = $images; //write the image array to a session variable.
} //this closes the if block, the above code will only run when the page is first open.

//Below is the displaying of the images, if you add images to the directory, you MUST close the browser window for
//this script to pick them up.

if(!isset($_SESSION['nextimage'])) { //if next image is NOT in the session array.
$next = $_SESSION['images'][1]; //next image will be the second image in the image array.
$current = $_SESSION['images'][0]; //current image will be the first image in the array.
}
else { //if next image is in the session array.
if(isset($_GET['next'])) { //and next is in the url bar.
	$current = $_SESSION['nextimage']; //current image is changed to the one held in Sessions nextimage.
}
elseif(isset($_GET['prev'])) { //or if prev is in the url bar,
	$current = $_SESSION['previousimage']; //we go to the previousimage in our sessions array.
}	
}
$keys = array_keys($_SESSION['images'],$current); //find our array key of the current pic.
$n = $keys[0]; //the key will reside in the first place of the keys array.
$next = (array_key_exists($n+1,$_SESSION['images'])) ? $_SESSION['images'][$n+1] : $_SESSION['images'][0]; //if the next array key exists in the images array, set the next image, if it don't the first image is next.
$previous = (array_key_exists($n-1,$_SESSION['images'])) ? $_SESSION['images'][$n-1] : end($_SESSION['images']); //if the previous key exists in the images array, set the previous image to it, if it don't set previous to the last value of the image array.

$_SESSION['nextimage'] = $next; //write the next to the session array.
$_SESSION['previousimage'] = $previous; //write the previous to the session array.
echo '<a href="?next=1"><img src="' . $dir . '/' . $current . '" alt="images" /></a><br />' . "\n"; // print the image element to the page.

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.