gallery/album with URL bbcode for forum

Soldato
Joined
12 Jun 2005
Posts
5,361
Thanks for the link seek.

I think i have figured out why my script is slow and trying to speed it up now.

I don't know any java, it's PHP i am doing this in :)
 
Soldato
OP
Joined
29 Jul 2003
Posts
7,668
ok thanks conrad.

i was thinking of uploader page for your script (egallery)

upload link/page with Jupload multi uploader. http://www.jupload.biz/

I've tested Jupload on my web server, seems to be very good. you upload multi-images to your webserver via FTP (see the left panel of Jupload). Jupload will generate thumbnails by using your PC processer (not the webserver) then upload them to the webserver. good? there are few php scripts need to change like URL of thumbnails folder to upload etc

was of thinking of that embedded in your egallery. that would be BEST gallery ever!
 
Soldato
Joined
12 Jun 2005
Posts
5,361
I think i have created a faster thumbnail recreating script.

Can the index.php file to this and see if its any faster:

This should create the thumbnails horizontally aswell.

Code:
<?

ini_set('display_errors', 0);

$extensions = array('gif','jpg','jpeg','png','bmp','wbm');
$thumbMaxSize = 150;
$tBorder = TRUE;
$imagesHorizonally = -1;

function CreateThumbnail ( $filename, $border = TRUE, $maxSize = 150, $quality = 90 )
{
	$imagePath = WorkingDir(0) . $filename;
	
	if ( $image = @imagecreatefromjpeg ( $imagePath ) )
	{
		$iWidth = imagesX ( $image );
		$iHeight = imagesY ( $image );

		$nHeight;
		$nWidth;

		if ( $iWidth > $iHeight )
		{
			$nWidth = $maxSize;
			$nHeight = floor ( $iHeight * ( $maxSize / $iWidth ) );
		}
		else
		{
			$nHeight = $maxSize;
			$nWidth = floor ( $iWidth * ( $maxSize / $iHeight ) );
		}

		rMkDir ( WorkingDir(1) );

		$thumbImage = ImageCreateTrueColor ( $nWidth, $nHeight );
		$thumbPath = WorkingDir(1) . $filename;

		imagecopyresampled($thumbImage, $image, 0, 0, 0, 0, $nWidth, $nHeight, $iWidth, $iHeight);

		if ( $Border )
		{
			$black = imagecolorallocate($tImg, 0, 0, 0);
			$white = imagecolorallocate($tImg, 255, 255, 255);

			imagerectangle($tImg, 0, 0, $tWidth - 1, $tHeight - 1, $black);
			imagerectangle($tImg, 1, 1, $tWidth - 2, $tHeight - 2, $white);
			imagerectangle($tImg, 2, 2, $tWidth - 3, $tHeight - 3, $white);
		}

		ImageJPEG($thumbImage,$thumbPath,$quality);
		chmod($thumbPath, 0644);
		imagedestroy($thumbImage);
		imagedestroy($image);
		
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

function ScanDirectory( $folderPath )
{
	if ( is_dir ( $folderPath ) )
	{
		$results;

	    if ( $dh = opendir ( $folderPath ) )
		{
			$i = 0;
	        while ( ($file = readdir ( $dh ) ) !== false )
			{
				if ( $file != "." && $file != ".." )
				{
					$results[$i] = $file;

					$i++;
				}
	        }

	        closedir($dh);
	    }

		return $results;
	}
	else
	{
		return array();
	}
}

function FolderDelete ( $folderPath )
{
	if ( is_dir ( $folderPath ) )
	{
		foreach ( ScanDirectory ( $folderPath ) as $value )
		{
			if ( $value != "." && $value != ".." )
			{
				$value = $folderPath . DIRECTORY_SEPARATOR . $value;

				if ( is_dir ( $value ) )
				{
					FolderDelete ( $value );
				}
				elseif ( is_file ( $value ) )
				{
					@unlink ( $value );
				}
			}
		}

		rmdir ( $folderPath );

		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

function rMkDir ( $path )
{
	if ( !is_dir ( $path ) )
	{
		if ( version_compare ( PHP_VERSION, '5.0.0' ) < 0 )
		{
			$exp = explode ( DIRECTORY_SEPARATOR, $path );
			$way = '';

			foreach ( $exp as $n )
			{
				$way .= $n . DIRECTORY_SEPARATOR;

				if ( !file_exists ( $way ) )
				{
					mkdir ( $way, 0777 );
				}
			}
		}
		else
		{
			mkdir ( $path, 0777, TRUE );
		}
	}
}

function BBCode ( $iFilename, $thumbnail = FALSE )
{
	$imagePath = ImageLink ( $iFilename );

	if ( ThumbnailExists ( $iFilename ) && $thumbnail )
	{
		$thumbnailPath = ThumbLink ( $iFilename );

		return "[url=" . $imagePath . "][img]" . $thumbnailPath . "[/img][/url]";
	}
	else
	{
		return "[img]" . $imagePath . "[/img]";
	}
}

function ImageLink ( $iFilename )
{
	return "http://" . $_SERVER['HTTP_HOST'] . str_replace ( "index.php", "", $_SERVER['PHP_SELF'] ) . "albums" . str_replace ( "//", "/", str_replace ( "\\", "/", str_replace ( " ", "%20", $_GET['dir'] ) ) ) . "/" . $iFilename;
}

function ThumbLink ( $iFilename )
{
	return "http://" . $_SERVER['HTTP_HOST'] . str_replace ( "index.php", "", $_SERVER['PHP_SELF'] ) . "thumbs" . str_replace ( "//", "/", str_replace ( "\\", "/", str_replace ( " ", "%20", $_GET['dir'] ) ) ) . "/" . $iFilename;
}

function WorkingDir ( $type = 0 )
{
	$cDir;

	if ( DIRECTORY_SEPARATOR == "/" )
	{
		$cDir = str_replace ( "//", "/", str_replace ( "\\", "/", $_GET['dir'] ) );
	}
	else
	{
		$cDir = str_replace ( "\\\\", "\\", $_GET['dir'] );
	}

	switch ( $type )
	{
		Case 0:
			return getcwd() . DIRECTORY_SEPARATOR . "albums" . $cDir . DIRECTORY_SEPARATOR;
			break;
		Case 1:
			return getcwd() . DIRECTORY_SEPARATOR . "thumbs" . $cDir . DIRECTORY_SEPARATOR;
			break;
	}
}

function ThumbnailExists ( $iFilename )
{
	if ( file_exists ( WorkingDir(1) . $iFilename ) )
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

function ActionBoxHTML ( $title, $body )
{
	$tabs = "\t\t\t";

	return $tabs . "<div id=\"action\">" . "\n" . "\t\t\t\t\t" . "<h2>" . $title . ":</h2>" . "\n" .
		   $tabs . "\t" . $body . "\n" .
		   $tabs . "\t" . "<p class=\"right\"><a href=\"?dir=" . stripslashes ( $_GET['dir'] ) . "\" title=\"Close BB Code box\">Close</a></p>" . "\n" .
		   $tabs . "</div>";
}

$actionHTML;
$feedback;

if ( !is_dir ( WorkingDir() ) )
{
	$_GET['dir'] = "";
}

if ( $_POST['folderAction'] )
{
	if ( $_GET['dir'] != "" )
	{
		if ( FolderDelete ( WorkingDir() ) )
		{
			FolderDelete ( WorkingDir(1) );

			$feedback = "<p id=\"fb2\"><b>Feedback:</b> The folder was successfully deleted.</p>";

			$_GET['dir'] = "";
		}
		else
		{
			$feedback = "<p id=\"fb1\"><b>Error:</b> The operation could not be completed.</p>";
		}
	}
	else
	{
		$feedback = "<p id=\"fb1\"><b>Error:</b> You cannot delete the root folder!</p>";
	}
}

if ( $_POST['imageAction'] )
{
	if ( $_POST['image'] )
	{
		$imageArray;

		$i = 0;
		while ( list ( $key, $val ) = @each ( $_POST['image'] ) )
		{
			$imageArray[$i] = $val;
			$i++;
		}

		switch ( $_POST['imageAction'] )
		{
			Case "Recreate Thumbnails":
				$success = 0;
				$failed = 0;
				$nothumb = 0;

				foreach( $imageArray as $value )
				{
					if ( CreateThumbnail ( $value, $tBorder ) )
					{
						$success++;
					}
					else
					{
						$failed++;
					}
				}

				$fbType = "2";
				if ( ( $nothumb > $success ) && ( $nothumb > $failed ) )
				{
					$fbType = "3";
				}
				else
				{
					if ( $failed > $success )
					{
						$fbType = "1";
					}
				}

				$fbMessage = "";
				if ( $success > 0 )
				{
					$fbMessage .= " " . $success . " image thumbnail(s) were successfully created.";
				}

				if ( $failed > 0 )
				{
					$fbMessage .= " " . $failed . " image or thumbnail(s) could <u>not</u> be created.";
				}

				if ( $nothumb > 0 )
				{
					$fbMessage .= " " . $nothumb . " image(s) <u>do not</u> need thumbnails.";
				}

				$feedback = "<p id=\"fb" . $fbType . "\"><b>Feedback:</b>" . $fbMessage . "</p>";
				break;
			Case "Generate BB Code":
				$bbCode;

				$i = 0;
				foreach( $imageArray as $value )
				{
					$bbCode .= BBCode ( $value, TRUE );

					$i++;

					if ( $i == $imagesHorizonally )
					{
						$bbCode .= "\r\n";
						$i = 0;
					}
					else
					{
						$bbCode .= " ";
					}
				}

				$actionHTML = ActionBoxHTML ( "BB Code", "<textarea rows=\"7\" cols=\"70\" onfocus=\"this.select()\">" . $bbCode . "</textarea>" );

				break;
			Case "Delete":
				$success = 0;
				$failed = 0;
				$nonexistant = 0;

				foreach( $imageArray as $value )
				{
					if ( is_file ( WorkingDir() . $value ) )
					{
						if ( @unlink ( WorkingDir() . $value ) )
						{
							if ( ThumbnailExists ( WorkingDir(1) . $value ) )
							{
								if ( @unlink ( WorkingDir(1) . $value ) )
								{
									$success++;
								}
								else
								{
									$failed++;
								}
							}
							else
							{
								$success++;
							}
						}
						else
						{
							$failed++;
						}
					}
					else
					{
						$nonexistant++;
					}
				}

				$fbType = "2";
				if ( ( $failed + $nonexistant ) > $success )
				{
					$fbType = "1";
				}

				$fbMessage = "";
				if ( $success > 0 )
				{
					$fbMessage .= " " . $success . " image(s) and thumbnail(s) were successfully deleted.";
				}

				if ( $failed > 0 )
				{
					$fbMessage .= " " . $failed . " image(s) or thumbnail(s) could <u>not</u> be deleted.";
				}

				if ( $nonexistant > 0 )
				{
					$fbMessage .= " " . $nonexistant . " image(s) did not exist on the server.";
				}

				$feedback = "<p id=\"fb" . $fbType . "\"><b>Feedback:</b>" . $fbMessage . "</p>";
				break;
		}
	}
	else
	{
		$feedback = "<p id=\"fb1\"><b>Error:</b> No images were selected.</p>";
	}
}

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

?>
<!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" xml:lang="en" lang="en">
<head>
	<title>eGallery</title>
	<link rel="stylesheet" type="text/css" href="style.css" media="all" />
	<script type="text/javascript">
		var formblock;
		var forminputs;
		function prepare() {
		formblock= document.getElementById('selected_images');
		forminputs = formblock.getElementsByTagName('input');
		}		
		function select_all(name, value) {
		for (i = 0; i < forminputs.length; i++) {
		var regex = new RegExp(name, "i");
		if (regex.test(forminputs[i].getAttribute('name'))) {
		if (value == '1') {
		forminputs[i].checked = true;
		} else {
		forminputs[i].checked = false;
		}
		}
		}
		}
		if (window.addEventListener) {
		window.addEventListener("load", prepare, false);
		} else if (window.attachEvent) {
		window.attachEvent("onload", prepare)
		} else if (document.getElementById) {
		window.onload = prepare;
		}
	</script>
</head>
<body>
	<div id="cont">
		<h1>e<span class="blue">Gallery</span></h1>
		<div id="main">
			<ul>
				<li><img src="images/folder.png" alt="Current Directory:" /><a href="?" title="Navigate to: Root Folder">Root Folder</a></li>
				<li>//</li>
<?

$tabs = "\t\t\t\t";

$dirBuild = "";
foreach( explode ( "\\", $_GET['dir'] ) as $value )
{
	if ( $value != "" )
	{
		$dirBuild .= "\\" . $value;
		echo $tabs . "<li><a href=\"?dir=" . $dirBuild . "\" title=\"Navigate to: " . $value . "\">" . $value . "</a></li>" . "\n" .
			 $tabs . "<li>//</li>" . "\n";
	}
}

?>
			</ul>
<?

if ( $actionHTML != "" )
{
	echo $actionHTML . "\n";
}

?>
			<form id="selected_images" method="post" action="">
				<ul id="user">
					<li style="float:left"><input type="button" onClick="select_all('image', '1');" value="Select All" /></li>
					<li style="float:left"><input type="button" onClick="select_all('image', '0');" value="Deselect All" /></li>
					<li><input name="folderAction" type="submit" class="del" onclick="return confirm('Are you sure you wish to delete the current directory?\r\nAny files or folders in the directory will be deleted too!');" value="Delete Folder" /></li>
					<li><input name="imageAction" type="submit" class="del" onclick="return confirm('Are you sure you wish to delete the selected images?');" value="Delete" /></li>
					<li><input name="imageAction" type="submit" value="Generate BB Code" /></li>
					<li><input name="imageAction" type="submit" value="Recreate Thumbnails" /></li>
				</ul>
				<ul>
					<li><h2>Folders in Current Directory:</h2></li>
<?

$tabs = "\t\t\t\t\t";

$i = 0;
foreach ( ScanDirectory( WorkingDir() ) as $value )
{
	if ( is_dir ( WorkingDir() . $value ) )
	{
		echo $tabs . "<li>-</li>" . "\n" .
			 $tabs . "<li><img src=\"images/folder.png\" alt=\"Folder\" /><a href=\"?dir=" . $_GET['dir'] . "\\" . $value . "\" title=\"Navigate to: $value\">$value</a></li>" . "\n";

		$i++;
	}
}

if ( $i == 0 )
{
	echo $tabs . "<li>No folders in this directory</li>" . "\n";
}

?>
				</ul>
<?

$tabs = "\t\t\t\t";

if ( $feedback != "" )
{
	echo $tabs . $feedback . "\n";
}

$i = 0;
foreach( ScanDirectory( WorkingDir() ) as $value )
{
	if ( is_file ( WorkingDir() . $value ) )
	{
		if ( in_array( strtolower ( end ( explode ( ".", $value ) ) ), $extensions) )
		{
			$thumbBox;			
			$thumbnailSRC = "images/image.png";
			if ( ThumbnailExists ( $value ) )
			{
				$thumbBox = $tabs . "\t\t" . "<h2>Thumb BB Code:</h2>" . "\n" . $tabs . "\t\t" . "<input class=\"i_code\" onfocus=\"this.select()\" value=\"" . BBCode ( $value, TRUE ) . "\" />" . "\n";
				$thumbnailSRC = ThumbLink ( $value );
			}

			echo $tabs . "<div class=\"thumb\">" . "\n" .
				 $tabs . "\t" . "<a href=\"" . ImageLink ( $value ) . "\" target=\"_blank\" title=\"Open image: " . $value . "\"><img src=\"" . $thumbnailSRC . "\" alt=\"" . $value . " thumbnail\" /></a>" . "\n" .
				 $tabs . "\t" . "<p class=\"code\">" . "\n" .
				 $thumbBox .
				 $tabs . "\t\t" . "<h2>Direct BB Code:</h2>" . "\n" .
				 $tabs . "\t\t" . "<input class=\"i_code\" onfocus=\"this.select()\" value=\"" . BBCode ( $value ) . "\" /><br />" . "\n" .
				 $tabs . "\t\t" . "<input class=\"check\" type=\"checkbox\" name=\"image[]\" value=\"" . $value . "\" />Select Image" . "\n" .
				 $tabs . "\t" . "</p>" . "\n" .
				 $tabs . "\t" . "<p class=\"clear\"></p>" . "\n" .
				 $tabs . "</div>" . "\n";

			$thumbBox = "";

			$i++;
		}
	}
}

if ( $i == 0 )
{
	echo $tabs . "<div class=\"thumb\">" . "\n" .
		 $tabs . "\t" . "<p class=\"center\"><b>There are no images in this folder!</b></p>" . "\n" .
		 $tabs . "</div>" . "\n";
}

?>
			</form>
		</div>
		<p id="copy">Copyright e<span class="blue">Gallery</span> // All rights reserved.</p>
	</div>
</body>
</html>
 
Soldato
OP
Joined
29 Jul 2003
Posts
7,668
ok. uploaded 115 images to a folder. click select all button, click thumbnails button, counted to 38 seconds then white blank screen with no error message. just 100% blank screen (firefox)

just did small test with 32 images (12mb), its worked but copy/paste the bbcode to my forum. display thumbnails fine but not clickable. just noticed the tag is missing from bbcode textare...o to bed now. gotta work in the morning :eek:
 
Soldato
Joined
6 Feb 2004
Posts
20,647
Location
England
What do you mean by this?

you're opening your php like this <? - bad practice. those are short tags. you should use <?php

HOW DARE YOU!!!£"!£

lol...i joke!.....i'm not bothered don't worry.

Anychance you could post marc's script so i can see how it works and hopefully improve mine.

Thanks.

remember, it's not my script. i only added a few lines to enable bbcode. :p

but looking at celerondude's, yours and my very own thumbnail functions, they all look pretty similar. have no idea why one would take longer than the other....

to stop it timing out, just add this to the start of the file....

set_time_limit(0);
 
Soldato
Joined
26 Dec 2003
Posts
16,522
Location
London
Use ImageMagick to resize images, sorted.

Code:
$ convert -strip 200x200 foo.jpg foo.thumb.jpg &

This will also neatly avoid the issue of PHP running out of memory on tremendously large images (as well as dealing with even more formats than GD does—generate your thumbs from DNGs for maximum qualitay!).
 
Soldato
Joined
6 Feb 2004
Posts
20,647
Location
England
I was under the impression that ImageMagick isn't enabled on most servers, so i thought GD would be best?

Would i be wrong in assuming that?

i'm assuming as this isn't a public gallery and the OP is wanting to resize hundreds of files at a time, rob is suggesting that he should process the files on his pc before he uploads them?
 
Soldato
Joined
12 Jun 2005
Posts
5,361
Hmm, on my sever i can only get an 4 second difference between my function and celerondude's (marc's) function when creating thumbnails for 100 images (47.8mb).

Marc's Script - 47 seconds
My Script - 51 seconds

Try this:

Code:
<?

ini_set('display_errors', 0);
set_time_limit(0);

$extensions = array('gif','jpg','jpeg','png','bmp','wbm');
$thumbMaxSize = 150;
$tBorder = TRUE;
$imagesHorizonally = 2;

function CreateThumbnail ( $filename, $border = TRUE, $maxSize = 150, $quality = 90 )
{
	$imagePath = WorkingDir() . $filename;

	if ( $image = @imagecreatefromjpeg ( $imagePath ) )
	{
		$iWidth = imagesX ( $image );
		$iHeight = imagesY ( $image );

		$nHeight;
		$nWidth;

		if ( $iWidth > $iHeight )
		{
			$nWidth = $maxSize;
			$nHeight = floor ( $iHeight * ( $maxSize / $iWidth ) );
		}
		else
		{
			$nHeight = $maxSize;
			$nWidth = floor ( $iWidth * ( $maxSize / $iHeight ) );
		}

		$thumbImage = ImageCreateTrueColor ( $nWidth, $nHeight );
		$thumbPath = WorkingDir(1) . $filename;

		imagecopyresampled($thumbImage, $image, 0, 0, 0, 0, $nWidth, $nHeight, $iWidth, $iHeight);

		if ( $border )
		{
			$black = imagecolorallocate($thumbImage, 0, 0, 0);
			$white = imagecolorallocate($thumbImage, 255, 255, 255);

			imagerectangle($thumbImage, 0, 0, $nWidth - 1, $nHeight - 1, $black);
			imagerectangle($thumbImage, 1, 1, $nWidth - 2, $nHeight - 2, $white);
			imagerectangle($thumbImage, 2, 2, $nWidth - 3, $nHeight - 3, $white);
		}

		ImageJPEG($thumbImage,$thumbPath,$quality);
		chmod($thumbPath, 0644);
		imagedestroy($thumbImage);
		imagedestroy($image);

		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

function ScanDirectory( $folderPath )
{
	if ( is_dir ( $folderPath ) )
	{
		$results;

	    if ( $dh = opendir ( $folderPath ) )
		{
			$i = 0;
	        while ( ($file = readdir ( $dh ) ) !== false )
			{
				if ( $file != "." && $file != ".." )
				{
					$results[$i] = $file;

					$i++;
				}
	        }

	        closedir($dh);
	    }

		return $results;
	}
	else
	{
		return array();
	}
}

function FolderDelete ( $folderPath )
{
	if ( is_dir ( $folderPath ) )
	{
		foreach ( ScanDirectory ( $folderPath ) as $value )
		{
			if ( $value != "." && $value != ".." )
			{
				$value = $folderPath . DIRECTORY_SEPARATOR . $value;

				if ( is_dir ( $value ) )
				{
					FolderDelete ( $value );
				}
				elseif ( is_file ( $value ) )
				{
					@unlink ( $value );
				}
			}
		}

		rmdir ( $folderPath );

		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

function rMkDir ( $path )
{
	if ( !is_dir ( $path ) )
	{
		if ( version_compare ( PHP_VERSION, '5.0.0' ) < 0 )
		{
			$exp = explode ( DIRECTORY_SEPARATOR, $path );
			$way = '';

			foreach ( $exp as $n )
			{
				$way .= $n . DIRECTORY_SEPARATOR;

				if ( !file_exists ( $way ) )
				{
					mkdir ( $way, 0777 );
				}
			}
		}
		else
		{
			mkdir ( $path, 0777, TRUE );
		}
	}
}

function BBCode ( $iFilename, $thumbnail = FALSE )
{
	$imagePath = ImageLink ( $iFilename );

	if ( ThumbnailExists ( $iFilename ) && $thumbnail )
	{
		$thumbnailPath = ThumbLink ( $iFilename );

		return "[url=" . $imagePath . "][img]" . $thumbnailPath . "[/img][/url]";
	}
	else
	{
		return "[img]" . $imagePath . "[/img]";
	}
}

function ImageLink ( $iFilename )
{
	return "http://" . $_SERVER['HTTP_HOST'] . str_replace ( "index.php", "", $_SERVER['PHP_SELF'] ) . "albums" . str_replace ( "//", "/", str_replace ( "\\", "/", str_replace ( " ", "%20", $_GET['dir'] ) ) ) . "/" . $iFilename;
}

function ThumbLink ( $iFilename )
{
	return "http://" . $_SERVER['HTTP_HOST'] . str_replace ( "index.php", "", $_SERVER['PHP_SELF'] ) . "thumbs" . str_replace ( "//", "/", str_replace ( "\\", "/", str_replace ( " ", "%20", $_GET['dir'] ) ) ) . "/" . $iFilename;
}

function WorkingDir ( $type = 0 )
{
	$cDir;

	if ( DIRECTORY_SEPARATOR == "/" )
	{
		$cDir = str_replace ( "//", "/", str_replace ( "\\", "/", $_GET['dir'] ) );
	}
	else
	{
		$cDir = str_replace ( "\\\\", "\\", $_GET['dir'] );
	}

	switch ( $type )
	{
		Case 0:
			return getcwd()  . DIRECTORY_SEPARATOR . "albums" . $cDir . DIRECTORY_SEPARATOR;
			break;
		Case 1:
			return getcwd() . DIRECTORY_SEPARATOR . "thumbs" . $cDir . DIRECTORY_SEPARATOR;
			break;
	}
}

function ThumbnailExists ( $iFilename )
{
	if ( file_exists ( WorkingDir(1) . $iFilename ) )
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

function ActionBoxHTML ( $title, $body )
{
	$tabs = "\t\t\t";

	return $tabs . "<div id=\"action\">" . "\n" . "\t\t\t\t\t" . "<h2>" . $title . ":</h2>" . "\n" .
		   $tabs . "\t" . $body . "\n" .
		   $tabs . "\t" . "<p class=\"right\"><a href=\"?dir=" . stripslashes ( $_GET['dir'] ) . "\" title=\"Close BB Code box\">Close</a></p>" . "\n" .
		   $tabs . "</div>";
}

$actionHTML;
$feedback;

if ( !is_dir ( WorkingDir() ) )
{
	$_GET['dir'] = "";
}

if ( $_POST['folderAction'] )
{
	if ( $_GET['dir'] != "" )
	{
		if ( FolderDelete ( WorkingDir() ) )
		{
			FolderDelete ( WorkingDir(1) );

			$feedback = "<p id=\"fb2\"><b>Feedback:</b> The folder was successfully deleted.</p>";

			$_GET['dir'] = "";
		}
		else
		{
			$feedback = "<p id=\"fb1\"><b>Error:</b> The operation could not be completed.</p>";
		}
	}
	else
	{
		$feedback = "<p id=\"fb1\"><b>Error:</b> You cannot delete the root folder!</p>";
	}
}

if ( $_POST['imageAction'] )
{
	if ( $_POST['image'] )
	{
		$imageArray;

		$i = 0;
		while ( list ( $key, $val ) = @each ( $_POST['image'] ) )
		{
			$imageArray[$i] = $val;
			$i++;
		}

		switch ( $_POST['imageAction'] )
		{
			Case "Recreate Thumbnails":
				$success = 0;
				$failed = 0;
				$nothumb = 0;

				rMkDir ( WorkingDir(1) );

				$startTime = time();

				foreach( $imageArray as $value )
				{
					if (   CreateThumbnail ( $value ) )
					{
						$success++;
					}
					else
					{
						$failed++;
					}
				}
				
				echo time() - $startTime;
				die();

				$fbType = "2";
				if ( ( $nothumb > $success ) && ( $nothumb > $failed ) )
				{
					$fbType = "3";
				}
				else
				{
					if ( $failed > $success )
					{
						$fbType = "1";
					}
				}

				$fbMessage = "";
				if ( $success > 0 )
				{
					$fbMessage .= " " . $success . " image thumbnail(s) were successfully created.";
				}

				if ( $failed > 0 )
				{
					$fbMessage .= " " . $failed . " image or thumbnail(s) could <u>not</u> be created.";
				}

				if ( $nothumb > 0 )
				{
					$fbMessage .= " " . $nothumb . " image(s) <u>do not</u> need thumbnails.";
				}

				$feedback = "<p id=\"fb" . $fbType . "\"><b>Feedback:</b>" . $fbMessage . "</p>";
				break;
			Case "Generate BB Code":
				$bbCode;

				$i = 0;
				foreach( $imageArray as $value )
				{
					$bbCode .= BBCode ( $value, TRUE );

					$i++;

					if ( $i == $imagesHorizonally )
					{
						$bbCode .= "\r\n";
						$i = 0;
					}
					else
					{
						$bbCode .= " ";
					}
				}

				$actionHTML = ActionBoxHTML ( "BB Code", "<textarea rows=\"7\" cols=\"70\" onfocus=\"this.select()\">" . $bbCode . "</textarea>" );

				break;
			Case "Delete":
				$success = 0;
				$failed = 0;
				$nonexistant = 0;

				foreach( $imageArray as $value )
				{
					if ( is_file ( WorkingDir() . $value ) )
					{
						if ( @unlink ( WorkingDir() . $value ) )
						{
							if ( ThumbnailExists ( WorkingDir(1) . $value ) )
							{
								if ( @unlink ( WorkingDir(1) . $value ) )
								{
									$success++;
								}
								else
								{
									$failed++;
								}
							}
							else
							{
								$success++;
							}
						}
						else
						{
							$failed++;
						}
					}
					else
					{
						$nonexistant++;
					}
				}

				$fbType = "2";
				if ( ( $failed + $nonexistant ) > $success )
				{
					$fbType = "1";
				}

				$fbMessage = "";
				if ( $success > 0 )
				{
					$fbMessage .= " " . $success . " image(s) and thumbnail(s) were successfully deleted.";
				}

				if ( $failed > 0 )
				{
					$fbMessage .= " " . $failed . " image(s) or thumbnail(s) could <u>not</u> be deleted.";
				}

				if ( $nonexistant > 0 )
				{
					$fbMessage .= " " . $nonexistant . " image(s) did not exist on the server.";
				}

				$feedback = "<p id=\"fb" . $fbType . "\"><b>Feedback:</b>" . $fbMessage . "</p>";
				break;
		}
	}
	else
	{
		$feedback = "<p id=\"fb1\"><b>Error:</b> No images were selected.</p>";
	}
}

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

?>
<!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" xml:lang="en" lang="en">
<head>
	<title>eGallery</title>
	<link rel="stylesheet" type="text/css" href="style.css" media="all" />
	<script type="text/javascript">
		var formblock;
		var forminputs;
		function prepare() {
		formblock= document.getElementById('selected_images');
		forminputs = formblock.getElementsByTagName('input');
		}		
		function select_all(name, value) {
		for (i = 0; i < forminputs.length; i++) {
		var regex = new RegExp(name, "i");
		if (regex.test(forminputs[i].getAttribute('name'))) {
		if (value == '1') {
		forminputs[i].checked = true;
		} else {
		forminputs[i].checked = false;
		}
		}
		}
		}
		if (window.addEventListener) {
		window.addEventListener("load", prepare, false);
		} else if (window.attachEvent) {
		window.attachEvent("onload", prepare)
		} else if (document.getElementById) {
		window.onload = prepare;
		}
	</script>
</head>
<body>
	<div id="cont">
		<h1>e<span class="blue">Gallery</span></h1>
		<div id="main">
			<ul>
				<li><img src="images/folder.png" alt="Current Directory:" /><a href="?" title="Navigate to: Root Folder">Root Folder</a></li>
				<li>//</li>
<?

$tabs = "\t\t\t\t";

$dirBuild = "";
foreach( explode ( "\\", $_GET['dir'] ) as $value )
{
	if ( $value != "" )
	{
		$dirBuild .= "\\" . $value;
		echo $tabs . "<li><a href=\"?dir=" . $dirBuild . "\" title=\"Navigate to: " . $value . "\">" . $value . "</a></li>" . "\n" .
			 $tabs . "<li>//</li>" . "\n";
	}
}

?>
			</ul>
<?

if ( $actionHTML != "" )
{
	echo $actionHTML . "\n";
}

?>
			<form id="selected_images" method="post" action="">
				<ul id="user">
					<li style="float:left"><input type="button" onClick="select_all('image', '1');" value="Select All" /></li>
					<li style="float:left"><input type="button" onClick="select_all('image', '0');" value="Deselect All" /></li>
					<li><input name="folderAction" type="submit" class="del" onclick="return confirm('Are you sure you wish to delete the current directory?\r\nAny files or folders in the directory will be deleted too!');" value="Delete Folder" /></li>
					<li><input name="imageAction" type="submit" class="del" onclick="return confirm('Are you sure you wish to delete the selected images?');" value="Delete" /></li>
					<li><input name="imageAction" type="submit" value="Generate BB Code" /></li>
					<li><input name="imageAction" type="submit" value="Recreate Thumbnails" /></li>
				</ul>
				<ul>
					<li><h2>Folders in Current Directory:</h2></li>
<?

$tabs = "\t\t\t\t\t";

$i = 0;
foreach ( ScanDirectory( WorkingDir() ) as $value )
{
	if ( is_dir ( WorkingDir() . $value ) )
	{
		echo $tabs . "<li>-</li>" . "\n" .
			 $tabs . "<li><img src=\"images/folder.png\" alt=\"Folder\" /><a href=\"?dir=" . $_GET['dir'] . "\\" . $value . "\" title=\"Navigate to: $value\">$value</a></li>" . "\n";

		$i++;
	}
}

if ( $i == 0 )
{
	echo $tabs . "<li>No folders in this directory</li>" . "\n";
}

?>
				</ul>
<?

$tabs = "\t\t\t\t";

if ( $feedback != "" )
{
	echo $tabs . $feedback . "\n";
}

$i = 0;
foreach( ScanDirectory( WorkingDir() ) as $value )
{
	if ( is_file ( WorkingDir() . $value ) )
	{
		if ( in_array( strtolower ( end ( explode ( ".", $value ) ) ), $extensions) )
		{
			$thumbBox;			
			$thumbnailSRC = "images/image.png";
			if ( ThumbnailExists ( $value ) )
			{
				$thumbBox = $tabs . "\t\t" . "<h2>Thumb BB Code:</h2>" . "\n" . $tabs . "\t\t" . "<input class=\"i_code\" onfocus=\"this.select()\" value=\"" . BBCode ( $value, TRUE ) . "\" />" . "\n";
				$thumbnailSRC = ThumbLink ( $value );
			}

			echo $tabs . "<div class=\"thumb\">" . "\n" .
				 $tabs . "\t" . "<a href=\"" . ImageLink ( $value ) . "\" target=\"_blank\" title=\"Open image: " . $value . "\"><img src=\"" . $thumbnailSRC . "\" alt=\"" . $value . " thumbnail\" /></a>" . "\n" .
				 $tabs . "\t" . "<p class=\"code\">" . "\n" .
				 $thumbBox .
				 $tabs . "\t\t" . "<h2>Direct BB Code:</h2>" . "\n" .
				 $tabs . "\t\t" . "<input class=\"i_code\" onfocus=\"this.select()\" value=\"" . BBCode ( $value ) . "\" /><br />" . "\n" .
				 $tabs . "\t\t" . "<input class=\"check\" type=\"checkbox\" name=\"image[]\" value=\"" . $value . "\" />Select Image" . "\n" .
				 $tabs . "\t" . "</p>" . "\n" .
				 $tabs . "\t" . "<p class=\"clear\"></p>" . "\n" .
				 $tabs . "</div>" . "\n";

			$thumbBox = "";

			$i++;
		}
	}
}

if ( $i == 0 )
{
	echo $tabs . "<div class=\"thumb\">" . "\n" .
		 $tabs . "\t" . "<p class=\"center\"><b>There are no images in this folder!</b></p>" . "\n" .
		 $tabs . "</div>" . "\n";
}

?>
			</form>
		</div>
		<p id="copy">Copyright e<span class="blue">Gallery</span> // All rights reserved.</p>
	</div>
</body>
</html>

You should get a white screen with a number representing the time in seconds to create the thumbnails.

Then time how long it takes on marcs script and post the difference :)
 
Soldato
Joined
12 Jun 2005
Posts
5,361
eGallery now has password support.

Open the index.php and change the $md5Password variable to the md5() function of your password. Current password is "test".

You have to download again here: Click to Download

(Please note: the upload function does not work yet)
 
Last edited:
Soldato
Joined
26 Dec 2003
Posts
16,522
Location
London
I was under the impression that ImageMagick isn't enabled on most servers, so i thought GD would be best?

Would i be wrong in assuming that?

No idea about actual statistics, but it's certainly worth a bash.

marc2003 said:
i'm assuming as this isn't a public gallery and the OP is wanting to resize hundreds of files at a time, rob is suggesting that he should process the files on his pc before he uploads them?

Nope, ImageMagick can be called like any other system command from a PHP script.

Code:
<?php
exec("convert -strip 200x200 foo.jpg foo.thumb.jpg");
?>
 
Soldato
Joined
12 Jun 2005
Posts
5,361
i like it :cool:

uploaded 306 images, no time out :)

but the images doesnt display horizontally for forums

thanks

Did it create the thumbnails for the 306 images ok with no timeout? How long did it take?

Open the index.php file - change line 8 from this:

Code:
$imagesHorizonally = 2

to this:

Code:
$imagesHorizonally = -1

This will display all the images horizontally.
 
Last edited:
Soldato
Joined
12 Jun 2005
Posts
5,361
How would I find out if the server had imagemagick?

I was thinking something along the lines of:

Code:
<?php

if ( exec ( "convert -strip 200x200 foo.jpg foothumb.jpg" ) )
{
	echo "done"; //has imagemagick
}
else
{
	echo "doesn't have imagemagick"; //use gd instead
}

?>
 
Soldato
OP
Joined
29 Jul 2003
Posts
7,668
yeah created 306 thumbnails in 1 go, i didnt time it but i would say around 40 secs, about the same as marc's script.

thanks :)
 
Back
Top Bottom