How to get image width and height on a group of image in PHP

46 views Asked by At

I have to get the width and height of hundreds of images.

I got this php to work on one image:

<?php list($width, $height) = getimagesize($_SERVER['DOCUMENT_ROOT'] ."/path/file.jpg"); ?>
<img src="/path/file.jpg" alt="alt text" width="<?= $sizing_width ?>" height="<?= $sizing_height ?>" />

I would like to modify it so that I wouldn't have to do this for each image, but have one php command on a list of image. Is that possible? After it's done, I plan to copy the output from the browser and then delete the command.

1

There are 1 answers

3
Asis On BEST ANSWER

You could do something like this:

<?php
$paths = [
    $_SERVER['DOCUMENT_ROOT'] ."/path/file.jpg",
    ...
];
foreach ($paths as $path) {
    [$width, $height] = getimagesize($path);
?>
    <img src="<?= str_replace($_SERVER['DOCUMENT_ROOT'], '', $path) ?>" alt="alt text" width="<?= $width ?>" height="<?= $height ?>" />
<?php
}
?>

Basically define all your image paths in a list and then loop over them printing out a new image with the sizes like you need.