Here is my code to turn a directory full of *files*
into thumbnail images. im sure you can adapt it to
your own needs... the code is a bit long-winded, but
it was my first attempt at writing thumbnail code,
so forgive me ;)
// example call to the function
make_thumbs('./pictures/cavern/', './pictures/cavern/thumbs', 200, 60 );
/*
Simple function to make thumbnail images for an
entire directory of .JPG images. (no GIF support
because the owners of the GIF patent are buttheads)
It will create square thumbnail images (height=width),
with the original image resized properly within the
thumbnail dimensions to preserve the height to width ratio
This code is free for use as long as you keep my
email and URL in here:
[EMAIL PROTECTED]
[EMAIL PROTECTED]
http://furt.com/code/make_thumbs/
Syntax of function call
make_thumbs(
$dir = where the original image files are located
$tdir = where you want the thumbnails to be saved
$max = $max=width=height of the new thumbnail images
all thumbnails are square.
$quality = quality of thumbnail JPG worst(1..100)best
$border = how many pixels of border to put around the image
);
*/
function make_thumbs($dir, $tdir, $max, $quality=75, $border=3) {
// this is so the script won't time out
set_time_limit(0);
$files = opendir($dir) or die("Cannot opendir $dir");
while ($file = readdir($files)) {
if ( preg_match("/[a-zA-Z0-9]\.(jpg|jpeg|JPG|JPEG)$/", $file) ) {
//if (!file_exists($tdir .'/'. $file .'_t.jpg')) {
// Information about the original image
$img_name = $dir .'/'. $file;
$img = imagecreatefromjpeg($img_name) or die("Cannot read
$img_name");
$img_x = $img_y = 0;
$img_w = imagesx($img);
$img_h = imagesy($img);
// Create a new empty container for the thumbnail image
$out = imagecreate($max, $max);
// Set background colour of the thumbnail container
$bg = ImageColorAllocate($out, 200,200,200);
// If image is more wide than tall
if ($img_w > $img_h) {
$mod = ($img_w - $max) / $img_w;
$out_w = $max;
$out_h = $img_h * (1 - $mod);
$out_x = 0;
$out_y = ($max - $out_h) / 2;
$out_x += $border;
$out_w -= $border*2;
}
// If image is more tall than wide
else {
$mod = ($img_h - $max) / $img_h;
$out_h = $max;
$out_w = $img_w * (1 - $mod);
$out_y = 0;
$out_x = ($max - $out_w) / 2;
$out_y += $border;
$out_h -= $border*2;
}
// for debugging purposes... so i can see what's going on
//print "out_x=$out_x, out_y=$out_y, img_x=$img_x, img_y=$img_y\n";
//print "out_w=$out_w, out_h=$out_h, img_w=$img_w, img_h=$img_h\n";
//Put some text into the image
//$fg = ImageColorAllocate($out, 0,0,0);
//imagestring ($out, '2', 3, $max-15, "Text", $fg);
// Copy and resize the original image into the new thumbnail container
imagecopyresized (
$out, $img,
$out_x, $out_y, $img_x, $img_y,
$out_w, $out_h, $img_w, $img_h);
// Save the image to disk (in the thumbnail directory)
imagejpeg($out, $tdir .'/'. $file .'_t.jpg', $quality);
//}
}#if
}#while
}
> -----Original Message-----
> From: Jason Bell [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, July 12, 2001 7:27 PM
> To: [EMAIL PROTECTED]
> Subject: [PHP] Thumbnail Generation from DB Stored Images.
>
>
> Hello! I have images stored in a MySQL Database. I can retrieve the images
> and display them without a problem, but I'd really like to be able to
> create thumbnails. The code that I've written just doesn't seem to work.
> Preferably, I'd like to pull the image from the database, and create the
> thumbnail for display, without ever writing an image to the disk. If this
> is not possible, I'd like to know alternate methods. I'm using
> imagemagick's convert utility to create the thumbs. Here is my code:
>
> getpic.php:
>
> <?php
> if($id) {
> @MYSQL_CONNECT("localhost","username","mypassword");
> @mysql_select_db("daelic");
> $query = "select image,type from images where id=$id";
> $result = @MYSQL_QUERY($query);
> $data = @MYSQL_RESULT($result,0,"image");
> $type = @MYSQL_RESULT($result,0,"type");
> Header( "Content-type: $type");
> echo $data;
> };
> ?>
>
> tgen.php:
>
> <?php
> if ($id) {
> $src = getpic.php?id=$id;
> $thumb = passthru("convert -geometry 40% $src jpeg:-");
> print $thumb;
> };
> ?>
>
>
> I know that getpic.php works. I can use it in like this: echo "<IMG
> SRC='getpic.php?id=$id'>"; and it will output the fullsize graphic in
> place. I'm trying to get to a point where I can use tgen.php in the same
> mannor. I can use the same convert command if I use an actual file, like
> pics/mypic.jpg and it works, so the command is good.....
>
> any help at all would be very appreciated,
>
> Jason Bell
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]