Soldato
- Joined
- 16 Nov 2003
- Posts
- 9,682
- Location
- On the pale blue dot
It's getting a pain generating thumbnails beforehand for my website so I've modified my gallery code to generate them on the fly using GD. This code will then be embedded in a Wordpress page so no complaints about not having the full <HTML>, <BODY>, etc. tags.
I think it's all okay as it works but I'm sure I've done something naughty, missed some steps etc. as it's been a while since I've touched any PHP. What do you think? Here is a sample link and the code:
http://www.sidtheturtle.co.uk/chaos/gallery/gallery.php?galname=halo3&repeat=2&width=200
gallery.php
thumbnail.php
I think it's all okay as it works but I'm sure I've done something naughty, missed some steps etc. as it's been a while since I've touched any PHP. What do you think? Here is a sample link and the code:
http://www.sidtheturtle.co.uk/chaos/gallery/gallery.php?galname=halo3&repeat=2&width=200
gallery.php
PHP:
<?php
// Check if a gallery has been specifed, if not exit cleanly.
if (isset($_GET["galname"]))
{
$galname = $_GET["galname"];
}
else
{
exit("Error: No gallery specified.");
}
// Check if specified gallery exists, if not exit cleanly.
if (is_dir("./$galname") != TRUE)
{
exit("Error: Specified gallery does not exist.");
}
// Check if a number of items before a new row is added is specified, if not exit cleanly.
if (isset($_GET["repeat"]))
{
$repeat = $_GET["repeat"];
}
else
{
exit("Error: Row repeat value not specified");
}
// Check if a width for the thumbnails is specified, if not exit cleanly.
if (isset($_GET["width"]))
{
$width = $_GET["width"];
}
else
{
exit("Error: Thumbnail width value not specified");
}
// Initialise row length counting variable.
$count = 0;
// Produce gallery
foreach (glob("./$galname/{*.jpg}", GLOB_BRACE) as $image)
{
$count++;
echo "<a href=\"" . $image . "\">";
echo "<img src=\"thumbnail.php?width=" . $width . "&image=" . $image . "\">";
echo "</a>";
if ($count == $repeat)
{
echo "<p>";
$count = 0;
}
else
{
echo " ";
}
}
?>
thumbnail.php
PHP:
<?php
if (isset($_GET["width"]))
{
$width = $_GET["width"];
}
else
{
exit("Error: Thumbnail width value not specified");
}
if (isset($_GET["image"]))
{
$image = $_GET["image"];
}
else
{
exit("Error: Source image not specified");
}
header("Content-type: image/jpeg");
$sourceimage=imagecreatefromjpeg($image);
$x = imageSX($sourceimage);
$y = imageSY($sourceimage);
$ratio = $x / $width;
$dX = $width;
$dY = $y / $ratio;
$thumbnail=ImageCreateTrueColor($dX,$dY);
imagecopyresampled($thumbnail,$sourceimage,0,0,0,0,$dX,$dY,$x,$y);
imagejpeg($thumbnail,NULL,100);
$imagedestroy($sourceimage);
$imagedestroy($thumbnail);
?>
Last edited: