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: ,

posted by Alison 5/16/2009 11:00:00 PM : (1) comments : splink



prime-number.com

site feed

Archives:

Powered by Blogger



www.flickr.com
This is a Flickr badge showing items in a set called DIY. Make your own badge here.


My Rules for the year:
• Make something new every day.
• Don't buy anything you can make yourself, especially clothes.

Elsewhere:
Bio
Graphics Homework
Pictures
Email Me

Very Old Flash Homework:
Old Flash Page
Aquarium
KiteBuilder
Shapes

I belong to:
WeddingBee
Metafilter

Weblogs Written By Nice People:
» Munsoned in the Middle of Nowhere
» Ben Bonetti
» romej.com
» The Rector's Page
» A Dress A Day
» The Point of Babette

Old Versions:
Fall Leaves
Early to Rise
Chopstick
Equine


Old Splash Pages:
Old Splash Pages:
»Original Recipe
»Sketch of Clark
»Big Blob
»Another Big Blob
»Digi Charat Bear
»Linoleum Samples




Google



Search WWW
Search www.prime-number.com



Current Weather in Pittsburgh, PA: The WeatherPixie