Jump to content

Image Display Script Help


CCraft

Recommended Posts

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.


[code]
<html>
<head></head>
<body>
<table>
<?php
$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>";
          }
        }
      closedir($dh);
  }
}
?>
</table>
</body>
</html>
[/code]

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

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

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

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:

[code]

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";
    }
}

// ...

uasort($imageFiles,'_filesorter');

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

[/code]

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.