dras Posted January 24, 2018 Share Posted January 24, 2018 (edited) natural sort of directory <?php $images_folder = "./corvette/"; $files = array(); // def array. $image_width = 213; $image_height = 168; $count = 0; $dir = opendir($images_folder); while(false != ($file = readdir($dir))) { if(($file != ".") and ($file != "..") ) { $files[] = $file; // put in array. } } natsort($files); // sort. foreach($files as $file) { $ext = pathinfo($file, PATHINFO_EXTENSION); if ($ext == 'gif' || $ext == 'jpeg' || $ext == 'jpg' || $ext == 'png') { echo "<a href=\"$images_folder$file\" rel=\"OnlineSlideShow1\">"; echo "<img class=\"image\" alt=\"\" style=\"border-width:0;left:0px;top:0px;width:" .$image_width. "px;height:" .$image_height. "px;"; if ($count != 0) { echo "display:none;"; } echo "\" src=\"$images_folder$file\">"; echo "</a>"; echo "\r\n"; $count++; } } ?> Tnx for help Edited January 25, 2018 by Zane use [code] tags next time ;) Quote Link to comment https://forums.phpfreaks.com/topic/306321-make-it-faster/ Share on other sites More sharing options...
requinix Posted January 24, 2018 Share Posted January 24, 2018 Is it... too slow? Quote Link to comment https://forums.phpfreaks.com/topic/306321-make-it-faster/#findComment-1555732 Share on other sites More sharing options...
ginerjm Posted January 25, 2018 Share Posted January 25, 2018 Hmmm.... Usually topics start with a question. Do you have one? Or are you giving us a tutorial on how to use natsort? Quote Link to comment https://forums.phpfreaks.com/topic/306321-make-it-faster/#findComment-1555750 Share on other sites More sharing options...
taquitosensei Posted January 25, 2018 Share Posted January 25, 2018 natural sort of directory <?php $images_folder = "./corvette/"; $files = array(); // def array. $image_width = 213; $image_height = 168; $count = 0; $dir = opendir($images_folder); while(false != ($file = readdir($dir))) { if(($file != ".") and ($file != "..") ) { $files[] = $file; // put in array. } } natsort($files); // sort. foreach($files as $file) { $ext = pathinfo($file, PATHINFO_EXTENSION); if ($ext == 'gif' || $ext == 'jpeg' || $ext == 'jpg' || $ext == 'png') { echo "<a href=\"$images_folder$file\" rel=\"OnlineSlideShow1\">"; echo "<img class=\"image\" alt=\"\" style=\"border-width:0;left:0px;top:0px;width:" .$image_width. "px;height:" .$image_height. "px;"; if ($count != 0) { echo "display:none;"; } echo "\" src=\"$images_folder$file\">"; echo "</a>"; echo "\r\n"; $count++; } } ?> Tnx for help I would use in_array instead of if for the extensions. $extensions=array('gif','jpeg','jpg','png'); natsort(files); foreach($files as $file) { if(in_array($ext,$extensions))) { // do_stuff } } Quote Link to comment https://forums.phpfreaks.com/topic/306321-make-it-faster/#findComment-1555752 Share on other sites More sharing options...
dras Posted January 25, 2018 Author Share Posted January 25, 2018 My English is not so good, so ... On web page I have several (12) Slideshows and pics are in folder. When I don't use natsort pic is not show in correct order (1.jpg, 2.jpg ...10.jpg, 11.jpg) So, I sort each folder, but now page open lazy. How can I do same work instead natsort each folder? Tnx. Quote Link to comment https://forums.phpfreaks.com/topic/306321-make-it-faster/#findComment-1555753 Share on other sites More sharing options...
requinix Posted January 25, 2018 Share Posted January 25, 2018 If you only have 12 images then it should not be slow. Are you sure that it became slow when you added sorting and was not slow before then? Quote Link to comment https://forums.phpfreaks.com/topic/306321-make-it-faster/#findComment-1555755 Share on other sites More sharing options...
dras Posted January 25, 2018 Author Share Posted January 25, 2018 (edited) 12 folders with 15-30 pics http://restauracija-oldtimera-gigi.hr/portfolio.php Edited January 25, 2018 by dras Quote Link to comment https://forums.phpfreaks.com/topic/306321-make-it-faster/#findComment-1555756 Share on other sites More sharing options...
Barand Posted January 25, 2018 Share Posted January 25, 2018 Are these full-size (megapixel) images that you are displaying at a reduced size (213x168). If so, even though you are displaying them in a smaller size, you are still downloading the full size image. The way to speed things up would be to pre-shrink the stored images and download the thumbnail sized versions for display. Quote Link to comment https://forums.phpfreaks.com/topic/306321-make-it-faster/#findComment-1555757 Share on other sites More sharing options...
requinix Posted January 25, 2018 Share Posted January 25, 2018 You're also loading them all at the same time. Sorting is fast but listing all the files in a directory is not. 12 folders * 30 pictures each = 360 files. If each file takes 0.001 seconds to find then that's 0.360 seconds to load the entire page. Here on the west coast of the US it takes 0.7 seconds for the page to be generated and sent to me, but some of that will be because the server is in Croatia. How often do you add or remove images? Quote Link to comment https://forums.phpfreaks.com/topic/306321-make-it-faster/#findComment-1555758 Share on other sites More sharing options...
dras Posted January 25, 2018 Author Share Posted January 25, 2018 (edited) No, images are small size (~100 kb) Adding new images weekly Edited January 25, 2018 by dras Quote Link to comment https://forums.phpfreaks.com/topic/306321-make-it-faster/#findComment-1555759 Share on other sites More sharing options...
requinix Posted January 26, 2018 Share Posted January 26, 2018 I think I would split the work between PHP and some AJAX. 1. Have your PHP script find the first image for each directory. If it's always named 1.jpg then that's easy. That's the only information it outputs in the page. 2. When the page is loaded, have it fire off AJAX requests for each directory. These requests go to a PHP script which reports all the images available in the directory. 3. Make this new PHP script use browser caching, like saying it Expires in 3 hours. 4. That Javascript then takes that set of files and sets up the lightboxes. The page should also not be preloading every single image in every single directory. That will hurt your bandwidth. It will preload the first images for each directory but loading the others should wait until the user opens up the lightbox slideshow. Quote Link to comment https://forums.phpfreaks.com/topic/306321-make-it-faster/#findComment-1555769 Share on other sites More sharing options...
dras Posted January 29, 2018 Author Share Posted January 29, 2018 If I put only 8 sideshow on page will that speed up loading? Quote Link to comment https://forums.phpfreaks.com/topic/306321-make-it-faster/#findComment-1555852 Share on other sites More sharing options...
Psycho Posted January 29, 2018 Share Posted January 29, 2018 No, images are small size (~100 kb) Adding new images weekly That is not what I see. From what I see, the code is loading the full-size images and resizing them in the page <a href="./corvette/0.jpg" rel="OnlineSlideShow1"><img class="image" alt="" style="border-width:0;left:0px;top:0px;width:213px;height:168px;" src="./corvette/0.jpg"></a> The image that is being loaded in that code is 1032 x 581 and is 322KB! You should create copies of the images that are at the smaller size to show in the slideshow list and only call the full size image when the user selects to see it in full size. Quote Link to comment https://forums.phpfreaks.com/topic/306321-make-it-faster/#findComment-1555853 Share on other sites More sharing options...
Barand Posted January 29, 2018 Share Posted January 29, 2018 Perhaps, if enough people tell him, it may sink in. Quote Link to comment https://forums.phpfreaks.com/topic/306321-make-it-faster/#findComment-1555855 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.