Saturday, May 16, 2009
Project #127 - PHP Image Gallery

I keep most of the images for this website in one folder. This worked well when I posted intermittently, but now with (nearly) daily posts accompanied by multiple pictures it's gotten out of control, especially if I want to reuse older images.
Out of frustration I wrote a quick PHP script that would turn my tangle of pictures into a neat, easily scanned gallery, saving me a ton of time and frustration. There are a lot of images in that folder, so beware that it takes a while to load all the way.
I also added a url parameter so that you can use my script if you have a similar folder content directory. Just type http://www.prime-number.com/see.php??url=your_url into your browser url bar. Here is an example of the parameter in action.
Here is the code. Use at your own risk:
<?PHP
//get $url from parameter or use default
if (isset($_GET['url'])) $url = $_GET['url'];
else $url = 'http://www.prime-number.com/images/?C=M;O=D';
//load url
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$url);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if (empty($buffer)) print "Cannot Access Webpage<p>";
else
{
//set up gallery table
print("<table>");
//remove any parameters from URL
$url_parts = explode("?", $url);
$url_part = $url_parts[0];
//divide by opening html tags
$rows = explode("<", $buffer);
foreach ($rows as $row) {
//check for tags containing links
if (ereg("href", $row)) {
$imgs = explode("\">", $row);
foreach ($imgs as $img) {
if ((ereg("JPG", $img))||(ereg("jpg", $img))||(ereg("gif", $img))){
if (!ereg("href", $img)) {
if($counter % 8 == 0) { print "<TR>"; }
print "<TD><a href=\"".$url_part.$img."\"><IMG SRC=\"".$url_part.$img.
"\" width=\"100\" height=\"100\" border=\"0\"></a></TD>\n";
if($counter % 8 == 7) { print "</TR>"; }
$counter++;
}
}
}
}
}
print("</table>");
}
?>
Labels: coding, projects
posted by Alison 5/16/2009 11:00:00 PM
:
(1) comments :
splink
|