How to Get the Dimensions of a Picture in PHP
PHP is a programming language used primarily to create dynamic web pages that interact with a user on the front-end and a server on the back-end. The nature of a dynamic web page is that it changes, and those changes often include images and their sizes. If the placeholder for an image is not the proper size for the image, it won't display in the manner you are expecting. Fortunately, PHP has a method that returns pieces of information about an image, including its dimensions.
Instructions
-
-
1
Launch your PHP code editor and open your PHP file.
-
2
Find the location in your code where you want to find the dimensions of an image.
-
-
3
Type "$imageSize = getimagesize ("filename.jpg");" to call the "getimagesize" function on an image called "filename.jpg." The result of this function is an array with seven pieces of information. Array location "0" is the width of the image and array location "1" is the height of the image. Array location "3" contains a string with the width and height already formatted for an IMG tag.
-
4
Type "print_r ($imageSize);," below the getimagesize function, to output the contents of the imageSize array when the PHP code is run and view the results of the getimagesize function, including the image dimensions. Save your file.
-
1