butsags Posted October 18, 2009 Share Posted October 18, 2009 I was wondering if it was possible and if anyone knew how to make a simple php script that looks at one specified directory and makes <img src="*.*" \> type links out of it? ive got a very nice javascript type image viewer but i cant seem to get it to show every image. and the next step would be getting it to view like 10 images per page but i havent gotten to the time that i need to figure that out yet lol. anyone have any ideas? this is what i have but i would like it to show more than 2 images lol: $extensions = array('jpg','jpeg','gif','png','bmp'); // images folder $images_folder_path = 'images/screenshots/'; // url to folder $url_to_folder = 'http://ingis.com/butsagsms/images/screenshots/'; // Images Array (SRC value) $images = array(); srand((float) microtime() * 10000000); // IF PHP Version < 4.2.0 // Open directory and read images if ($handle = opendir($images_folder_path)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { // get file extension $ext = strtolower(substr(strrchr($file, "."), 1)); if(in_array($ext, $extensions)) { $images[] = $url_to_folder.$file; } } } closedir($handle); } if(!empty($images)) // Do we have something to show? { $rand_key = array_rand($images, 1); $src = $images[$rand_key]; echo " <a id='thumb1' href='".$src."' class='highslide' onclick='return hs.expand(this)'> <img src='".$src."' align='absmiddle' width='100' height='100' border='2px'></a> </a>"; // Show second image; Make sure it will not be the same as the first one; We will remove the element of the first image from the array. This way the script will not reselect it unset($images[$rand_key]); $rand_key = array_rand($images, 1); $src = $images[$rand_key]; echo " <a id='thumb2' href='".$src."' class='highslide' onclick='return hs.expand(this)'> <img src='".$src."' align='absmiddle' width='100' height='100' border='2px' style='padding-left:3px;'> </a>"; } else { echo '<small>No images found</small>'; } if i set the array to say 6 and then i dont have 6 images , ill get a error so is there a way to present as many images as are in the directory? Quote Link to comment https://forums.phpfreaks.com/topic/178136-solved-simple-image-grab-script/ Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 You can use the DirectoryIterator for that. Quote Link to comment https://forums.phpfreaks.com/topic/178136-solved-simple-image-grab-script/#findComment-939230 Share on other sites More sharing options...
butsags Posted October 18, 2009 Author Share Posted October 18, 2009 thats a pretty good resource actually. i think i have most of what i need already in the above script. i think i just need a simple script that shoots out however many images are there. something like : if(!empty($images)) { $rand_key = array_rand($images, !); $src = $images[$rand_key]; echo " <a id='thumb1' href='".$src."' class='highslide' onclick='return hs.expand(this)'> <img src='".$src."' align='absmiddle' width='100' height='100' border='2px'></a> </a>"; } would that work? sorry im very new at php Quote Link to comment https://forums.phpfreaks.com/topic/178136-solved-simple-image-grab-script/#findComment-939246 Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 Something simple like this should do it: <?php $path = '/images/'; $numImages = 0; foreach (new DirectoryIterator('/path/to/images') as $item) { if ($item->isFile() && in_array(pathinfo($item->getPathname(), PATHINFO_EXTENSION), array('png', 'jpg', 'gif'))) { echo "<img src='" . $path . $item->getFilename() . "'>"; ++$numImages; } } printf('There are %d images.', $numImages); Quote Link to comment https://forums.phpfreaks.com/topic/178136-solved-simple-image-grab-script/#findComment-939257 Share on other sites More sharing options...
butsags Posted October 18, 2009 Author Share Posted October 18, 2009 hey thats a great start but when i tried it , it only jumped to teh print function and tells me the amount of images there are in the directory. lol maybe the img src part is wrong? it deffinately sees the correct directory tho in order to tell how many images. Quote Link to comment https://forums.phpfreaks.com/topic/178136-solved-simple-image-grab-script/#findComment-939269 Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 If it counts correctly then it gets inside the if. In that case you'll just have to adjust the echo and the $path variable at the top. Quote Link to comment https://forums.phpfreaks.com/topic/178136-solved-simple-image-grab-script/#findComment-939274 Share on other sites More sharing options...
butsags Posted October 18, 2009 Author Share Posted October 18, 2009 yep your right i was missing 1 slash after the directory name haha thank you so much thats a very big help. 1 last thing/ would you happen to know how to make them group into pages of say 15 for example? wouldnt want extraneous images making the page miles long Quote Link to comment https://forums.phpfreaks.com/topic/178136-solved-simple-image-grab-script/#findComment-939280 Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 If you want to do that it would be wiser storing the information in a DBMS. Otherwise, the regular pagination techniques for databases would apply here as well: use an offset and a limit. It would quickly become inefficient without a DBMS. Quote Link to comment https://forums.phpfreaks.com/topic/178136-solved-simple-image-grab-script/#findComment-939283 Share on other sites More sharing options...
butsags Posted October 18, 2009 Author Share Posted October 18, 2009 DBMS= database management system? i am using mysql for the website but mysql doesnt store images but i think i know what you mean. thanks for all your help Solved Quote Link to comment https://forums.phpfreaks.com/topic/178136-solved-simple-image-grab-script/#findComment-939286 Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 DBMS= database management system? Yep. i am using mysql for the website but mysql doesnt store images but i think i know what you mean. thanks for all your help Actually, it can store binary files (like images), but I just meant that you should store the path to the file. Quote Link to comment https://forums.phpfreaks.com/topic/178136-solved-simple-image-grab-script/#findComment-939296 Share on other sites More sharing options...
butsags Posted October 18, 2009 Author Share Posted October 18, 2009 not a bad idea/ ill look into it. thanks Quote Link to comment https://forums.phpfreaks.com/topic/178136-solved-simple-image-grab-script/#findComment-939318 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.