Get image size values with PHP

Get image size values with PHP

by adesignguy

Posted on 18/02/2016 18:54:34 | Code

This is especially useful if you need to grab either width and height values from an image that you need to display when implementing rich snippets and you need singular values (for one example).


//lets set up some variables

$dir = $_SERVER['DOCUMENT_ROOT'].'/images/';
$image = $dir.$row['image1']; //example for imagename stored in database ie: picture1.jpg

//check if image exists

  if (file_exists($image) && $image !== $dir) {
  list($width, $height) = getimagesize($image);

//return the values

echo $width;

echo $height;