Search code examples
phphtmlimagescandirimage-size

get image height and width with php


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 . '"/>';
                    }
            }
?>

Solution

  • <?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 . '"/>';
                        }
                }
    ?>