Jump to content

Make it faster


dras

Recommended Posts


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

Link to comment
Share on other sites

 

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 
     }
}
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.