Location: PHPKode > scripts > Ike Php Gallery > ike-php-gallery/index.php
<?php

/*
*  	This is Ike, a simplistic PHP-based Image Gallery with captions and tags
*
*	Current version: 1.1 (31.3.2008)
*	
*	USAGE:
*
*	0. Make changes to the configuration section of this file, if you think you need it.
*	You might want to move the css to an external file. 
*	The html it prints out are divided into two php variables, $thumbHtml and $html. 
*	You can add your own layout and just drop the variables in their appropriate places.
*
*	1. Make sure your webserver is running php (4 or newer), has the GD graphics library and the rights to create folders and files on the server.
*
*	2. Drop the file in a folder which contains your pictures in the size you want to present them. 
*	Make sure you don't have any weird characters in the pictures' filenames (including blank spaces).
*	You can name this file anything you want (as long the file extension is .php), although index.php is propably the most convenient.
*
*	3. Navigate to this file with your browser
*	
*	4. After a few seconds you will see a "Continue" -link, click on that
*	
*	5. Enter your preferred username and password (you have to set them in this file)
*	
*	6. Edit the pics' captions & tags if you want to
*	
*	7. Navigate to this file again
*	
*	8. Voilá! (Hopefully.)
*	
*	Workflow:
*	
*	- Check if the thumbnails are already made
*	- If not, create them and allow editing of the images' captions & tags
*	- If yes, show the gallery
*
*
*	WARRANTY & GUARANTEES
*	
*	None! You can modify the code under the terms of GPL (http://www.gnu.org/licenses/gpl-3.0.txt). 
*
*	BY:
*
*	hide@address.com 2008
*	
*
*/

//CONFIGURATION STARTS


//Texts that are used in the user interface.
$texts = array("pictext" => "Caption",
				"pictags" => "Tags",
				"save" => "Save",
				"editPics" => "Edit pics",
				"picsAdded" => "Pics added",
				"continue" => "Continue",
				"saveNextBtn" => "Save and go to the next",
				"savePrevBtn" => "Save and go to the previous",
				"allPics" => "All pics",
				"tagTitle" => "All pics tagged with #:",
				"firstPics" => "First pics",
				"lastPics" => "Last pics",
				"previous5" => "Previous 5 pics",
				"next5" => "Next 5 pics"
				);
//FINNISH TRANSLATION				
/*$texts = array("pictext" => "Kuvateksti",
				"pictags" => "Avainsanat",
				"save" => "Tallenna",
				"editPics" => "Muokkaa kuvia",
				"picsAdded" => "Kuvat lisätty",
				"continue" => "Jatka",
				"saveNextBtn" => "Tallenna ja mene seuraavaan",
				"savePrevBtn" => "Tallenna ja mene edelliseen",
				"allPics" => "Kaikki kuvat",
				"tagTitle" => "Kaikki aiheen # kuvat:",
				"firstPics" => "Ensimmäiset kuvat",
				"lastPics" => "Viimeiset kuvat",
				"previous5" => "Edelliset 5 kuvaa",
				"next5" => "Seuraavat 5 kuvaa"
				);*/
				
				
// Color values to use in thumbnail generation. These set the Red, Green and Blue values of the background color of the thumbail. 
// By default the color is white.
$thumbnailBackgroundColorRGB = array(
									"RED" => 255,
									"GREEN" => 255,
									"BLUE" => 255
									); 

//Thumbnail size in pixels (shape is always square)
$thumbSize = 60;

// Set these to keep strangers out of the edit mode.
								
$username = "Change";
$password = "these";

//LAYOUT FUNCTIONS
//USED TO CONTROL HTML OUTPUT
function thumbHtml($src, $alt, $selected = "", $tagLink = ""){

	$addr = $_SERVER['PHP_SELF']."?img=".$src;
	
	// Here you can edit the look & feel of the thumbnail pics. By default it's just a 
	// "img"-tag, surrounded by an "a"-tag. If the thumbnail is of the currently visible pic,
	// the $selected-attribute adds a " class="selected" "-css style to the thumbnail.
	

	
	$thumbHtml .= "<a href=\"$addr$tagLink\">\n";
	$thumbHtml .= "<img src=\"thumbs/$src\" alt=\"$alt\" $selected /></a>&nbsp;\n";
	
	
	
	return $thumbHtml;
}

function currentThumbHtml($img){

	// Here you can edit the look & feel of the currently selected thumbnail. By default it's just a thumbnail w/o a link and w/ a css style class "selected".



	$h = "<img src=\"thumbs/".$img."\" class=\"selected\" alt=\"".$img."\"/>&nbsp;\n";
	
	
	
	return $h;
	
}

function picTagHtml($addr, $picTag){

	// Here you can edit the html for the individual tags.



	$h = "<a href=\"$addr\">$picTag</a>&nbsp;\n";



	return $h;
}

function thumbRowHtmlPrevious($addr1, $title1, $addr2, $title2){

	//Here you can edit the HTML that happens before the thumbnails are printed. By default, it prints out < <<



	$h .= "<a href=\"$addr1\" title=\"$title1\" class=\"nextPrev\">&lsaquo;</a>&nbsp;";
	$h .= "&nbsp;<a href=\"$addr2\" title=\"$title2\" class=\"nextPrev\">&laquo;</a>&nbsp;";
	
	

	return $h;
}

function thumbRowHtmlNext($addr1, $title1, $addr2, $title2){

	//Here you can edit the HTML that happens after the thumbnails are printed. By default, it prints out >> >



	$h .= "<a href=\"$addr1\" title=\"$title1\" class=\"nextPrev\">&raquo;</a>&nbsp;";
	$h .= "&nbsp;<a href=\"$addr2\" title=\"$title2\" class=\"nextPrev\">&rsaquo;</a>&nbsp;";
	
	

	return $h;
}

function tagThumbHtml($title, $text){
	
	//Here you can edit the HTML that prints the describing text for the thumbnails for tagwords.
	
	
	
	$h = "<p>$title<br />$text</p>\n";


	
	return $h;

}

function mainPicHtml($img, $caption, $tagtitle, $tags){

	//Here you can edit the HTML that prints the main picture, the caption and the tags.
	
	
	
	$h .= "<img src=\"$img\" alt=\"$description\" class=\"main\" />";
	
	//If there's an caption for the picture, print the following
	if (isset($caption)) :
	
		$h .= "<p>$caption</p>\n";
	
	endif;
	
	
	//If there's any tags, print the following
	if (strlen($tags) > 0) :
	
		$h .= "<p><i>$tagtitle:&nbsp;$tags</i></p>\n";

	endif;
	
	
	return $h;

}

/*
	END OF CONFIGURATION
	No need to touch down from here. You can, though.
*/

				
//FUNCTIONS
function resize($filename)
{

	global $thumbnailBackgroundColorRGB, $thumbSize;

	if (!is_file("thumbs/".$filename)){
		$format = strtolower(substr(strrchr($filename,"."),1));
		$type ="jpg";
		$img = imagecreatefromjpeg($filename);
		list($org_width, $org_height) = getimagesize($filename);
		//
		$imginfo = @getimagesize($filename);
		if ($imginfo == NULL) return false;
		//		
		$newWidth = $thumbSize;
		//		
		$srcWidth = $imginfo[0];
		$srcHeight = $imginfo[1];
		$ratioWidth = $srcWidth/$newWidth;
		$ratioHeight = 0;
		$destWidth = $newWidth;
		$destHeight = $srcHeight / $ratioWidth;
		//	
		$yoffset = ($newWidth - $destHeight) * .5;
		if ($yoffset < 0) $yoffset = 0;
		//			 
		$img_n=imagecreatetruecolor($newWidth, $newWidth);
		
		$bgColor = imagecolorallocate($img_n, $thumbnailBackgroundColorRGB["RED"], $thumbnailBackgroundColorRGB["GREEN"], $thumbnailBackgroundColorRGB["BLUE"]);
		imagefill($img_n, 0, 0, $bgColor);
		imagecopyresampled($img_n, $img, $xoffset, $yoffset, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
		$dest = "thumbs/".$filename;
		imagejpeg($img_n, $dest);
		return true;
	} else {
		return false;
	}
  	
}

function scanDirectory($dir){
	
	$dh  = opendir($dir);
	while (false !== ($filename = readdir($dh))) {
		$files[] = $filename;
	}
	sort($files);
	
	return $files;
	
}

function getList(){
	$dir = getcwd();
	$list = scanDirectory($dir);
	for ($i = 0; $i < count($list); $i++){
		if ($list[$i] != basename($_SERVER['PHP_SELF']) && is_file($list[$i]) && strtolower($list[$i]) != ".ds_store" && strtolower($list[$i]) != "data.txt"){
			$files[] = $list[$i];
		}
	}
	return $files;
}

function text($text, $replace = ""){
	global $texts;
	
	$replace = str_replace("\\","",$replace);
	
	if (is_numeric(strpos($texts[$text], "#"))){
		$str = str_replace("#", "<span class=\"tagWord\">".$replace."</span>", $texts[$text]);
	} else {
		$str = $texts[$text];
	}
	return $str;
}


if ($_POST['uname']){
	session_start();
	
	if ($_POST['uname'] == $username && $_POST['pass'] == $password){
	
		$_SESSION['user'] = true;
	} else {
		$_SESSION['user'] = false;
	}
	header("Location: ".$_SERVER['PHP_SELF']."?action=edit");
	die();
}


$action = $_GET['action'];
if (!$action && !is_dir("thumbs")){
	mkdir("thumbs");
	$action = "new";
}
if ($action == "new") :
	//NEW
	$dir = getcwd();
	$list = scandir($dir);
	$files = array();
	for ($i = 0; $i < count($list); $i++){
		if ($list[$i] != basename($_SERVER['PHP_SELF']) && is_file($list[$i])  && $list[$i] != "data.txt" && $list[$i] != ".DS_Store"){
			$files[] = $list[$i];
			//Do thumbnails
			if (!is_file("thumbs/".$list[$i])){
				$fileContents .= $list[$i]."#"."#";
				resize($list[$i]);
			}
		}
	}
	//Make file with data
	if (!is_file("data.txt")){
		$handle = fopen("data.txt", "w");
		if (fwrite($handle, $fileContents) === FALSE) {
			echo "Cannot write to file ($filename)";
			exit;
		}
		fclose($handle);
	}
	$html = "<p><span class=\"message\">".text("picsAdded").".</span></p>\n";
	$html .= "<p><a href=\"".$_SERVER['PHP_SELF']."?action=edit\">".text("continue")."</a></p>\n";
	
	
elseif ($action == "edit") :
	//EDIT
	
	session_start();
	
	if ($_SESSION['user'] == false) :
	
		$html = '<form name="form1" method="post"><p><input name="uname" type="text" id="uname"></p><p><input name="pass" type="password" id="pass"></p><p><input type="submit" name="Submit" value="&gt;&gt;&gt;"></p></form>';
	
	
	else :
	
	
	$html = "<h1>".text("editPics")."</h1>\n";
	//
	$list = getList();
	if (!$_GET['img']){
		$img = $list[0];
	} else {
		$img = $_GET['img'];
	}
	//Get data
	$data = file_get_contents("data.txt");
	$data = split("#",$data);
	$int = array_search($img, $data);
	$dataInt = $int+1;
	//Save data
	if ($_POST){
	
		$data2save = $_POST['picText']."|".$_POST['picTags'];
		$data[$dataInt] = $data2save;
		$fileContents = implode("#",$data);
		$handle = fopen("data.txt", "w");
		if (fwrite($handle, $fileContents) === FALSE) {
			echo "Cannot write to file ($filename)";
			exit;
		}
		fclose($handle);
		if ($_POST['saveNextBtn']){
			$nextPicInt = $int+2;
			$img = $data[$nextPicInt];
		} else if ($_POST['savePrevBtn']){
			$prevPicInt = $int-2;
			$img = $data[$prevPicInt];
		}
		$url = $_SERVER['PHP_SELF']."?action=edit&img=".$img;
		header("Location: $url");
	}
	$picInfo = $data[$int+1];
	$picInfo = explode("|", $picInfo);
	//
	
	$html .= "<form method=\"POST\">";
	$html .= "<img src=\"".$img."\" height=\"300\" alt=\"$img\" />";
	$html .= "<p>".text("pictext")."<br /><textarea name=\"picText\">".$picInfo[0]."</textarea></p>\n";
	$html .= "<p>".text("pictags")."<br /><textarea name=\"picTags\">".$picInfo[1]."</textarea></p>\n";
	$html .= "<p><input name=\"saveNextBtn\" type=\"submit\" value=\"".text("saveNextBtn")."\" />&nbsp;\n";
	$html .= "<input name=\"savePrevBtn\" type=\"submit\" value=\"".text("savePrevBtn")."\" />&nbsp;\n";
	$html .= "<input name=\"saveBtn\" type=\"submit\" value=\"".text("save")."\" /></p>\n";
	$html .= "</form>";
	$html .= "<p>";
	for ($i = 0; $i < count($list); $i++){
		$html .= "<a href=\"".$_SERVER['PHP_SELF']."?action=edit&amp;img=".$list[$i]."\">\n";
		$html .= "<img src=\"thumbs/".$list[$i]."\" alt=\"".$list[$i]."\" height=\"$thumbSize\" width=\"$thumbSize\" /></a>&nbsp;\n";
	}
	$html .= "</p>";
	
	
	endif;
	
	//
elseif ($action == "xml") :
	//SHOW XML
	//Coming in future versions. I hope.
	$html = "Näytä XML.";
	
	
else :
	
	//VIEW PICS
	$list = getList();
	if (!$_GET['img']){
		$img = $list[0];
	} else {
		$img = $_GET['img'];
	}
	
	//Get data
	$data = file_get_contents("data.txt");
	$data = split("#",$data);
	$int = array_search($img, $data);
	$dataInt = $int+1;
	$picInfo = $data[$int+1];
	$picInfo = str_replace("\\","",$picInfo);
	$picInfo = explode("|", $picInfo);
	
	$metaTags = $picInfo[1];
	
	$picInfo[1] = str_replace(",", "", $picInfo[1]);
	$picTags = explode(" ", $picInfo[1]);
	foreach ($picTags as $picTag){
	
		if (strlen($picTag) > 0){
	
			$addr = $_SERVER['PHP_SELF']."?img=$img&amp;tag=$picTag";
			$picTagHtml .= picTagHtml($addr, $picTag);
			
		}
		//
		
	}
	
	//GET TAGS
	$tagList = array();
	$currentTag = $_GET['tag'];
	if (isset($currentTag)){
		foreach ($data as $key => $value){
			if (is_numeric(strpos($value, "|"))){
				$value = explode("|", $value);
				$tags = $value[1];
				$tags = explode(" ", $tags);
				if (is_numeric(array_search($currentTag, $tags))){
					$data[$key-1] == $img ? $add = "class=\"selected\" " : $add = "";
					
					
					
					$tagThumbHtml .= thumbHtml($data[$key-1], $value[0], $add, "&tag=".$_GET['tag']);

				}
				
			}
		}
	}
	
	//
	if (isset($tagThumbHtml)){
	
		$html .= tagThumbHtml(text("tagTitle",$currentTag), $tagThumbHtml);

	}
	
	$html .= mainPicHtml($img, $picInfo[0], text("pictags"), $picTagHtml);
	
	//Show thumbs
	$thumbHtml .= "<div class=\"allThumbs\"><p>";
	if ($int > 10){
		$prevpics = $data[$int-12];
	
		$addr1 = $_SERVER['PHP_SELF'];
		$title1 = text("firstPics");
		$addr2 = $_SERVER['PHP_SELF']."?img=$prevpics"; 
		$title2 = text("previous5");
		
		$thumbHtml .= thumbRowHtmlPrevious($addr1, $title1, $addr2, $title2);
		
	}
	
	//Thumbs before current
	for ($i = $int-10; $i < $int; $i+=2){
		if (isset($data[$i])){
		
			$thumbHtml .= thumbHtml($data[$i], $data[$i+1]);
		
		}
	}
	
	//Current
	$thumbHtml .= currentThumbHtml($img);
	
	//Thumbs after current
	for ($i = $int + 2; $i < $int + 12; $i+=2){
		if (isset($data[$i]) && is_file("thumbs/".$data[$i])){		
			$thumbHtml .= thumbHtml($data[$i], $data[$i+1]);
		}
	}
	$lastpicArr = $data;
	$lastInt = count($lastpicArr)-3;
	if ($int < $lastInt - 10){
		$lastpic = $lastpicArr[$lastInt];
		$nextpics = $lastpicArr[$int+12];
	
		
		$thumbHtml .= thumbRowHtmlNext($_SERVER['PHP_SELF']."?img=$nextpics", text("next5"), $_SERVER['PHP_SELF']."?img=$lastpic", text("lastPics"));
		
	}
	$thumbHtml .= "</p></div>";
	
endif;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php 

if (strlen($metaTags) > 1) : 

?>

<meta name="keywords" content="<?php echo $metaTags; ?>" />

<?php 
	
endif;

?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Kuvia.</title>
<style type="text/css">
<!--
body {
	font-family:"Trebuchet MS", Tahoma, Verdana, sans-serif;
	font-size: 14px;
	text-align:center;
	background-color:#000;
	color: #FFF;
}
a {
	color: #E0FCEB;
}
img {
	border: 1px solid #DB0048;
}
.main {
	border: 1px solid #E0FCEB;
}
textarea {
	width: 360px;
	height: 50px;
}
.selected {
	border: 3px solid #DB0048;
}
.tagWord {
	font-style:italic;
}
.message {
	color:#000000;
	padding: 10px;
	border: 1px dashed #990000;
	background-color:#FFCCFF;
}
.allThumbs img{
	vertical-align:middle;
}
.nextPrev{
	font-size: 40px;
	text-decoration:none;
	line-height: 10px;
}
-->
</style>
</head>
<body>
<?php echo $thumbHtml; ?>
<?php echo $html; ?>
</body>
</html>
Return current item: Ike Php Gallery