RE: [PHP] Thumbnail Generation from DB Stored Images.
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
[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
RE: [PHP] Thumbnail Generation from DB Stored Images.
Jason-- There are many reasons I don't quite recommend creating the thumbnails on the fly with ImageMagick: 1) I've heard many say that storing/retrieving images from MySQL databases isn't the greatest idea, because you end up with -huge- tables, which leads to long query times. 2) Resizing a JPEG on the fly is a very CPU intensive task 3) Calling the shell, then calling the ImageMagick program is more and more overhead. If you insist on storing the images in the database, then you're best off storing the thumbnails in there too. If you insist on creating thumbnails on the fly, then I suggest you install PHP 4.0.6 with GD 2.0.1 and use ImageCopyResampled() or ImageCopyResized() to do it-- it'll save you from spawning a shell and running an external program. I had to create a Real Estate site, where house listings could be posted with photos to accompany them... When a photo was uploaded (HTTP upload), the script would use ImageCreateTrueColor() to create two new images, then I called ImageCopyResampled() to resize the image to two different sizes-- one thumbnail for search results, then a larger one for listing details. I stored these images in a directory in the format: /images/1_thumb.jpg /images/1_full.jpg And so on... The number (1 in this case) corresponded with the auto_increment ID of the MySQL record for the item. When the listing is deleted, PHP deletes the associated images automatically (if they exist). Good luck! -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]