vetman Posted June 3, 2008 Share Posted June 3, 2008 I have a script to read images from a directory and echo them to the screen. My question is, is there a way to save the file name to a mysql database table along with its thumbnail image file name so I can add comments to the database for each picture to be shown when I show the large image? Any help would be appreciated! Link to comment https://forums.phpfreaks.com/topic/108605-help-needed/ Share on other sites More sharing options...
marklarah Posted June 3, 2008 Share Posted June 3, 2008 Its not quite clear what you mean, but if you have the images stored on your server, just store the url to it each as a record, then echo <img src="<?=$row['url']?>"> or whatever, and for the thumbnails, do the same, using the height="" and width="". Link to comment https://forums.phpfreaks.com/topic/108605-help-needed/#findComment-556994 Share on other sites More sharing options...
marklarah Posted June 3, 2008 Share Posted June 3, 2008 Just to add, make sure each record has a UID, and then in your comments table, have three rows, the comment ID, the text of it, and the ID of the image it refers to in the image table. Link to comment https://forums.phpfreaks.com/topic/108605-help-needed/#findComment-556996 Share on other sites More sharing options...
vetman Posted June 3, 2008 Author Share Posted June 3, 2008 Okay, I do have the images stored in a directory, I can view them on the webpage. I was wondering if it would be possible to write a php script to read the directory, convert the file names so I can save them to a table so that I then can add the comments to the table. I guess you are saying that I can do it manually, which I have done for another directory. I was looking to make it a little easier for me down the line. Thanks for the info! Link to comment https://forums.phpfreaks.com/topic/108605-help-needed/#findComment-557025 Share on other sites More sharing options...
chronister Posted June 3, 2008 Share Posted June 3, 2008 If you have a script to read the dir, and echo out on screen, then you have everything you need to simply write a query to take the output and write to the db. If ya post some code, we can probably help more. Link to comment https://forums.phpfreaks.com/topic/108605-help-needed/#findComment-557034 Share on other sites More sharing options...
vetman Posted June 4, 2008 Author Share Posted June 4, 2008 Okay, here is the code, it works fine, makes the thumbnails as it runs thru the directory. <?php $columns = 4; $thmb_width = 200; $thmb_height = 128; function resizeImage($originalImage,$toWidth,$toHeight){ // Get the original geometry and calculate scales list($width, $height) = getimagesize($originalImage); $xscale=$width/$toWidth; $yscale=$height/$toHeight; // Recalculate new size with default ratio if ($yscale>$xscale){ $new_width = round($width * (1/$yscale)); $new_height = round($height * (1/$yscale)); } else { $new_width = round($width * (1/$xscale)); $new_height = round($height * (1/$xscale)); } // Resize the original image $imageResized = imagecreatetruecolor($new_width, $new_height); $imageTmp = imagecreatefromjpeg ($originalImage); imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height); return $imageResized; } function generateThumbnails(){ global $thmb_width,$thmb_height; // Open the actual directory if ($handle = opendir(".")) { // Read all file from the actual directory while ($file = readdir($handle)) { // Check whether tha actual item is a valid file if (is_file($file)){ // Check whether the actual image is a thumbnail if (strpos($file,'_th.jpg')){ $isThumb = true; } else { $isThumb = false; } if (!$isThumb) { // Process the file string $dirName = substr($file,0,strpos($file,basename($file))); if (strlen($dirName) < 1) $dirName = '.'; $fileName = basename($file); $fileMain = substr($fileName,0,strrpos($fileName,'.')); $extName = substr($fileName,strrpos($fileName,'.'), strlen($fileName)-strrpos($fileName,'.')); // Check if the actual file is a jpeg image if (($extName == '.jpg') || ($extName == '.jpeg')){ $thmbFile = $dirName.'/'.$fileMain.'_th.jpg'; // If a thumbnail dosn't exists tahn create a new one if (!file_exists($thmbFile)){ imagejpeg(resizeImage($file,$thmb_width,$thmb_height) ,$thmbFile,80); } } } } } } } function getNormalImage($file){ $base = substr($file,0,strrpos($file,'_th.jpg')); if (file_exists($base.'.jpg')) return $base.'.jpg'; elseif (file_exists($base.'.jpeg')) return $base.'.jpeg'; else return ""; } function displayPhotos(){ global $columns; generateThumbnails(); $act = 0; // Open the actual directory if ($handle = opendir(".")) { // Read all file from the actual directory while ($file = readdir($handle)) { // Check whether tha actual item is a valid file if (is_file($file)){ // Check whether the actual image is a thumbnail if (strpos($file,'_th.jpg')){ ++$act; if ($act > $columns) { echo '</tr><tr> <td class="photo"><a href="'.getNormalImage($file).'"> <img src="'.$file.'" alt="'.$file.'"/></a></td>'; $act = 1; } else { echo '<td class="photo"><a href="'.getNormalImage($file).'"> <img src="'.$file.'" alt="'.$file.'"/></a></td>'; } } } } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html> <head> <title>Photo Gallery</title> </head> <body> <center><h3>Photo Gallery</h3></center> <table align="center"><tr> <?php displayPhotos(); ?> </table> </body> </html> Link to comment https://forums.phpfreaks.com/topic/108605-help-needed/#findComment-557108 Share on other sites More sharing options...
vetman Posted June 4, 2008 Author Share Posted June 4, 2008 Okay, I posted the code I'm using, now I get no repsonse. What's up with that? Link to comment https://forums.phpfreaks.com/topic/108605-help-needed/#findComment-557941 Share on other sites More sharing options...
chronister Posted June 5, 2008 Share Posted June 5, 2008 Sorry, been busy with work:) In this chunk of code... <?php function displayPhotos(){ global $columns; generateThumbnails(); $act = 0; // Open the actual directory /****************************************** CREATE A QUERY TO READ DB & RETURN AN ARRAY OF FILENAMES */ if ($handle = opendir(".")) { // Read all file from the actual directory while ($file = readdir($handle)) { // Check whether tha actual item is a valid file if (is_file($file)){ /*********************************** $file is the actual file ... echo it to see exactly what it contains */ if(in_array($FILE_ARRAY_FROM_QUERY)) { //file is already in db... display it or do what you will } else { //file don't exist, write it to the db. } // Check whether the actual image is a thumbnail if (strpos($file,'_th.jpg')){ ++$act; if ($act > $columns) { echo '</tr><tr> <td class="photo"><a href="'.getNormalImage($file).'"> <img src="'.$file.'" alt="'.$file.'"/>[/url]</td>'; $act = 1; } else { echo '<td class="photo"><a href="'.getNormalImage($file).'"> <img src="'.$file.'" alt="'.$file.'"/>[/url]</td>'; } } } } } } ?> thats a very basic way... bedtime now... post back with questions if ya need. Nate Link to comment https://forums.phpfreaks.com/topic/108605-help-needed/#findComment-558072 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.