Jump to content


Image Display Script Help

  • Please log in to reply
1 reply to this topic

#1 CCraft

  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 08 July 2006 - 09:28 PM

Greetings everyone,
What I have is a server with some web cam software installed. The software saves images whenever motion is detected. I have an apache/php server going to serve a php page that I want to display the images. Its just a side DIY project at my home. Here is what I have so far.

$dir = ".";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if (preg_match("/.jpg/", $file)) {
                $exif = exif_read_data($file, 0, true);
                echo "<tr>";
                echo "<td valign=top><a href=$dir/$file><img src=$dir/$file width=250 height=200></a><td>";
                echo "<td valign=top><font size=-1>";
                echo "File: <b>" . $exif['FILE']['FileName'] . "</b><br/>";
		echo "Size" . ': ' . filesize($file) . ' bytes' . "<br/>";
		echo date ("F d Y H:i:s.", filemtime($file));
                echo "</font></td>";
                echo "</tr>";

I am trying to do 2 things that I haven't been able to figure out. 1) I don't want to display any images will a filesize less than 100000 bytes. 2) I want to order the images by their creation date with the most recent at the top. I just have all the images and the script in the root directory of my web sever. Any help with figuring this out would be great.

Thank you in advance,

#2 Wildbug

  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 09 July 2006 - 03:15 PM

You already know about filesize(), so why aren't you using it to filter the results?

if (preg_match('/\.jpg$/',$file) && filesize($file) > 100000) {
    // ....

If you need to sort data, you can't do it on the fly.  You can save it in an array and use the array sorting functions to rearrange the data in the order you want it.  Then it can be output in the desired order.

For example:

function _filesorter($a,$b) {
    if ($a['date'] == $b['date']) return 0;
    return ($a < $b) ? -1 : 1;

$imageFiles = array();
while (($file = readdir($dh)) !== false) {
    if (preg_match('/\.jpg$/',$file)) {
        $exif = exif_read_data($file,0,true);
        $imageFiles[$file]['date'] = filemtime($file);
        $imageFiles[$file]['size'] = filesize($file);
        $imageFiles[$file]['name'] = $exif['FILE']['FileName'];
        $imageFiles[$file]['path'] = "$dir/$file";

// ...


// ... then print the output ...

I haven't tested that, but you get the idea.  You need the extra callback function to sort the multi-dimensional array by one of its values.

(Notice the updated regex pattern.  You need to escape the period (\.) to actually match a period, and the pattern should also be anchored at the end ($).  Your pattern will match *.jpg files, but it will also match "about_jpgs.html," etc.)
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users