I use this to display the latest image from 2 directories where images from cameras upload every 10s
The code works, but as I know I might end up with potentially 10s of thousands of images in each directory I believe the code is not optimized for the situation.
Also I reload the whole page every 10s where maybe it would be more efficient to just update the images.
Could someone help giving me directions to optimize this?
Thanks a lot.
<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";
$base_url_east = 'East/snap/';
$base_url_south = 'South/snap/';
$newest_mtime_east = 0;
$show_file_east = 'BROKEN';
if ($handle = opendir($base_url_east)) {
while (false !== ($file = readdir($handle))) {
if (($file != '.') && ($file != '..') && ($file != '.htaccess')) {
$mtime = filemtime("$base_url_east/$file");
if ($mtime > $newest_mtime_east) {
$newest_mtime_east = $mtime;
$show_file_east = "$base_url_east/$file";
}
}
}
}
$newest_mtime_south = 0;
$show_file_south = 'BROKEN';
if ($handle = opendir($base_url_south)) {
while (false !== ($file = readdir($handle))) {
if (($file != '.') && ($file != '..') && ($file != '.htaccess')) {
$mtime = filemtime("$base_url_south/$file");
if ($mtime > $newest_mtime_south) {
$newest_mtime_south = $mtime;
$show_file_south = "$base_url_south/$file";
}
}
}
}
?>
<html>
<head>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
</head>
<body bgcolor="#000000">
<center>
<?php
print '<img src="' .$show_file_east. '" alt="Latest image uploaded" width="720" height="480">';
print '<img src="' .$show_file_south. '" alt="Latest image uploaded" width="720" height="480">';
?>
</center>
</body>
</html>
If the new files are named East/snap/current.jpg and South/snap/current.jpg, you could easily use the HTML even without PHP like this
<img src="East/snap/current.jpg" alt="Latest image uploaded" width="720" height="480">
<img src="South/snap/current.jpg" alt="Latest image uploaded" width="720" height="480">
The one/script uploads the photos should be responsible for
copying the already uploaded current.jpg to 2016-08-13-07:00:00.jpg (or some other filename containing eg. the date
copying the new file to current.jpg
EDIT
If you cannot modify the filenames like I said above, try
$string = date('Ymd-H'); // 20160812-12
$files = glob('Schedule_' . $string . '*.jpg');
This will get you all files into an array, which were taken in the current hour. It could be that you get a problem, if the script is called 9 seconds, after the hour changed, because it won't find an image from that hour. But with this way you can minimize the count of scanned files.
EDIT
This script is not tested, but should return one recent file for one folder:
<?php
$basePath = '/home/user/camera/East/snap';
// Scans files from the current and the last minutes
// this ensures that always files will be found, even if the minute changed
$date = new \DateTime();
$date->setTimeZone(new \DateTimeZone('America/Vancouver'));
$prefixCurrentMinute = $basePath . '/Schedule_' . $date->format('Ymd-Hi') . '*.jpg';
$date->sub(new \DateInterval('PT1M'));
$prefixLastMinute = $basePath . '/Schedule_' . $date->format('Ymd-Hi') . '*.jpg';
$files = array_merge(
glob($prefixLastMinute),
glob($prefixCurrentMinute)
);
$lastFile = 'dummy.jpg';
if (is_array($files) AND count($files) > 0) {
// this methods sorts the files "regularly" by default, and I guess
// regularly means alpha-numeric in ascending order ...
sort($files);
$lastFile = $files[0];
// use this, if the order is not ascending after using sort() on the array
// $lastFile = $files[count($files) - 1];
}
$eastPhoto = basename($lastFile);
?>
<img src="East/snap/<?php echo $eastPhoto; ?>" alt="Latest image uploaded" width="720" height="480">
Related
I'm trying to paginate an arbitrary number of photos that I'm getting from a image directory onto my web page.
Here is my PHP script I'm running thus far:
<?php
$dir = "images/";
$counter = 1;
if($opendir = opendir($dir)){
//read directory
while(($file = readdir($opendir)) !== FALSE){
if($file != "." && $file != ".."){
echo "<img src='$dir/$file' alt='Picture broken' style='border-style: solid;border-width: 2px;border-color: #4d94ff; margin: 5px'>";
$counter++;
}
}
}
echo "<p>There are $counter images</p>";
?>
How do I get this to paginate automatically with 10 images on each page?
save all your images name of specific directory in a global array and paginate it with you own will :)
I have a directory with almost 60 images but in HD quality so theirs size are around 5 ~ 6 MB and load all them in a web page is to much time for server and browser so both hang up. I read this post and this other too and since I'm using PHP 5.4.20 in my server I'll like to use DirectoryIterator and LimitIterator but example leave in the post are not so explicit to me since I don't know how to move forward/backward in this cases. Can any give me some sample code about paginate files in a directory?
UPDATE: show some code
Right now this is how I read files:
function directoryToArray($directory, $recursive) {
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($directory . "/" . $file)) {
if ($recursive) {
$array_items = array_merge($array_items, directoryToArray($directory . "/" . $file, $recursive));
}
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
} else {
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
}
}
}
closedir($handle);
}
return $array_items;
}
$images = directoryToArray("images/portfolio/");
for ($i = 0; $i < count($images); $i++) {
$old_img_name = explode('/', $images[$i]);
$new_img_name = $old_img_name[0] . "/" . $old_img_name[1] . '/large/' . $old_img_name[2];
echo '<div class="span4 element">';
echo '<div class="hover_img">';
echo '<img src="' . $images[$i] . '" alt="" />';
echo '<span class="portfolio_zoom"></span>';
echo '</div>';
echo '</div>';
}
Aristona's absolutely right. You should probably resize the images to an appropriate file-format, quality & size. At the very least if you're trying to make some sort of gallery, you could use something like image magick to make 'thumbnails' for the gallery where clicking on them may take you to the full-quality image.
Image magick is scriptable in a variety of languages to batch process your images and build thumbnails if you want it to run as a process, alternatively from the command line you can do it as a once off, something like what's mentioned here:
Batch resize images into new folder using ImageMagick
Hello there good people of stackoverflow.
I have this code that basically get any photo i place in a folder called "pictures" and displays them on my page. Which is all good, and it works, with lightbox as well.
My main question is, can there be an easy way to display the photos in some form of order? i.e newest photos first?
<?php $handle = opendir(dirname(realpath(__FILE__)).'/pictures/');
while($file = readdir($handle)){if($file !== '.' && $file !== '..'){
echo '<img src="pictures/'.$file.'" border="0" />';}}?>
I know the code is very vulgar and ancient, but its only for a sample page so it doesn't need to be massive.
You can order them using php's filemtime(): http://php.net/manual/en/function.filemtime.php
<?php
$path = dirname(realpath(__FILE__)).'/pictures/';
$handle = opendir($path);
$arrFiles = array();
while($file = readdir($handle))
{
if($file !== '.' && $file !== '..')
{
$arrFiles[filemtime($path.$file)] = '<img src="pictures/'.$file.'" border="0" />';
}
}
arsort($arrFiles);
foreach ($arrFiles as $file)
{
echo $file;
}
?>
I've been trying for days to display a thumbnail of my weather cam. The server path is as follows
public_html
..cam
...20121012
...20121013
...20121014
The date folder are auto created everyday by my webcam settings. So everyday it will create a new folder and name it date('Ymd').
I'm using the following script to have the image displayed on my wordpress sidebar, but I can't get it to work. Furthermore, I would like to have it displayed as a thumbnail and when a visitor clicks on it, to have it open slightly larger (real size) in a popup screen.
<?php
chdir('/home/deb57301n2/domains/meteowestkust.be/public_html/cam/');
$subdirname = date('Ymd').'/';
echo getcwd();
$newest_mtime = 0;
$show_file = 'webcam_offline.png';
if ($handle = opendir($subdirname)) {
while (false !== ($file = readdir($handle))) {
if (($file != '.') && ($file != '..')) {
$mtime = filemtime($subdirname.$file);
if ($mtime > $newest_mtime) {
$newest_mtime = $mtime;
$show_file = $subdirname.$file;
}
}
}
}
print '<img src="' .$show_file. '" alt="Weather Cam - West Coast - Belgium">';
?>
any help is highly appreciated !
It worked by adding
$show_file = '/cam/'.$subdirname.$file;
thanks for the help anyway guys !
I followed a few sites and have come to this:
<?php
$imagesDir = base_url() . 'images/eventGallery/';
$files = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
for ($i = 0; $i < count($files); $i++) {
$num = $files[$i];
echo '<img src="'.$num.'" alt="random image">'." ";
}
?>
It is not working as nothing is displaying! What am I doing wrong? There are two images in said directory, with ,jpg extension.
Thanks for any assistance!
What does your base_url() returns? Do you have slash('/') between base url and images?
case-sensitivity of glob is different on various systems, do your pictures have jpg or JPG or Jpg (etc) extensions?
when creating img tags you must transform paths from file-system to web path (relative from web root)
remove count from loop
This is working version of your code (altough I would rather use readdir :) ):
define('BASE_URL', dirname(__FILE__));
$imagesDir = BASE_URL . '/images/eventGallery/';
$files = glob($imagesDir . '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE);
$len=count($files);
for ($i = 0; $i < $len; $i++)
{
$num = $files[$i];
// transform from file system path to web path - assuming images is in the web root
$num = substr($num, strlen(BASE_URL) );
echo '<img src="'.$num.'" alt="random image">'." ";
}
Just use PHP's built in readdir function
Reference: http://php.net/manual/en/function.readdir.php
<?php
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry\n";
}
}
closedir($handle);
}
?>
i would probably do something like this:
foreach (glob('../_pics/about/'.'*.{jpg,jpeg,JPG,JPEG}', GLOB_BRACE) as $filename)
{
echo"$filename";
}