I am building a template that displays image(s) from IP camera(s).
Every camera has different IP address from where to request its image.
I would like to hide the IP address in Sage 9 front-end and create + show the camera image path in cameras page template something like: https://mysite.com/cameras/webcam1.jpg
I would appreciate any ideas, suggestions, help how to achieve this or implement it in Roots full stack environment.
Below is the code that did the magic until now …
<?php
function update() {
file_put_contents(realpath(dirname(__FILE__) . "/cameras/webcam1.jpg"), file_get_contents("http://ip.to.hide.in.front.end/record/current.jpg?rand=".rand(1,999999)));
}
... some setTimeInterval stuff
echo 'refreshed image' on template-cameras.blade.php;
This is more of a WordPress question than a Roots question, really, but you could look at programmatically importing the images into WordPress, so that the camera isn’t involved in displaying the images.
I would be a bit cautious about importing all the images in WordPress, especially if the camera updates the image very frequently and/or the code that imports the image is called a lot (like on page load). Unless you actually want a media library full of every image retrieved from the cameras, it could be a bit of a headache later…
It sounds like all you really is a URL mask for each camera, so you could even do this at the server level with mod_rewrite or whatever URL rewriting library is appropriate to your server.
If the camera IP addresses don’t need to be editable by normal WordPress users, you could even create individual rules for each camera in your .htaccess file (assuming you use Apache).
With mod_rewrite you can proxy the request ([P]) and also pass a variable for the time (%{TIME}) if you want a unique URL like in your example. See here for more details:
You could also make a more dynamic version that will take the camera’s number and pass it to a script, which could then be responsible for fetching and returning the image:
Your fetch.php could contain the code you posted originally with a switch statement for each camera. You could save the file locally or simply stream it out to the user. Just make sure you set the appropriate HTTP headers for an image and also set whatever caching you want for your “image”.
PS: I didn’t actually test the above mod_rewrite rules and it’s not my strong point but hopefully it gives you a starting point.
I don’t have that much experience with Nginx but if you’re doing a straight proxy, maybe you will need an encrypted reverse proxy, unless unless Nginx automatically will fetch it from HTTP and then encrypt it to send to the client via HTTPS. I really don’t know but you could always try the simplest version.
If you were to use PHP via file_get_contents(), the image would be fetched internally by the server and then if you streamed it back to the client, I think that would encrypt it if you had a secure connection already. However, it’s probably much less efficient to pass the data through PHP so I’d try the Nginx solution first.
then I updated server conf just for nginx includes ansible-playbook server.yml -e env=production --tags nginx-includes
in wordpress admin I created new page with url webcam
https://mysite.com/webcam
I have proxied url there along with the camera image there with src="" attribute …
… which I can use in front-end on mysite with help of some javascript
<img ... src="https://mysite.com/webcam">
in front-end I use JS to display static image on mysite.com front-page template and then refresh it inside the modal when modal is open and end refreshing when modal is closed