Weblog Pictures and Gallery
Tuesday, January 13th, 2004I’ve been meaning to add some pictures to my weblog for quite a while so tonight I decided to spend a little time and develop something. I’ve been using Gallery for my photo gallery and I wanted something that would integrate somewhat with it. Here’s what I did:
- I created a photo album in gallery called “Random” (I set the thumbnail size to be 200 for the album).
- I uploaded a bunch of photos that I wanted to appear on the weblog to the “Random” album.
- I wrote a nice little php script that selects a thumbnail of a picture at random. The picture also links to the larger picture. That’s about all there is to it. Here’s the code:
<?php
$d = dir(”../albums/random/”);
while (false !== ($entry = $d->read())) {
if (false !== ($pos = strpos($entry, “.thumb.jpg”))) {
$picArray[] = $entry;
$linkArray[] = substr($entry, 0, $pos);
}
}
srand ((float) microtime() * 10000000);
$pos = array_rand ($picArray,1);
echo “<a
href=”/gallery/view_photo.php?set_albumName=random&id=”.$linkArray[$pos].”" target=”_blank”>”;
echo “<img src=”/”.$d->path.$picArray[$pos].”" border=”0″/></a>”;
$d->close();
?> - I thought it might be nice to add a border to the pictures, to accent them a little. To do that I added a few lines to the style-sheet template in MovableType:
.randomImages {
font-family:verdana, arial, sans-serif;
color:#333;
font-size:x-small;
font-weight:normal;
background:#FFF;
line-height:140%;
padding:2px;
}
.randomImages img {
border: 2px solid #a98c4b;
} - The last step was wrapping my php script in a div tag:
<div class=”randomImages” align=”center”>
…PHP CODE HERE…
</div>
That’s all there was to it. I’m pretty pleased with the results. All I have to do to add more pictures is upload them to the “Random” album using gallery. One of these days maybe I’ll try and integrate it more tightly with Gallery and pull the picture caption also.


