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?
rellerbeAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
markhoyConnect With a Mentor Commented:
http://www.experts-exchange.com/Web/Web_Languages/PHP/PHP_Databases/Q_20493705.html

http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_20473128.html

Whenever you reference a file from PHP (whether through GetImageSize or ReadFile or whatever), you need to provide the full file name, not just it's location under the document root.

Understand that PHP is installed on the machine, and it will access files as if you referenced them from a command line.  It is NOT limited to accessing the webserver's document root, which is one of the things that makes PHP so cool.

In your above code, the correct call would be:
$size = GetImageSize("/var/www/html/userpics/sslcg.jpg");

Assuming that the DocumentRoot of your webserver is "/var/www/html", you can also use:
$size = GetImageSize($DOCUMENT_ROOT."/userpics/sslcg.jpg");

(http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_20148941.html)

or, maybe:
$image = "whatever.gif";
$image_size = getimagesize($image);
print("<IMG SRC=\"$image\" image_size[3]>\n");


$image_size[0] is width in pixel
$image_size[1] is height
$image_size[2] is type (1,gif, 2,jpg, 3,png)
$image_size[3] is a string
height=# width=#


0
 
markhoyCommented:
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?



0
 
rellerbeAuthor Commented:
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?
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
markhoyCommented:
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
0
 
rellerbeAuthor Commented:
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.
0
 
markhoyCommented:
$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!
0
 
rellerbeAuthor Commented:
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.
0
All Courses

From novice to tech pro — start learning today.