Link to home
Start Free TrialLog in
Avatar of rellerbe
rellerbe

asked on

Having problem with getimagesize when the image is not there I get an error.

I am trying to write a PHP script that check to see if my webcam is on and if it isn't it redirects to a offline webcam page.

I have the code get the image with getimagesize and then do an if statement to see if the image array is equal to zero and if it is redirect the user to the offline webcam page.

It works with no errors when the webcam is on, but all kind of errors when I turn the webcam off.

Errors:
Warning: php_hostconnect: connect failed in /home/virtual/site191/fst/var/www/html/webcam/decide-image.php on line 7

Warning: getimagesize: Unable to open 'http://4.64.54.81:8080/cam_1.jpg' for reading. in /home/virtual/site191/fst/var/www/html/webcam/decide-image.php on line 7

Warning: Cannot add header information - headers already sent by (output started at /home/virtual/site191/fst/var/www/html/webcam/decide-image.php:7) in /home/virtual/site191/fst/var/www/html/webcam/decide-image.php on line 11


Here is the code:

$offlineurl = "offlinecam.htm";
$onlineurl = "cam.htm";

$image = getimagesize("http://4.64.54.81:8080/cam_1.jpg");
$width=$image[0];
$height=$image[1];    
     if ($width == 0) {
          header("Location: $offlineurl");
          }
What I am doing wrong?
Avatar of markhoy
markhoy
Flag of United Kingdom of Great Britain and Northern Ireland image

this:

Warning: php_hostconnect: connect failed in /home/virtual/site191/fst/var/www/html/webcam/decide-image.php on line 7


means you need to put error checking around your code at least:

if ($image){
#do stuff
}else{
print "can't read from camera"
}

what file/ folder permssions do you have on cam1.jpg?



Avatar of rellerbe
rellerbe

ASKER

Well I dont know what permissions I have because the webcam program act as its own webserver.  I tried what you said with the error checking and now I don't get an error but the code doesn't seem to work right.

$offlineurl = "phptest1.html";
$onlineurl = "phptest2.html";

$get_image ="http://4.64.54.81:8080/cam_1.jpg";
$image = getimagesize($get_image);
$width=$image[0];
$height=$image[1];    
     if ($width == 0) {
          header("Location: $offlineurl");
          } else {
     header("Location: $onlineurl");
     }

Any ideas?
can you use getimagesize on an image on the same server as you are running this script? ie get the script to work on a local image file first.

$get_image ="cam_1.jpg"; # put an image called cam_1.jpg in the same folder as this script
The code works, but now if I delete the image it gives me the error. Do I need to use fopen instead, or is there another way to see if the image is there and if it isn't it won't give me an error.
$image= "cam_1.jpg";

if (-e $image){
print "it exists"
}else{
print "there is no file"
}

checks for existence of the file on your local server; haven't tried it using http:// ref. Give it a try!
ASKER CERTIFIED SOLUTION
Avatar of markhoy
markhoy
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help.  You put me in the right direction. I got it to work with the @Getimagesize to supress the errors.  It seems to work well now, so thanks for helping.