Now, Ive got something like this:

Code:

<?php
if($img = @GetImageSize("image.jpg"))
{
   echo "images exist, heres some info";
   echo "width = $img[0]<br>";
   echo "height = $img[1]<br>";
}
else
{
   echo "N/A";
}
?>

Q1: How to make this script reading image` size for any image, not just one?

Q2: And how would look the code to call and print the value for that sript on the page?

...oh and is there a way to print the value of this script inside a CSS div class` contents?

Sorry for PHP but the more I post the more a chance that I got the right answer))))

1 answer

o.k.w 2355
0
points

The function only accept a single filename. However you can get the image size from more files by running a loop and passing in an array of the image name or iterating the files of an folder.

Here's an example of getting image size of all the files in a folder called 'gfx':

<?php
$folder = 'gfx/';
if ($handle = opendir($folder)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            echo "$file<br/>"; //print filename
            if($img = @GetImageSize($folder . $file))
            {
               echo "images exist, heres some info:<br/>";
               echo " width = $img[0]<br/>";
               echo " height = $img[1]<br/>";
            }
            else
            {
               echo "N/A<br />";
            }
        }
    }
    closedir($handle);
}
?>

The output will be something like this:

image1.png
images exist, heres some info:
width = 20
height = 1200
image2.jpg
images exist, heres some info:
width = 200
height = 200
Answered over 9 years ago by o.k.w
  • Whoa this is nice!!!, but it turns out that I will have plain text on the screen running this script through a folder with JPGs files. Is there any way I could move a certain output(dimension)of a single jpg file to an another php page(just the output)? For e i: to give textual description of some image width and height on some page in a certain location... Megaman over 9 years ago
  • It's possible, but you might have to get your hands dirty with javascript ajax calls and a php file to serve out these info. o.k.w over 9 years ago
  • I guess its not that simple after all.... Megaman over 9 years ago
  • If you can get the hang of it, could be pretty straightforward :P o.k.w over 9 years ago
  • Yeah thats what im trying now...) Megaman over 9 years ago
  • I now that javascript isnt so hard, as for ajax I dunno...how long it would take before I can write more or less diffucult code?) Megaman over 9 years ago