Using PHP to get the dimensions of an image
By Mike Street
The following code will get the dimensions of an image:
<?php
list($width, $height, $type, $attr) = getimagesize(PATH_TO_IMAGE);
I found this useful when using a lightbox to load an image that I didn't initially know the size of, to ensure the lightbox positioned itself in the middle, based on the height and width of the image.
The contents of $attr
is useful for slotting straight into an <img>
tag, like so:
<img src="URL" <?php echo $attr?> />
This will produce, for example:
<img src="URL" width="200" height="300">