Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 296343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:32:25+00:00 2026-05-12T06:32:25+00:00

I have a PHP script that creates a thumbnail and lists an image gallery.

  • 0

I have a PHP script that creates a thumbnail and lists an image gallery. The problem I’m having is that it lists it by timestamp on the server but I want it to list ‘naturally’.

<?php
# SETTINGS
$max_width = 100;
$max_height = 100;
$per_page = 24;

$page = $_GET['page'];

$has_previous = false;
$has_next = false;

function getPictures() {
    global $page, $per_page, $has_previous, $has_next;
    if ( $handle = opendir(".") ) {
        $lightbox = rand();
        echo '<ul id="pictures">';

        $count = 0;
        $skip = $page * $per_page;

        if ( $skip != 0 )
            $has_previous = true;

        while ( $count < $skip && ($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
                $count++;
        }
        $count = 0;
        while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
                if ( ! is_dir('thumbs') ) {
                    mkdir('thumbs');
                }
                if ( ! file_exists('thumbs/'.$file) ) {
                    makeThumb( $file, $type );
                }
                echo '<li><a href="'.$file.'" class="zoom" rel="group">';
                echo '<img src="thumbs/'.$file.'" alt="" />';
                echo '</a></li>';
                $count++;
            }
        }
        echo '</ul>';

        while ( ($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
                $has_next = true;
                break;
            }
        }
    }
}

function getPictureType($file) {
    $split = explode('.', $file); 
    $ext = $split[count($split) - 1];
    if ( preg_match('/jpg|jpeg/i', $ext) ) {
        return 'jpg';
    } else if ( preg_match('/png/i', $ext) ) {
        return 'png';
    } else if ( preg_match('/gif/i', $ext) ) {
        return 'gif';
    } else {
        return '';
    }
}

function makeThumb( $file, $type ) {
    global $max_width, $max_height;
    if ( $type == 'jpg' ) {
        $src = imagecreatefromjpeg($file);
    } else if ( $type == 'png' ) {
        $src = imagecreatefrompng($file);
    } else if ( $type == 'gif' ) {
        $src = imagecreatefromgif($file);
    }
    if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
        $newW = $oldW * ($max_width / $oldH);
        $newH = $max_height;
    } else {
        $newW = $max_width;
        $newH = $oldH * ($max_height / $oldW);
    }
    $new = imagecreatetruecolor($newW, $newH);
    imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
    if ( $type == 'jpg' ) {
        imagejpeg($new, 'thumbs/'.$file);
    } else if ( $type == 'png' ) {
        imagepng($new, 'thumbs/'.$file);
    } else if ( $type == 'gif' ) {
        imagegif($new, 'thumbs/'.$file);
    }
    imagedestroy($new);
    imagedestroy($src);
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:32:25+00:00Added an answer on May 12, 2026 at 6:32 am

    The trick was to put everything inside an array… I took the liberty of rewriting your getPictures() function… This one implements the sorting.

    function getPictures() {
        global $page, $per_page, $has_previous, $has_next;
    
        if (!is_dir('thumbs')) {
            mkdir('thumbs');
        }
    
        if ($handle = opendir(".")) {
            $lightbox = rand();
    
            $files = array();
    
            while (($file = readdir($handle)) !== false) {
                if (!is_dir($file) && ($type = getPictureType($file)) != '') {
                    $files[] = $file;
                }
            }
    
            natsort($files);
    
            $has_previous = $skip != 0;
            $has_next = (count($files) - $skip - $per_page) > 0;
    
            $spliceLength = min($per_page, count($files) - $skip);
            $files = array_slice($files, $skip, $spliceLength);
    
            echo '<ul id="pictures">';
    
            foreach($files as $file) {
                if (!file_exists('thumbs/' . $file)) {
                    $type = getPictureType($file);
                    makeThumb($file, $type);
                }
    
                echo '<li><a href="' . $file . '" class="zoom" rel="group">';
    
                echo '<img src="thumbs/' . $file . '" alt="" />';
    
                echo '</a></li>';
            }
    
            echo '</ul>';
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 207k
  • Answers 207k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer String.prototype.alertDialog = function() { alert(this); }; May 12, 2026 at 9:16 pm
  • Editorial Team
    Editorial Team added an answer AutoScroll will only help if the children controls span an… May 12, 2026 at 9:16 pm
  • Editorial Team
    Editorial Team added an answer If you're designing a state diagram, try to first figure… May 12, 2026 at 9:16 pm

Related Questions

I have the following script (it looks long, but well commented). Problem is, i
I'm currently working on a PHP/MySQL script that does the following, in this order:
I'm looking to create a PHP script where, a user will provide a link
I have a function in PHP that resizes images into a thumbnail, my image

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.