Get the size of an image using a PHP function

PHP Image Processing

June 3, 2017

Get the size of an image using a PHP function

PHP provides a function to get the width and height of an image.
getimagesize()
Get the size of an image.
Description
The getimagesize() function will determine the size of any supported given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag and the correspondent HTTP content type.
getimagesize() can also return some more information in imageinfo parameter.

Example:
This is a sample code, change img_name to your image name and see result!

<?php
list($width, $height, $type, $attr) = getimagesize("img/flag.jpg");
echo "<img src=\"img/flag.jpg\" $attr alt=\"getimagesize() example\" />";
?>
<?php
$size = getimagesize("https://webtaxonomy.com/wp-content/uploads/2017/05/webtaxonomy-logo.png");

// if the file name has space in it, encode it properly
$size = getimagesize("https://webtaxonomy.com/wp-content/uploads/2017/05/webtaxonomy-lo%20go.png");

?>
<?php

list($width, $height, $type, $attr) = getimagesize("image_name.jpg");

echo "Image width " .$width;
echo "<BR>";
echo "Image height " .$height;
echo "<BR>";
echo "Image type " .$type;
echo "<BR>";
echo "Attribute " .$attr;

?>

When you run this script the result you will see like this

Image width 379
Image height 344
Image type 2
Image attribute width=”379″ height=”344″

You will get the width, height, type of an image and also attribute of an image, I use this function in my image upload form.
Type of an image you can see from table below.

Type of Image

1 = GIF 5 = PSD 9 = JPC 13 = SWC
2 = JPG 6 = BMP 10 = JP2 14 = IFF
3 = PNG 7 = TIFF(intel byte order) 11 = JPX 15 = WBMP
4 = SWF 8 = TIFF(motorola byte order) 12 = JB2 16 = XBM

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts

Tags

Graphics ManipulationPHP FunctionsServer-Side Scripting

Related Posts

Many new updates happened for Android developers lately after Google I/O. Initially there was no restriction on some features but now.
phpMyAdmin is a web-based database management tool that you can use to view and edit the MySQL databases on your EC2 instance.
Title attribute can be helpful to expand on the meaning of your navigation label and give your users more context You can provide to the link.