?
Solved

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

Posted on 2003-03-12
7
Medium Priority
?
210 Views
Last Modified: 2010-04-06
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?
0
Comment
Question by:rellerbe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 7

Expert Comment

by:markhoy
ID: 8126619
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
 

Author Comment

by:rellerbe
ID: 8131796
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
 
LVL 7

Expert Comment

by:markhoy
ID: 8135258
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:rellerbe
ID: 8145121
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
 
LVL 7

Expert Comment

by:markhoy
ID: 8146106
$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
 
LVL 7

Accepted Solution

by:
markhoy earned 200 total points
ID: 8146303
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
 

Author Comment

by:rellerbe
ID: 8146820
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question