WXforum.net

Web Weather => Weather Website PHP/AJAX scripting => Topic started by: Ian. on October 14, 2017, 04:32:27 AM

Title: Image Rename Script Help **Solved**
Post by: Ian. on October 14, 2017, 04:32:27 AM
Hi,

I'm trying to get image_rename script to work, I've tried numerous permutations but have conceded defeat.

I have a Foscam F19901EP set to FTP a snapshot to x10hosting test site and would like the script to generate a standard.jpg name.

Any help apreachiated as always

Ian


Error messages http://chatteris.x10host.com//FI9901EP_00626E70E4DF/image_rename.php (http://chatteris.x10host.com//FI9901EP_00626E70E4DF/image_rename.php)
Title: Re: Image Rename Script Help
Post by: droiddk on October 14, 2017, 09:00:04 AM
Can you post the script without your modifications?

And, may I ask, why do you need a script like that? Why not simple scan the upload folder and sort the images (by filename) to show the latest image ? (Guess you are looking for at static filename to use in your html?)

Regards
Title: Re: Image Rename Script Help
Post by: Ian. on October 14, 2017, 09:30:45 AM
Hi,

I've attached the file, I copied this from wxforum and includes modifications to make it work with version 7 php which Jachym worked on and the forum member with the issue reported back that it all works.


Title: Re: Image Rename Script Help
Post by: droiddk on October 14, 2017, 10:31:10 AM
And second question ?  :grin:
Title: Re: Image Rename Script Help
Post by: PaulMy on October 14, 2017, 11:05:16 AM
Hi Ian,
Not sure if this will help as my HV failed and haven't replaced it and was not using PHP7, but I worked with morfeas2002 on that script and it worked fine for me - even though I didn't understand the code.
In comparing your attached script to mine I see some differences:

Your line 11: $dir = "../FI9901EP_00626E70E4DF"; // change to suit your system relative to where this script is executed
Mine line 15: $dir = "../hvipupload"; // change to suit your system relative to where this script is executed
and that should be fine.

Your line 20: if (preg_match('/^\.{1,2}$/',$fname)) continue;
Mine line 25: if (ereg('^\.{1,2}$',$fname)) continue;

Your line 22: if (! preg_match('/\.(JPG)$/',$fname)) continue;
Mine line 27: if (! ereg($pattern,$fname)) continue;
and the above 2 differences could be PHP 7 requirement

Your line 41: copy("/snap/$newname", "/new/webcamimage.jpg"); // change to suit your system relative to where this script is executed
Mine line 50: copy("../hvipupload/$newname", "../hvipimage/hvipimage.jpg"); // change to suit your system relative to where this script is executed
where my defined $dir = "../hvipupload" in line 15 is used in line 50, your line 41 is different than in line 11

My webserver structure was?
/pictures/hvip/hik.php  (my name change script)
/pictures/hvipupload/  (where the date/time stamped image was FTP and which was emptied at each run of the rename script)
/pictures/hvipimage/hvipimage.jpg  (the renamed current time image)

Darn, I wish that camera still worked http://www.komokaweather.com/pictures/hvipimage/hvipimage.jpg (http://www.komokaweather.com/pictures/hvipimage/hvipimage.jpg)
Enjoy,
Paul

EDIT: after I typed my reply and posted I noticed you had also posted my script in full ;)
Title: Re: Image Rename Script Help
Post by: Ian. on October 14, 2017, 12:36:33 PM
Hi Paul,

Many thanks for all the effort you have put in on this, I'm blowed if I can get it to work.

When I run the script which I have called foscam.php it throws up the following error message:

Warning: sort() expects parameter 1 to be array, null given in /home/chatteri/public_html/foscam.php on line 37

Warning: copy(): The first argument to copy() function cannot be a directory in /home/chatteri/public_html/foscam.php on line 50

Some good news is that the .jpg file which the camera uploads to the snap sub directory is being erased when the script is ran, but this does not generate a new foscamimage1.jpg in my case.

I've attached the modified script which should have followed your information, I've added notes in the document to highlight chnages.

I never have been any good at relitive and absolute paths, and so far filezilla transfer count with me trying to get this working has hit 45 lol

Ian


New link to script - http://chatteris.x10host.com/foscam.php
Title: Re: Image Rename Script Help
Post by: droiddk on October 14, 2017, 12:59:05 PM
I guess you will not reply to my second question so good luck!

Title: Re: Image Rename Script Help
Post by: Ian. on October 14, 2017, 01:05:25 PM
Sorry droidk I didn't see the second part of your first reply.

Yes you are correct, I need a fixed image name so that I can link to it, unfortunaltly the webcam only uplads day, date, year time format.

Please dont be offended, I genuinly did not see the second part.

Ian
Title: Re: Image Rename Script Help
Post by: droiddk on October 14, 2017, 01:10:20 PM
Sorry droidk I didn't see the second part of your first reply.

Yes you are correct, I need a fixed image name so that I can link to it, unfortunaltly the webcam only uplads day, date, year time format.

Please dont be offended, I genuinly did not see the second part.

Ian

Its okay Ian, I just want to help you  :grin:

For the same situation I just use this very simple vode:

<?php

$dirname = "images/";

if ($images = glob($dirname."*.jpg") ) {

  rsort($images);

  $img = $images[0];

  echo  "<img src=\"$img\">";

}

?>

That script will find all the jpg's in the "images" folder and show the latest image.
Maybe you can use it.

Kind Regards
droid
Title: Re: Image Rename Script Help
Post by: Ian. on October 14, 2017, 01:15:37 PM
Thanks droiddk,

As you can proberbly tell I'm not a scripter  :oops:  , how would I impliment your code, my webcam is sending a new image ebery 5 minutes to a folder: /FI9901EP_00626E70E4DF/snap
this is embedded in the camera firmware so I can't change this.

What do you suggest that I can try?

tnx Ian
Title: Re: Image Rename Script Help
Post by: droiddk on October 14, 2017, 01:19:15 PM
Thanks droiddk,

As you can proberbly tell I'm not a scripter  :oops:  , how would I impliment your code, my webcam is sending a new image ebery 5 minutes to a folder: /FI9901EP_00626E70E4DF/snap
this is embedded in the camera firmware so I can't change this.

What do you suggest that I can try?

tnx Ian

Hi Ian

The page where you want to show your image, is that a php-page?

Regards
Droid
Title: Re: Image Rename Script Help
Post by: droiddk on October 14, 2017, 01:35:25 PM
Hi again Ian

You can try making a php file with this code:

<?php

if ($images = glob("*.jpg") ) {

  rsort($images);

  $img = $images[0];

  echo  "<img src=\"$img\">";

}

?>

Place the new file in the folder with your images.

When you are loading that new file in your browser the latest image should be on screen. Just as a test.

Regards
Droid
Title: Re: Image Rename Script Help
Post by: Ian. on October 14, 2017, 01:42:43 PM
Hi,

Yep that displays  :lol::

http://chatteris.x10host.com//FI9901EP_00626E70E4DF/snap/camtest.php


Ian
Title: Re: Image Rename Script Help
Post by: PaulMy on October 14, 2017, 01:48:50 PM
The droiddk suggestions is working... great!  I presume that keeps all the images in the /snap folder without doing any erasing, nice!
 
Quote
Some good news is that the .jpg file which the camera uploads to the snap sub directory is being erased when the script is ran, but this does not generate a new foscamimage1.jpg in my case.
which is good and presume on the right track for that script.

Quote
Warning: sort() expects parameter 1 to be array, null given in /home/chatteri/public_html/foscam.php on line 37
Warning: copy(): The first argument to copy() function cannot be a directory in /home/chatteri/public_html/foscam.php on line 50
Sorry I don't know what would be needed to overcome this.

Quote
I never have been any good at relitive and absolute paths, and so far filezilla transfer count with me trying to get this working has hit 45 lol
If foscam.php is in your /public_html folder I wonder if the path to $dir = "./FI9901EP_00626E70E4DF"; // change to suit your system relative to where this script is executed
should be $dir = "/FI9901EP_00626E70E4DF"; // change to suit your system relative to where this script is executed
but presume you have already tried numerous versions of the path... in line15 and 50.

Regards,
Paul

 
Title: Re: Image Rename Script Help
Post by: droiddk on October 14, 2017, 01:49:58 PM
Hi,

Yep that displays  :lol::

http://chatteris.x10host.com//FI9901EP_00626E70E4DF/snap/camtest.php


Ian

Great.

Now, in your php file where you want to show your image put in this:

<?php

$dirname = "images/";

if ($images = glob($dirname."*.jpg") ) {

  rsort($images);

  $img = $images[0];

  echo  "<img src=\"$img\">";

}

?>

And change $dirname = "images/"; to suit your setup. If page with the image is in root-folder and images are in root/snap then change it to $dirname = "snap/";

Let me know :-D

Kind Regards
Title: Re: Image Rename Script Help
Post by: droiddk on October 14, 2017, 01:51:13 PM
I presume that keeps all the images in the /snap folder without doing any erasing.

Yes  :grin:

Kind Regards

Title: Re: Image Rename Script Help
Post by: droiddk on October 14, 2017, 02:01:24 PM
If you want you can split it like this:

This at the beginning of the php file:

<?php

$dirname = "images/";

if ($images = glob($dirname."*.jpg") ) {

  rsort($images);

  $img = $images[0];

}

?>

And this where you want to show the image:

 <?php echo  "<img src=\"$img\">"; ?>

Regards
Droid
Title: Re: Image Rename Script Help
Post by: Ian. on October 14, 2017, 02:11:16 PM
Hi Droiddk,

My main webhost is GoDaddy, I'm using x 10 so I can play without breaking anything, so the main php scripts (Saratoga & Meteotemplate) for displaying images are on that host, I can change the FTP of the camera and move the work done so far which isn't a problem, I can see how you are moving me forward which is brilliant, is it possible to chnage to a fixed file name at this point without moving over to Go Daddy, if not, I'll start making the changes if its all part of a bigger plan.

Ian
Title: Re: Image Rename Script Help
Post by: droiddk on October 14, 2017, 02:18:29 PM
Hi Ian

I'm not sure I understand your last question  :oops:

If you can send me the php file and mark the place in that file where the image shall show, and also send me a screenshot showing the folder structure on the web-server I can try making it for you.

Note that you can change the width and height like this:

"<img src=\"$img\" alt=\"My Image\" height=\"42\" width=\"42\" >";

Regards
Droid
Title: Re: Image Rename Script Help
Post by: Ian. on October 14, 2017, 02:19:57 PM
The droiddk suggestions is working... great!  I presume that keeps all the images in the /snap folder without doing any erasing, nice!
 
Quote
Some good news is that the .jpg file which the camera uploads to the snap sub directory is being erased when the script is ran, but this does not generate a new foscamimage1.jpg in my case.
which is good and presume on the right track for that script.

Quote
Warning: sort() expects parameter 1 to be array, null given in /home/chatteri/public_html/foscam.php on line 37
Warning: copy(): The first argument to copy() function cannot be a directory in /home/chatteri/public_html/foscam.php on line 50
Sorry I don't know what would be needed to overcome this.

Quote
I never have been any good at relitive and absolute paths, and so far filezilla transfer count with me trying to get this working has hit 45 lol
If foscam.php is in your /public_html folder I wonder if the path to $dir = "./FI9901EP_00626E70E4DF"; // change to suit your system relative to where this script is executed
should be $dir = "/FI9901EP_00626E70E4DF"; // change to suit your system relative to where this script is executed
but presume you have already tried numerous versions of the path... in line15 and 50.

Regards,
Paul

Hi Paul,

Your help has been great, it looks like I'm on the way with getting this sorted, I have two differnt ipcams which I will be using the finished script on so its worth the pain.

Hope you manage to get a new camera soon, Christmas isn't a million miles away :-)

All the best

Ian
Title: Re: Image Rename Script Help
Post by: Ian. on October 14, 2017, 02:22:20 PM
Hi Ian

I'm not sure I understand your last question  :oops:

If you can send me the php file and mark the place in that file where the image shall show, and also send me a screenshot showing the folder structure on the web-server I can try making it for you.

Note that you can change the width and height like this:

"<img src=\"$img\" alt=\"My Image\" height=\"42\" width=\"42\" >";

Regards
Droid

Hi Droid,

I'll start moving files and make camera FTP changes, I'll post up with progress as quick as I can

Ian
Title: Re: Image Rename Script Help
Post by: droiddk on October 14, 2017, 02:29:31 PM

Hi Droid,

I'll start moving files and make camera FTP changes, I'll post up with progress as quick as I can

Ian

Oki, good luck  :grin: Remember backup all files.

Regards
Droid
Title: Re: Image Rename Script Help
Post by: PaulMy on October 14, 2017, 02:39:11 PM
Thanks Ian,
Quote
Hope you manage to get a new camera soon, Christmas isn't a million miles away (http://www.wxforum.net/Smileys/default/icon_smile.gif)
It's the climbing on the roof, setting up the camera FTP and in the router, etc. that is holding me back...  I did it once and remember the pain.

Paul

Title: Re: Image Rename Script Help
Post by: Ian. on October 14, 2017, 03:45:09 PM
OK Stuck! my new Foscam F19901EP wont FTP to GoDaddy for some reason, so to move this on....

Recap -
webcam uploading images every 5 mins to server
Script to display latest image works fine - http://chatteris.x10host.com//FI9901EP_00626E70E4DF/snap/camtest.php (http://chatteris.x10host.com//FI9901EP_00626E70E4DF/snap/camtest.php)

Issues to resolve -

Delete images which are earlier than the latest one
Rename latest image so that it can be pointed to by WU and external users (including my web site in the short term).


Title: Re: Image Rename Script Help
Post by: Ian. on October 15, 2017, 03:47:07 AM
Ok new day new start....and many thanks for all the help and suggestions so far..

Going back to basics to get the original script working for me, Paul described his config which worked for him when his camera was available... so I mimiced his setup on the server and the script, thinking if I can at least get it working that way, I can incrementally make the changes to suit the foscam:

I have

/pictures/hvip/hik.php
/pictures/hvipimage
/pictures/hvipupload

A test image named hvipimage.jpg in the hvipimage sub directory
A test image with a date/time name in the hvipupload sub directory

Running the script clears the contents of hvipupload, but does not update the image in hvipimage, also an error message is generated:

Warning: sort() expects parameter 1 to be array, null given in /home/chatteri/public_html/pictures/hvip/hik.php on line 37

Warning: copy(): The first argument to copy() function cannot be a directory in /home/chatteri/public_html/pictures/hvip/hik.php on line 50


This is the script I'm trying to work:

Code: [Select]
<?php
#############################################################################################################
## Script to rename Hikvision IP uploaded files containing date and time in file name to a new fixed filename
## Provided by morfeas2002  http://kalamata.meteoclub.gr/

## CAUTION this script will remove any ???.jpg uploaded files from the folder
## CAUTION
## CAUTION
## CAUTION this script will remove any ???.jpg uploaded files from the folder
#############################################################################################################

date_default_timezone_set("Europe/London"); // 
//
//
$dir "../hvipupload"// change to suit your system relative to where this script is executed
// changed from "../hvipupload"
$pattern '\.(jpg)$';
//
$newstamp 0;           
$newname "";

if (
$handle opendir($dir)) {              
       while (
false !== ($fname readdir($handle)))  {           
         
// Eliminate current directory, parent directory           
         
if (preg_match('/^\.{1,2}$/',$fname)) continue;           
         
// Eliminate other pages not in pattern           
         
if (! preg_match('/\.(JPG)$/',$fname)) continue;           
         
// -------------
         //
         //
        
         //
         
$pinakas[] = $fname;
       }
       
//
       //
       
sort($pinakas);
 // 
       
$posot count($pinakas);
       
//
       
if ($posot>1$newname $pinakas[$posot-2];
       if (
$posot==1$newname $pinakas[$posot-1];

}
closedir ($handle);

// $newname
//

copy("../hvipupload/$newname""../hvipimage/hvipimage.jpg"); // change to suit your system relative to where this script is executed
// changed from "../hvipupload/$newname", "../hvipimage/hvipimage.jpg" but had to add $dir from line 15
//

$filesa glob('../hvipupload/*.jpg'); // change to suit your system relative to where this script is executed
// not sure what to enter here?
array_walk($filesa,'myunlink');

function 
myunlink($t)
{
unlink($t);
}

?>


What am I missing (apart form the obvious lack of scripting knowledge)!

Ian
Title: Re: Image Rename Script Help
Post by: Jáchym on October 15, 2017, 05:50:39 AM
That sort error is because the while loop is not satisfied by any file in that folder, the array is not created and it then doesn´t know what to sort
Title: Re: Image Rename Script Help
Post by: Ian. on October 15, 2017, 08:06:38 AM
Hi Jachym,

Thanks for pitching in, is it that the script deletes the file before it has been renamed due to an error?

The upload folder does have a file in it, and after running this is deleted.

No newname image is created.

Ian
Title: Re: Image Rename Script Help
Post by: Jáchym on October 15, 2017, 08:41:22 AM
I am not familiar with those functions, I would use the "glob" function for scanning the dir content
Title: Re: Image Rename Script Help
Post by: Ian. on October 15, 2017, 09:09:15 AM
No probs, I'll ask on the forum if anyone will share a working script in case the one I'm using is flawed.

Cheers

Ian

**Solved** the upper case JPG threw the script, all working now, thanks a million to all.
Title: Re: Image Rename Script Help **Solved**
Post by: droiddk on October 15, 2017, 02:29:14 PM
Hi Ian

Marked as solved?

And I agree with Jáchym and will use glob.

This simple script will find jpg's in the folder where the script is and save the latest as image.jpg, can be used with a cron-job.

<?php

if ($images = glob("*.jpg") ) {

   rsort($images);

   $img = $images[0];

   $newfile = 'image.jpg';

   copy($img, $newfile);

}

?>

Kind Regards
Droid
Title: Re: Image Rename Script Help **Solved**
Post by: Ian. on October 15, 2017, 02:38:48 PM
Hi Droid,

Yep all sorted, the script you shared was incredibly useful and I will be using that for another task, the original renaming script is up and running, so I have had a good day...well that was until I asked GoDaddy to migrate my website to a European server and it broke the SSL certificate whichj means I can't send data, so well pi**ed off.
Title: Re: Image Rename Script Help **Solved**
Post by: droiddk on October 15, 2017, 02:51:56 PM
Glad it works (the script)  :grin:

Regards
Title: Re: Image Rename Script Help **Solved**
Post by: PaulMy on October 15, 2017, 02:54:55 PM
Quote
the original renaming script is up and running, so I have had a good day...well that was until
What changes did you need to make to the script Ian?

Enjoy,
Paul
Title: Re: Image Rename Script Help **Solved**
Post by: Ian. on October 15, 2017, 03:00:23 PM
Hi Paul,

I compared the original script with the one I was using and the only difference was that the timezone was enabled and I had .JPG rather than .jpg set in one place.

Using your description really helped as I reconstructed it and worked through until it worked.

So many thanks

Ian
Title: Re: Image Rename Script Help **Solved**
Post by: WessexWeather on June 22, 2020, 05:58:07 PM
Hi Ian

Marked as solved?

And I agree with Jáchym and will use glob.

This simple script will find jpg's in the folder where the script is and save the latest as image.jpg, can be used with a cron-job.

<?php

if ($images = glob("*.jpg") ) {

   rsort($images);

   $img = $images[0];

   $newfile = 'image.jpg';

   copy($img, $newfile);

}

?>

Kind Regards
Droid

Hi Droid,

Reviving an old thread here... your script works perfectly with filenames. Can it be adapted to sort by timestamp, taking and renaming the most recent image regardless of filename?
Title: Re: Image Rename Script Help **Solved**
Post by: LU9DKL on September 28, 2020, 10:25:58 AM
Hello, can anyone help me?

I have a dvr that uploads ftp every 60 seconds a photo to

climasaladillo.com:/public_html/192.168.0.150/2020-09-26/XVR_ch4_20200926102653_T.jpg

I need to delete the old photo every time I upload a new one and rename the new one to image.jpg

(https://climasaladillo.com/template/plugins/stickers/stickerInteractiveAnim.php?text=Clima%20Saladillor%20%20-%20%20Buenos%20Aires,%20AR&bgColor=212121&type=random)
Title: Re: Image Rename Script Help **Solved**
Post by: PaulMy on September 28, 2020, 11:36:47 AM
Have you tried the scripts mentioned above or https://www.wxforum.net/index.php?topic=32044.msg322649#msg322649 (https://www.wxforum.net/index.php?topic=32044.msg322649#msg322649)  and do they not work?


Enjoy,
Paul


Title: Re: Image Rename Script Help **Solved**
Post by: LU9DKL on September 29, 2020, 08:59:15 AM


Hi Paul, good morning, could you help me configure the script you quoted? I'm new to this   :oops:

P.S. My dvr is dahua does this script work the same?
Title: Re: Image Rename Script Help **Solved**
Post by: PaulMy on September 29, 2020, 11:17:49 AM
Hi,
My Hikvision failed a few years ago so it has been a long time since I looked at this, and my PHP coding skill is next to nothing.  Also, the code in the script is old and the script no longer works komokaweather.com/pictures/hv/hik.php and see the same for the other links here.  Likely someone has created an alternate way to do the rename.  Unfortunately the heading of this thread says **Resolved**  and that may stop people from looking at it.


I now have a newer camera and use the Sebec IPTimelapse program that uses a fixed name for file uploading.


Enjoy,
Paul