Solved

GetImageSize(): Why isn't this working?

Posted on 2001-07-10
5
295 Views
Last Modified: 2012-06-21
I'm trying to figure out the size of an image.  According to the PHP manual, I have to use the GetImageSize() function.

Well, for some reason, it's not working.  Here's my coding:

<?PHP
$size = GetImageSize("/userpics/sslcg.jpg"); // LINE 2
//print "<img src=\"/userpics/sslcg.jpg\">";
//The above line printed the picture correctly.
?>

I know that this picture exists in the userpics directory and everything is spelled correctly.  Below is the error I get:

Warning: Unable to open /userpics/sslcg.jpg in /var/www/html/test.php on line 2

I'm a little puzzled.  Can you someone help me out?

Thanks,
Mike
0
Comment
Question by:myuen
5 Comments
 
LVL 1

Expert Comment

by:anupkarade
ID: 6272060
hi...
   try this different approch...

Please follow these steps    

1. Paste this code in a file
it contains the browse button for Image selection
and a submit button.


<HTML>
<HEAD>
<TITLE>Upload example</TITLE>
</HEAD>
<BODY>

<FORM METHOD="post" ACTION="resultUpload.php" ENCTYPE="multipart/form-data">

<p><strong>File</strong><br>
<INPUT TYPE="file" NAME="img1" SIZE="30"></p>

<P><INPUT TYPE="submit" NAME="submit" VALUE="Send"></p>

</FORM>

</BODY>
</HTML>


::Step 2

resultUpload.php
this is the page you called on submission
here you get all the image related properties...
<?
   print("$img1_type <BR>");
   print("$img1_size <BR>");
?>



:: If you want to open the image on an button click event
   or a href use the javascript function

   window.open('Yourpage.php','Window Name','toolbar=no location =no,addressbar=no');

::: Then in the Yourpage.php paste the following script

<?
   print("<HTML>");
   print("<HEAD>");
  print("<TITLE>Image</TITLE>");
  print("</HEAD>");
  print("<BODY>");
   
  print("<img src=".\Yourimage.jpg">")  
  print("</BODY>");
  print("</HTML>");

?>
   
Also note that you can set the height,width and position of window and image as well

               
                         regards
                                Anup


0
 
LVL 8

Expert Comment

by:us111
ID: 6272168
where's this folder : /userpics ? in /var/www/html/ ???
0
 
LVL 1

Author Comment

by:myuen
ID: 6272973
The directory structure is as follows for the sslcg.jpg picture I want to open:

/var/www/html/userpics/sslcg.jpg
0
 
LVL 1

Expert Comment

by:manucorp
ID: 6274050
<?PHP
$size = GetImageSize("/userpics/sslcg.jpg"); // LINE 2
//print "<img src=\"/userpics/sslcg.jpg\">";
//The above line printed the picture correctly.
?>

give up the / else you are an absolute file description

<?PHP
$size = GetImageSize("userpics/sslcg.jpg"); // LINE 2
//print "<img src=\"/userpics/sslcg.jpg\">";
//The above line printed the picture correctly.
?>

if file is /var/www/html/test.php

if you want to be sure

<?PHP
$size = GetImageSize("/var/www/html/userpics/sslcg.jpg"); // LINE 2
//print "<img src=\"/userpics/sslcg.jpg\">";
//The above line printed the picture correctly.
?>

0
 

Accepted Solution

by:
roglewis earned 50 total points
ID: 6275061
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");

that's your answer!
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

825 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