get image height and width with php

6.7k views Asked by At

i have a little script that scans a directory and then echos out a list of all files (jpg's) in that directory and lists it into html image links. it works pretty well.

is there a way i can get the image dimentions for each individual jpg?

i need my output to look something like this.

<img src="albm/1.jpg" width="333" height="460" />

<img src="albm/2.jpg" width="256" height="560" />

<img src="albm/3.jpg" width="327" height="580" />

here is my current script without the image dimentions.

<?php
    $albm = $_REQUEST['albm'];
            $dir = 'albums/'.$albm.'/';
            $files = scandir($dir);
            arsort($files);
            foreach ($files as $file) {
                    if ($file != '.' && $file != '..') {
                            echo '<img src="' . $dir . $file . '"/>';
                    }
            }
?>
3

There are 3 answers

0
Jeppe On BEST ANSWER
<?php
    $albm = $_REQUEST['albm'];
            $dir = 'albums/'.$albm.'/';
            $files = scandir($dir);
            arsort($files);
            foreach ($files as $file) {
                    if ($file != '.' && $file != '..') {
                            $info = getimagesize($dir . $file);
                            $width = $info[0];
                            $height = $info[1];
                            echo '<img src="' . $dir . $file . '"/>';
                    }
            }
?>
0
shak On

Here how to use it:

        $size = getimagesize($path);
        $width = $size[0];
        $height = $size[1]; 
0
Mehmet Eren Yener On

You can simple use getimagesize(image location) function, it returns width,height,type and attributes. Here is the example

<?php    
    list($width, $height, $type, $attr) = getimagesize("image_name.jpg");
    echo "Image width " .$width;
    echo "<BR>";
    echo "Image height " .$height;
    echo "<BR>";
    echo "Image type " .$type;
    echo "<BR>";
    echo "Attribute " .$attr;
?>