Author Topic: Help needed updating php webcam image rename script (SOLVED)  (Read 6450 times)

0 Members and 1 Guest are viewing this topic.

Offline WessexWeather

  • Senior Member
  • **
  • Posts: 83
    • WessexWeather
Appealing to any php experts out there!

In this post...

http://www.wxforum.net/index.php?topic=23310.msg275076#msg275076

...PaulMy designed a nice php script to rename the latest time stamped webcam image uploaded by an IP camera, to a fixed filename to be displayed on a website: -

Quote
<?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 = "/home/XXX/public_html/XXX/XXX/"; // change to suit your system relative to where this script is executed
$pattern = '\.(jpg)$';
//
$newstamp = 0;           
$newname = "";
//
if ($handle = opendir($dir)) {             
       while (false !== ($fname = readdir($handle)))  {           
         // Eliminate current directory, parent directory           
         if (ereg('^\.{1,2}$',$fname)) continue;           
         // Eliminate other pages not in pattern           
         if (! ereg($pattern,$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("/home/XXX/public_html/XXX/XXX/$newname", "/home/XXX/public_html/XXX/XXX/webcamimage.jpg"); // change to suit your system relative to where this script is executed
//
$filesa = glob('/home/XXX/public_html/XXX/XXX/*.jpg'); // change to suit your system relative to where this script is executed
array_walk($filesa,'myunlink');
//
function myunlink($t)
   {
   unlink($t);
   }
//
?>

 It works well on older versions of php (albeit with ereg deprecation warnings) but with php 7.0 installed it throws up the following error: -

Quote
[03-May-2017 17:26:01 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function ereg() in /home/XXX/public_html/XXX/XXX/webcamimagerename.php:20
Stack trace:
#0 {main}
  thrown in /home/XXX/public_html/XXX/XXX/webcamimagerename.php on line 20

This may be due to the fact that ereg has been deprecated. Can anyone re-write this script to do the same thing in later versions of php, or suggest an alternative?

Many thanks in advance.

« Last Edit: October 17, 2017, 07:24:36 AM by WessexWeather »
Cheers,

Simon (WessexWeather)


Offline Jáchym

  • Meteotemplate Developer
  • Forecaster
  • *****
  • Posts: 8605
    • Meteotemplate
Re: Help needed updating php webcam image rename script
« Reply #1 on: May 03, 2017, 03:29:42 PM »
Simon try this:

Code: [Select]
if (preg_match('/^\.{1,2}$/',$fname)) continue;   
and

Code: [Select]
if (! preg_match('/\.(jpg)$/',$fname)) continue;         

Offline WessexWeather

  • Senior Member
  • **
  • Posts: 83
    • WessexWeather
Re: Help needed updating php webcam image rename script
« Reply #2 on: May 03, 2017, 03:42:24 PM »
Hi Jachym,

Do you mean replace the two ereg lines with those two preg_match lines?
Cheers,

Simon (WessexWeather)


Offline Jáchym

  • Meteotemplate Developer
  • Forecaster
  • *****
  • Posts: 8605
    • Meteotemplate
Re: Help needed updating php webcam image rename script
« Reply #3 on: May 03, 2017, 03:42:44 PM »
Yes

Offline WessexWeather

  • Senior Member
  • **
  • Posts: 83
    • WessexWeather
Re: Help needed updating php webcam image rename script
« Reply #4 on: May 03, 2017, 04:02:09 PM »
Jachym...

You are a php genius!  =D&gt;
Cheers,

Simon (WessexWeather)


Offline Jáchym

  • Meteotemplate Developer
  • Forecaster
  • *****
  • Posts: 8605
    • Meteotemplate
Re: Help needed updating php webcam image rename script
« Reply #5 on: May 03, 2017, 04:33:32 PM »
Jachym...

You are a php genius!  =D&gt;

You're welcome

Offline WessexWeather

  • Senior Member
  • **
  • Posts: 83
    • WessexWeather
Re: Help needed updating php webcam image rename script
« Reply #6 on: May 06, 2017, 08:35:06 AM »
The whitebalance on my new cam isn't that great. It doesn't cope well with bright skies particularly.

I have been looking at Fred's ImageMagick Scripts, specifically his autowhite script: -

http://www.fmwconcepts.com/imagemagick/autowhite/index.php

I am no expert at php but is there any way this could be incorporated into my rename script?
Cheers,

Simon (WessexWeather)


Offline Jáchym

  • Meteotemplate Developer
  • Forecaster
  • *****
  • Posts: 8605
    • Meteotemplate
Re: Help needed updating php webcam image rename script
« Reply #7 on: May 06, 2017, 08:51:09 AM »
Hi Simon,
never worked with this before and not all servers will support this. You would also have to contact that guy because he wants everyone to contact him if they want to use this script.
More importantly however, I think the problem with WB is that for each image a different value will work. So you optimize the script for one image and it applies it to a different one and makes it even worse. If the brightness of the image changes during the day, then this is going to be a problem. If you make it darker, then it will be useful for bright images, but make darker images even darker and vice versa.

Offline Bushman

  • Forecaster
  • *****
  • Posts: 7549
    • Eagle Bay Weather
Re: Help needed updating php webcam image rename script
« Reply #8 on: May 06, 2017, 09:44:14 AM »
Contact the author ONLY if you are using his scripts in commercial (i.e. not free/personal) applications is what it says.  (Which is silly since IM is free and the  "scripts" are  not really scripts per se; no "magic" coding involved.  But I digress)

I sed IM a lot for aa gov't client.  Pretty easy to use in PHP, assuming you can install it on the server.  Example:

<?php
exec("/usr/bin/convert input.jpg -bordercolor black -border 10x10 output.jpg");
?>
<img src="output.jpg">

Uses the IM Convert module to create a bordered image.  Etc.
Need low cost IP monitoring?  http://wirelesstag.net/wta.aspx?link=NisJxz6FhUa4V67/cwCRWA or PM me for 50% off Wirelesstags!!

Offline WessexWeather

  • Senior Member
  • **
  • Posts: 83
    • WessexWeather
Re: Help needed updating php webcam image rename script
« Reply #9 on: October 17, 2017, 07:24:15 AM »
Reviving an old topic here...

Is there any way a logo could be added to the image at the same time it is renamed?

Thanks in advance.
Cheers,

Simon (WessexWeather)


Offline droiddk

  • Forecaster
  • *****
  • Posts: 334
Re: Help needed updating php webcam image rename script
« Reply #10 on: October 17, 2017, 08:04:53 AM »
Reviving an old topic here...

Is there any way a logo could be added to the image at the same time it is renamed?

Thanks in advance.

Hi

Have not tried it but it should be possible, try something like this:

exec("/usr/bin/convert photo.png logo.png -gravity southeast -geometry +10+10 -composite output.png");

https://www.imagemagick.org/script/command-line-options.php#gravity
https://www.imagemagick.org/script/command-line-options.php#geometry

Regards


Offline WessexWeather

  • Senior Member
  • **
  • Posts: 83
    • WessexWeather
Re: Help needed updating php webcam image rename script (SOLVED)
« Reply #11 on: October 17, 2017, 04:34:18 PM »
Hi droiddk,

Many thanks for your reply.

I’m afraid I am no expert here... Woukd you be kind enough to clarify where I would enter this into the script above, and how to amend it to use my own folder structure and filenames.
Cheers,

Simon (WessexWeather)


Offline droiddk

  • Forecaster
  • *****
  • Posts: 334
Re: Help needed updating php webcam image rename script (SOLVED)
« Reply #12 on: October 18, 2017, 09:00:41 AM »
Hi droiddk,

Many thanks for your reply.

I’m afraid I am no expert here... Woukd you be kind enough to clarify where I would enter this into the script above, and how to amend it to use my own folder structure and filenames.

Hi Simon

Do you already use server-side imagemagick? If yes then please show your code so far. I do not use it but If you have a working example then we may be able to expand it to suit your needs.

Regards
Droiddk

Offline WessexWeather

  • Senior Member
  • **
  • Posts: 83
    • WessexWeather
Re: Help needed updating php webcam image rename script (SOLVED)
« Reply #13 on: October 18, 2017, 02:35:42 PM »
Thanks Droiddk,

The script is as follows: -

Quote
<?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 = "/home/****/public_html/iegeek/uploads/"; // change to suit your system relative to where this script is executed
$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("/home/****/public_html/iegeek/uploads/$newname", "/home/****/public_html/iegeek/webcamimage/webcamimage.jpg"); // change to suit your system relative to where this script is executed
//
$filesa = glob('/home/****/public_html/iegeek/uploads/*.jpg'); // change to suit your system relative to where this script is executed
array_walk($filesa,'myunlink');
//
function myunlink($t)
{
unlink($t);
}
//
?>

The webcam uploads a time stamped image to the uploads directory. The script webcamimagerename.php is located in the script directory, and the file is renamed to webcaminage.jpg in the webcaminage directory.

It would be wonderful if I could overlay my logo wwlogo100x100.png, currently also located in the scripts directory, onto webcaminage.jpg.

Hope you can help!
Cheers,

Simon (WessexWeather)


Offline droiddk

  • Forecaster
  • *****
  • Posts: 334
Re: Help needed updating php webcam image rename script (SOLVED)
« Reply #14 on: October 18, 2017, 03:07:42 PM »
Hi Simon

If imagemagick is correctly installed in your server this should do it:

Quote
<?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 = "/home/****/public_html/iegeek/uploads/"; // change to suit your system relative to where this script is executed
$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("/home/****/public_html/iegeek/uploads/$newname", "/home/****/public_html/iegeek/webcamimage/webcamimage.jpg"); // change to suit your system relative to where this script is executed
//
$filesa = glob('/home/****/public_html/iegeek/uploads/*.jpg'); // change to suit your system relative to where this script is executed
array_walk($filesa,'myunlink');
//
function myunlink($t)
{
unlink($t);
}
//
?><?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 = "/home/****/public_html/iegeek/uploads/"; // change to suit your system relative to where this script is executed
$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("/home/****/public_html/iegeek/uploads/$newname", "/home/****/public_html/iegeek/webcamimage/webcamimage.jpg"); // change to suit your system relative to where this script is executed
//
$filesa = glob('/home/****/public_html/iegeek/uploads/*.jpg'); // change to suit your system relative to where this script is executed
array_walk($filesa,'myunlink');
//
function myunlink($t)
{
unlink($t);
}
//
exec("/usr/bin/convert webcaminage.jpg wwlogo100x100.png -gravity southeast -geometry +10+10 -composite webcaminagewithlogo.png");
?>

I have just added the imagemagick (with your supplied filenames) part to the last of the script. Run the script from the folder with webcaminage.jpg and wwlogo100x100.png. New image will be webcaminagewithlogo.png.

Please note that I do not use server-side imagemagick myself so example is only by what I can read on the internet.

Regards
« Last Edit: October 18, 2017, 03:09:43 PM by droiddk »

Offline WessexWeather

  • Senior Member
  • **
  • Posts: 83
    • WessexWeather
Re: Help needed updating php webcam image rename script (SOLVED)
« Reply #15 on: October 18, 2017, 03:34:47 PM »
Thank you,

I will try it.

Just to clarify I should edit the script, place it in the webcaminage directory rather than the script directory, and amend the cron job accordingly? The cron job currently runs the script every minute.

Also could webcaminagewithlogo be output as a jpg rather than a png?
Cheers,

Simon (WessexWeather)


Offline droiddk

  • Forecaster
  • *****
  • Posts: 334
Re: Help needed updating php webcam image rename script (SOLVED)
« Reply #16 on: October 18, 2017, 03:38:19 PM »

Just to clarify I should edit the script, place it in the webcaminage directory rather than the script directory, and amend the cron job accordingly? The cron job currently runs the script every minute.

I would make a new .php file with the updated script and setup a cron for that file, disable the existing cron when testing the new.

Also could webcaminagewithlogo be output as a jpg rather than a png?

Yes, that should be possible.

Regards
Droiddk

Offline WessexWeather

  • Senior Member
  • **
  • Posts: 83
    • WessexWeather
Re: Help needed updating php webcam image rename script (SOLVED)
« Reply #17 on: October 18, 2017, 05:54:27 PM »
Thank you so much for your help Droiddk,

This has worked perfectly!

The resulting image with conditions overlay can be seen here: -

http://content.meteobridge.com/cam/ff270973649e081b8f41c2288fcfd859/camplus.jpg

Now this is a long shot, but do you think there is any way of making all or part of the image clickable to my website?
Cheers,

Simon (WessexWeather)


Offline droiddk

  • Forecaster
  • *****
  • Posts: 334
Re: Help needed updating php webcam image rename script (SOLVED)
« Reply #18 on: October 19, 2017, 03:38:41 AM »
Hi Simon

Glad it works.

No, sorry, don't know if it is possible to incorporate a link in a image, but on a homepage you can make the entire image into a link:

<a href="http://www.wessexweather.net/meteotemplate/indexDesktop.php"><img src="http://content.meteobridge.com/cam/ff270973649e081b8f41c2288fcfd859/camplus.jpg"></a>

Regards
Droiddk

Offline WessexWeather

  • Senior Member
  • **
  • Posts: 83
    • WessexWeather
Re: Help needed updating php webcam image rename script (SOLVED)
« Reply #19 on: October 25, 2017, 10:50:34 AM »
Only me again Droiddk!  ](*,)

Would it be possible to set the transparency of the overlaid logo image wwlogo100x100.png to, say, 50%?
Cheers,

Simon (WessexWeather)


Offline droiddk

  • Forecaster
  • *****
  • Posts: 334
Re: Help needed updating php webcam image rename script (SOLVED)
« Reply #20 on: October 25, 2017, 11:47:16 AM »
Quite sure something can be done but I don't know the exact command.

Regards
Droid

Offline ConligWX

  • Forecaster
  • *****
  • Posts: 836
  • #conligwx
    • conligwx.org
Re: Help needed updating php webcam image rename script (SOLVED)
« Reply #21 on: October 25, 2017, 04:05:52 PM »
you could use something like Imagik's "convert" to do most of it thats what I use on my webcam.
Regards Simon
Davis Vantage Pro2 Plus (6162UK) • Daytime FARS • WeatherLink Live • AirLink • PurpleAir PA-II-SD • CumulusMX •