Category archive: php

Super simple PHP gallery 3

Just save this as index.php and put it in a directory with a bunch of images. Images are displayed one per page (newest images first), and clicking on an image takes you to the next one.

<?
exec("ls -r|egrep -i \"\.(gif|jpg|jpeg|png)$\"", $files);
$files = array_reverse($files);
$request = $_SERVER["QUERY_STRING"];
if(!(is_numeric($request) && $request > 0 && $files[$request - 1])){
	$request = count($files);
}
$r_index = $request - 1;
$next = $r_index;
if($next==0 || !$files[$next-1]){
	$next = count($files);
}
?>
<html>
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8">
	<title>my image gallery</title>
 
	<style type="text/css" media="screen">
		a img{ border:0; }
	</style>
 
</head>
<body>
	<table border="0" cellspacing="0" cellpadding="0" width="100%" height="100%">
		<tr><td align="center" valign="middle"><a href="?<?=$next?>"><img src="<?=$files[$r_index]?>" /></a></td></tr>
	</table>
</body>
</html>

Bookflipper 0

Bookflipper is a tiny web app that serves up pricing information for used books from the Amazon API. My friend Brian has been picking up cheap books and selling them on Amazon for a profit. Now when he’s wondering if a purchase is going to pay off, he can just pull out his cell phone and enter the book’s ISBN into Bookflipper to see what the market is like.

Example 1
Example 2
Example 3

The page size is usually between 0.5-0.6k, thanks in part to some output buffer trickery. Of course, the preview image is much bigger but it seemed too useful to get rid of.

You can find it on the plugins download page now. This basically just gives you a function that will print out a snippet of text like “Viewing 1-10 of 423 posts” based on the contents of the page. Hopefully this is marginally useful to someone out there.

Reducing HTML output size 7

I recently found myself writing a small web app for my friend Brian to use on his cell phone, one of the last places where page size really matters. Since every character counts, we want to keep our page structure lean and mean (probably a good idea on a mobile device anyway). But after that there’s a lazy, completely pain-free step we can take: stripping all of the redundant whitespace from our HTML.

With PHP’s output buffer handlers we can just set it and forget it:

function clean_html($output){
	$output = preg_replace("/\n/", "", $output);
	$output = preg_replace("/\t/", "", $output);
	$output = preg_replace("/\s+/", " ", $output);
	return $output;
}
 
ob_start('clean_html');
 
//a bunch of stuff that prints out html
 
ob_flush();

This will give use one long, ugly line of dense markup, but what do we care? It’s still valid and the browser handles it just fine. This seems to reduce page size by about 10-20% in this particular application.

PHP relative time function 0

I couldn’t find a PHP function that would take a number of seconds as input and give me something like “3 weeks and 2 days” so I put this together:

function rel_time($seconds){
	if($seconds < 60){
		if($seconds < 0){ $seconds = 0; }
		switch($seconds){
			case 1:
				return "1 second";
				break;
			default:
				return "$seconds seconds";
				break;
		}
	}else{
		$date_push = array();
		$time_units = array(	'year'		=> (365*24*60*60),
					'month'		=> (30*24*60*60),
					'week'		=> (7*24*60*60),
					'day'		=> (24*60*60),
					'hour'		=> (60*60),
					'minute'	=> (60));
		foreach($time_units as $unit=>$unit_time){
			$total = 0;
			if($unit=='day' && count($date_push) && ($seconds < $time_units['day'])){
				$seconds = 0;
			}
			while($seconds >= $unit_time){
				$seconds -= $unit_time;
				$total++;
			}
			switch ($total){
				case 0:
					break;
				case 1:
					$date_push[] = "1 $unit";
					break;
				default:
					$date_push[] = "$total {$unit}s";
					break;
			}
			if(count($date_push) == 2){
				break;
			}
		}
		return implode(" and ", $date_push);
	}
}

Example usage:

$rel_time  = rel_time(time() - $timestamp);
print "Posted $rel_time ago";

It’s not as efficient as it could be, but it gets the job done for now. And it’s pretty easy to configure it for the time units that you want to use.



My name is Joey Nelson. I'm a web developer living in Raleigh, NC and working with Node.js, MongoDB, Ruby, PHP, jQuery and some other stuff. More about me.