Display latest webcam image in thumbnail - php

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 !

Related

How do I prevent certain elements from loading in until they're in my viewport?

I have page where there's gonna be 1000+ images. They're separated into different photography projects so you just scroll down and look through them. However, all (135 at the moment) the images cache at once so it's slow, even though the images are down-scaled 75% from original size. I load in the images dynamically with PHP to make it easier for my client to expand their portfolio with minimal code editing.
I'm looking for something that Pinterest uses. It doesn't load in the images (or previews) until you've scrolled to them and they're actually visible in the viewport.
Dynamic PHP code:
$dir = ("projects/photography/" . scandir("projects/photography/")[$photographyIndex]);
$files = scandir($dir);
foreach($files as $file) {
if($file !== "." && $file !== ".." && $file !== "info.php") {
echo "<img src='$dir/$file' class='h-full' />";
}
}
Thanks #Ownagin!
The problem was that I didn't have loading="lazy" on my <img> tags. I was aware of this being a thing, but never bothered to apply it because I read it was enabled by default.
$dir = ("projects/photography/" . scandir("projects/photography/")[$photographyIndex]);
$files = scandir($dir);
foreach($files as $file) {
if($file !== "." && $file !== ".." && $file !== "info.php") {
echo "<img src='$dir/$file' class='h-full' loading='lazy' />";
}
}

WordPress PHP Random Image Script

I've been displaying using the below code for a few years to display a random image in WordPress without any issues. The code broke today when I upgraded to PHP 7.4. I'm stuck on how to make the code work again or replace it with new code so that images from a directory are randomly displayed.
*Update: Broken code means images are you no longer being displayed. There are no error messages our outputs, just a blank section where the random image would usually display.
<?php
$folder = '';
$exts = 'jpg jpeg png gif';
$files = array(); $i = -1;
if ('' == $folder) $folder = './';
$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) {
if (preg_match('/\.'.$ext.'$/i', $file, $test)) {
$files[] = $file;
++$i;
}
}
}
closedir($handle);
mt_srand((double)microtime()*1000000);
$rand = mt_rand(0, $i);
header('Location: '.$folder.$files[$rand]); // Voila!
?>
The file is called rotate.php and it's located within the images directory. I call the rotate.php script from within the website's css style sheet using the following code:
background: #ffffff url(images/rotate.php) no-repeat center top;
Thanks for your help.

Display latest image from directory

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

How to display image from server (newest-oldest) to a new php page?

i have a form that create images (using imagejpeg, imagettftext and imagecreatefromjpeg) and save it on a folder in the server. What i need to do is to display ALL created images to another page, the newest on the top and so that the oldest is at the bottom. i have a form called formdisplay.php but it's just displaying a broken image and not newest to oldest. hope you can help me with this. really need to get this working. thanks in advance for your help.
i have read the posts but none of those worked for me.
Pull dedicated images from folder - show image + filename (strip part of filename + file-extension)
How can I display latest uploaded image first? (PHP+CSS)
getting images from the server and display them
Displaying images from folder in php
display image from server embedding php in html
formcreatesave.php
imagecopymerge($im, $img2, 10, 350, 0, 0, imagesx($img2), imagesy($img2), 100);
$date_created = date("YmdHis");//get date created
$img_name = "-img_entry.jpg"; //the file name of the generated image
$img_newname = $date_created . $img_name; //datecreated+name
$img_dir =dirname($_SERVER['SCRIPT_FILENAME']) ."/". $img_newname; //the location to save
imagejpeg($im, $img_dir , 80); //function to save the image with the name and quality
imagedestroy($im);
formdisplay.php
$dir = '/home3/site/public_html/Master/uploader/uploader';
$base_url = 'http://mysite.com/Master/uploader/uploader/20131027024705-img_entry.jpg';
$newest_mtime = 0;
$show_file = 'BROKEN';
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (($file != '.') && ($file != '..')) {
$mtime = filemtime("$dir/$file");
if ($mtime > $newest_mtime) {
$newest_mtime = $mtime;
$show_file = "$base_url/$file";
}
}
}
}
print '<img src="' .$show_file. '" alt="Image Title Here">';
please feel free to edit my code. thanks :)
This goes after the while loop that creates the $images array (list of image filenames
foreach ($images as $image)
{
$filetime = filemtime("images1/$image");
$sortedimages[]$filetime => $image;
}
krsort($sortedimages);
echo "<pre>";
print_r($sortedimages);
echo "</pre>"
?>
Read images from folder.
Loop through them and insert into array with the key filetime of image.
Use krsort function to sort them according to date.
Loop and display them.
I think this link will help you:
http://forums.phpfreaks.com/topic/219466-php-displaying-images-from-folder-with-most-recent-first/

Easy way to sort getting images from a gallery using php?

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

Categories