User:Meyermagic Salome

From Second Life Wiki
Jump to navigation Jump to search

About me

Name: Meyer S. Jacobs

Alias: Meyermagic, if(x), |-1|

Age: 472794735 as of 1175103075 (seconds since the Unix Epoch)

Avoids: Carbonated drinks, dressing up, political parties, and organized religion

Is drawn to: Math, physics, technology, interesting people, old cartoons, sci-fi, anime, good books, things that spin, magnets, food

Projects

Perception - A four part story
⇒ Part One: The Crystals of Power
⇒ Part Two: The Tower Archive
⇒ Part Three: Perpetual*
⇒ Part Four: Transcend

The WeFiki - A next generation web protocol

null_Void - My personal site; not yet online

Some code

My PHP image gallery scripts: config.php:

<?php
		/*General Data*/
	//$main_template = "template.html"
	//$template_base_prefix = "base_";
	//$pages = array('home', array('visual', 'galleries'), 'blog', array('composition', 'galleries'), array('programming', 'galleries'), 'about', 'links');
	
		/*Image Gallery Data*/
	$img_dir = 'visual';
	$separator = '!~.~^~.~!';
	$thumbXsize = 128; //128
	$thumbYsize = 96; //96
	$width = 4;//Table Columns
	
		/*Composition Gallery Data*/
		
		/*Other Data*/
		
		/*Functions*/
	//We have a little bit of trouble with layered .png files; the thumbs always seem to come out blank
	//Also, there is no .bmp support
	//Oh yeah! I'm planning on adding the ability to specify a set of thumbnail rules. Those might include alternate center, rotations, and checks for complexity of thumb
	function create_thumb($source, $dest, $sizeX, $sizeY)
	{
		if(($source_data = getimagesize($source)) !== false)
		{
			$startX = ($source_data[0] / 2) - ($sizeX / 2);
			$startY = ($source_data[1] / 2) - ($sizeY / 2);
			
			if($source_data[2] == 1)
			{	//Apparently imagecreatetruecolor won't work with gif images
				$thumb = imagecreate($sizeX, $sizeY);
				$imgsource = imagecreatefromgif($source);
				imagecopymerge($thumb, $imgsource, 0, 0, $startX, $startY, $sizeX, $sizeY, 100);
				imagegif($thumb, $dest);
			}
			elseif($source_data[2] == 2)
			{
				$thumb = imagecreatetruecolor($sizeX, $sizeY);
				$imgsource = imagecreatefromjpeg($source);
				imagecopymerge($thumb, $imgsource, 0, 0, $startX, $startY, $sizeX, $sizeY, 100);
				imagejpeg($thumb, $dest, 100);
			}
			elseif($source_data[2] == 3)
			{
				$thumb = imagecreatetruecolor($sizeX, $sizeY);
				$imgsource = imagecreatefrompng($source);
				imagecopymerge($thumb, $imgsource, 0, 0, $startX, $startY, $sizeX, $sizeY, 100);
				imagepng($thumb, $dest);
			}
		}
	}

	function imagelist_create($dir, $continue, $thumbX, $thumbY, $parseMark)
	{
		$image_array = array();
		if(is_dir($dir) && ($dh = opendir($dir)))
		{
			while(($object = readdir($dh)) !== false)
			{	//If its an image file...
				if(is_file($dir.'/'.$object) && $object !='.' && $object != '..' && ($imagetype = getimagesize($dir.'/'.$object)) !== false)
				{	//...with either gif, png, or jpeg type...

					$imagetype = $imagetype[2];
					$image_mimetype = image_type_to_mime_type($imagetype);
					if(substr($image_mimetype, 6) == 'gif' || substr($image_mimetype, 6) == 'png' || substr($image_mimetype, 6) =='jpeg')
					{
						
						//...add it to the image array
						if(!file_exists($dir.'/image_data/'.substr($object, 0, -3).'txt'))
						{	//If there is no image data file, default display name to image name
							$image_displayname = substr($object, 0, -4);
						}
						else
						{
							$image_data = file_get_contents($dir.'/image_data/'.substr($object, 0, -3).'txt');
							$image_displayname = explode($parseMark, $image_data);
							$image_displayname = $image_displayname[0];
						}
						$image_array[$dir][filemtime($dir.'/'.$object)] = array($object, $image_mimetype, $image_displayname, $dir);
						
						//Eventually, I'll have it call ksort or krsort, depending on preference
						krsort($image_array[$dir], SORT_NUMERIC);//Sort by date modified
						$image_array[$dir] = array_values($image_array[$dir]);//Renumber sorted array
						
						if(!file_exists($dir.'/thumbs/th_'.$object))
						{	//If there isn't a thumbnail, create one using the thumbnail specifications from config.php
							$dir_array = explode('/', $dir);
							create_thumb($dir.'/'.$object, $dir_array[0].'/thumbs/th_'.$object, $thumbX, $thumbY);
						}
						else
						{
							$image_data = getimagesize($dir.'/thumbs/th_'.$object);
							if($image_data[0] != $thumbX || $image_data[1] != $thumbY)
							{	//Make sure the thumb, if there already was one, has the correct sizes
								unlink($dir.'/thumbs/th_'.$object);
								$dir_array = explode('/', $dir);
								create_thumb($dir.'/'.$object, $dir_array[0].'/thumbs/th_'.$object, $thumbX, $thumbY);
							}
						}
					}
				}
//If you see a directory inside the root image directory, besides the "thumbs" and "image_data" directories, run this function again, on that directory, with continue FALSE
				if(is_dir($dir.'/'.$object) && $object != 'thumbs' && $object != 'image_data' && $continue  && $object !='.' && $object != '..')
				{
					$image_array += imagelist_create($dir.'/'.$object, false, $thumbX, $thumbY, $parseMark);
				}
			}
			closedir($dh);
		}
		return $image_array;
	}
	
?>

Example usage in visual.php:

<?php
	include('config.php');

	/*
	Okie dokie. I still need to:
		Add .bmp support to thumbnail generator
		Add unused thumbnail check
		Add additional thumbnail generation features
			Alternate Center Cropping
			Scale + Crop
			Rotate + Crop
			Thumbnail complexity check (Uses rotation and alternate center to assure thumbnail contains at least a given number of unique pixels)
		Add multiple sort setting
			Alphabetical
			Date Modified
			User defined order preference
		Try to find a way around the layered .png bug
			<Update>It appears that the bug is not in the layers, as they are not present in all the images, but rather that <b><i>most of the images have alpha channel backgrounds</i></b>, with black content. <b><i>The black content does not appear against the page's black background.</i></b></Update>
				--Another update-- The clear areas read black
		Find some way to deal with image titles longer than one thumbnail's width
		Incorporate gallery with Tom's slideshow code (This will require some modification on both sides)
		Have image data be stored in a .xml file, just to be fancy (And to gain experience with php's XML functions)
		
		-As an unrelated note, don't forget the php optimizing script idea (I thought I might be in the programming mood after reading this)-
		
		Oh! Tidy is awesome, but kind of sucks when I want verbatim output. I wish you could set some directive at the beggining of the script to define wether or not the output should be repaired by Tidy.  : )
		$meta_thought = "I geuss I could load the extension manually, but I'm not sure if I can turn on and off auto-tidy at the same time, as that is specified the php.ini file";
	*/
	
	//Load Data
	$base = implode("", file("base_visual.html"));
	$template = implode("", file("template.html"));
	$thumb_temp_base = implode("", file("visual_thumbTemp.html"));
	
	//Prepare table
	$base_insert = '';
	
	/*
	List Format:
	$images[Gallery][Image_Number]
	Returns:
	array(Image_Filename, Image_Type, Image_Displayname, Image_Gallery);
	*/
	
	$images = array_values(imagelist_create($img_dir, true, $thumbXsize, $thumbYsize, $separator));
	$gallery_count = count($images);
	$current_gallery;
	
	
	
	/*
	Note for template-based configuration; visual.php currently parses the following terms as described after each one
		IMAGE_DIRECTORY - The path to the image
		IMAGE_TITLE - The image's title, as described in the imagedata file, or, if the imagedata file is not present, the image's filename without the extension
		THUMB_SOURCE - The image thumbnail path and filename; For example, 'visual/thumbs/th_someimage.jpg'
		SLIDE_NUMBER - The image's location in the image list, as described by the following formula: 'Slide_Number = Current_Row * Row_Width + Current_Column'
		IMAGE_FILENAME - The source image's filename
		GALLERY_TITLE - The current gallery in 'title form'; that is, the directory, with forward slashes, '/', replaced by '->'
	*/
	
	
	$thumb_temp = $thumb_temp_base;//Set working thumbnail template
	for($current_gallery = 0; $current_gallery < $gallery_count; $current_gallery++)
	{	//More of the formatting, including the location of gallery titles and other structure data, will be migrated to a template file
		$base_insert .= '<p>GALLERY_TITLE</p>';
		$base_insert .= '<table class="gallery">';
		$extra_images = count($images[$current_gallery]) % $width; //Extra images
		$full_rows = (count($images[$current_gallery]) - $extra_images) / $width; //Number of full rows
		$current_row;
		for($current_row = 0; $current_row < $full_rows; $current_row++)
		{
			$base_insert .= '<tr>';
			$current_column;
			for($current_column = 0; $current_column < $width; $current_column++)
			{
				$base_insert .= '<td><center>';
				$ImageNumber = ($current_row * $width) + $current_column;//Calculate image position in array
				$ImageData = $images[$current_gallery][$ImageNumber];
				$ImageDirectory = $ImageData[3];
				$ThumbSource = $img_dir.'/thumbs/th_'.$ImageData[0];
				$ImageTitle = $ImageData[2];
				
				//Start the replacing
				$thumb_temp = str_replace("IMAGE_DIRECTORY", $ImageDirectory, $thumb_temp);
				$thumb_temp = str_replace("IMAGE_TITLE", $ImageTitle, $thumb_temp);
				$thumb_temp = str_replace("THUMB_SOURCE", $ThumbSource, $thumb_temp);
				$thumb_temp = str_replace("SLIDE_NUMBER", $ImageNumber, $thumb_temp);
				$thumb_temp = str_replace("IMAGE_FILENAME", $ImageData[0], $thumb_temp);
				
				$base_insert .= $thumb_temp;
				$base_insert = str_replace("GALLERY_TITLE", str_replace("/", " -> ", $ImageDirectory), $base_insert);
				$base_insert .= '</center></td>';
				$thumb_temp = $thumb_temp_base;//Revert thumbnail template for next generation
			}	
			$base_insert .= '</tr>';
		}
		if($extra_images > 0)//Slightly modified generation for incomplete image rows
		{
			$current_row = $full_rows;
			$base_insert .= '<tr>';
			for($current_column = 0; $current_column < $extra_images; $current_column++)
			{							   //The difference is right ^here^. I'm ashamed to have this as a separate block of code.
				$base_insert .= '<td><center>';
				$ImageNumber = ($current_row * $width) + $current_column;
				$ImageData = $images[$current_gallery][$ImageNumber];
				$ImageDirectory = $ImageData[3];
				$ThumbSource = $img_dir.'/thumbs/th_'.$ImageData[0];
				$ImageTitle = $ImageData[2];
				$thumb_temp = str_replace("IMAGE_DIRECTORY", $ImageDirectory, $thumb_temp);
				$thumb_temp = str_replace("IMAGE_TITLE", $ImageTitle, $thumb_temp);
				$thumb_temp = str_replace("THUMB_SOURCE", $ThumbSource, $thumb_temp);
				$thumb_temp = str_replace("SLIDE_NUMBER", $ImageNumber, $thumb_temp);
				$thumb_temp = str_replace("IMAGE_FILENAME", $ImageData[0], $thumb_temp);
				$base_insert .= $thumb_temp;
				$base_insert = str_replace("GALLERY_TITLE",  str_replace("/", " -> ", $ImageDirectory), $base_insert);
				$base_insert .= '</center></td>';
				$thumb_temp = $thumb_temp_base;
			}
			$base_insert .= '</tr>';
		}
		$base_insert .= '</table>';
		$base_insert .= '<p>-----------------------------------</p>';
	}
	
	
	//Perform Substitution
	$template = str_replace("SECTION_TITLE", "Visual", $template);
	$template = str_replace(' _<a href="visual.php">Visual</a><br />', ' #<strong>Visual</strong><br />', $template);
	$base = str_replace("IMAGE_GALLERY", $base_insert, $base);
	
	//Print
	print(str_replace("PAGE_CONTENT", $base, $template));
	
?>

Along with the file base_visual.html:

IMAGE_GALLERY

And template.html:

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<link rel="icon" type="image/png" href="0.png" />
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
		<link rel="stylesheet" type="text/css" href="style.css" />
		<title>Welcome to The Void ~::~ SECTION_TITLE</title>
	</head>
	<body>
		<table align="center" cellspacing="10" cellpadding="0" width="760" class="maintable">
			<tr>
				<th colspan="2" class="header">
					<img src="null_Void.png" alt="null_Void" />
				</th>
			</tr>
			<tr>
				<td width="190" valign="top" rowspan="2" class="sidebar">
					<div align="center">
						<p>Navigation</p>
						<p align="left">
							_<a href="home.php">Home</a><br />
						</p>
						<p align="left">
							 Galleries:<br />
							 _<a href="visual.php">Visual</a><br />
							 _<a href="composition.php">Composition</a><br />
							 _<a href="programming.php">Programming</a>
						</p>
						<p align="left">
							_<a href="blog.php">Blog</a><br />
							_<a href="about.php">About</a><br />
							_<a href="links.php">Links</a>
						</p>
						<p>
							<a href="http://validator.w3.org/check?uri=referer">Valid XHTML 1.0 Transitional</a>
						</p>
					</div>
				</td>
				<td width="570" valign="top" class="content">
					<div align="center">
						<p>-----------------------------------</p>
						<p>--SECTION_TITLE--</p>
						PAGE_CONTENT
					</div>
				</td>
			</tr>
			<tr>
				<td valign="bottom" class="footer">© 2006 - 2010, All Rights Reserved.</td>
			</tr>
		</table>
	</body>
</html>

CC-GNU GPL
This software is licensed under the CC-GNU GPL.


Notes

  • *Title tentative