• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php getimagesize – Get Height, Width and Image File Type

php getimagesize – Get Height, Width and Image File Type

December 15, 2021 Leave a Comment

To get the image height, image width, and image file type in php, we can use the php getimagesize() function.

$image_information = getimagesize("sample_image.png");
print_r($image_information);

// Output:
Array ( [0] => 500
        [1] => 250 
        [2] => 3 
        [3] => width="500" height="250" 
        [bits] => 8 
        [mime] => image/png )

Many times when designing web pages, we work with images and need to ensure that we are serving the appropriate size and type of image.

With the php getimagesize() function, we can pass a valid image URL and get the image height, image width, and image mime type.

From the php documentation, all we need is a valid file name to call getimagesize() and if the file name is valid, we will receive an array of the image information.

getimagesize(string $filename, array &$image_info = null): array|false

For most image types, the getimagesize() returns an array which contains the image height, image width, image HTML attr string, and image mime type.

If we want to just see this information, we can use the php print_r() function.

$image_information = getimagesize("sample_image.png");
print_r($image_information);

// Output:
Array ( [0] => 500
        [1] => 250 
        [2] => 3 
        [3] => width="500" height="250" 
        [bits] => 8 
        [mime] => image/png )

If you want to use the variables in the return array, we can use the list php language construct.

$image_information = getimagesize("sample_image.png");
list($width, $height, $type, $attr) = $image_information;

echo "Width: " . $width  . "\n";
echo "Height: " . $height  . "\n";
echo "Type: " . $type  . "\n";
echo "Attr: " . $attr  . "\n";

//Output:

Width: 500
Height: 250
Type: 3
Attr: width="500" height="250"

Using getimagesize() with a URL in php

We can use the php getimagesize() function to get the image size information from any valid file including those online.

Let’s say I have the following image:

gears

If I wanted to get the information from the following image on this website, I can do that in the following way in php:

$image_information = getimagesize("http://theprogrammingexpert.com/wp-content/uploads/2021/11/tpe-main.png");
list($width, $height, $type, $attr) = $image_information;

echo "Width: " . $width  . "\n";
echo "Height: " . $height  . "\n";
echo "Type: " . $type  . "\n";
echo "Attr: " . $attr  . "\n";

//Output:

Width: 500
Height: 497
Type: 3
Attr: width="500" height="497"

One thing to note here is that you may have to enable the ability for your php server to handle url_fopen in the php.ini file (from Stack Overflow answer):

allow_url_fopen = 1 // 0 for Off and 1 for On Flag
allow_url_include = 1 // 0 for Off and 1 for On Flag

Hopefully this article has been beneficial for you to understand how to use getimagesize() to get image information in php.

Other Articles You'll Also Like:

  • 1.  echo php – Output One or More Strings
  • 2.  Truncate Strings in php Using substr()
  • 3.  php array_combine() – Create New Array Given Arrays of Keys and Values
  • 4.  Get Query String from URL in php
  • 5.  php atan – Find Arctangent and Inverse Tangent of Number
  • 6.  php var_dump() Function – Print Information about Variable Type and Value
  • 7.  How to Create an Empty Array in php
  • 8.  php sleep() Function – Pause Execution of Code in php
  • 9.  php array_flip() – Flip the Keys and Values of an Array in php
  • 10.  php array_pop() – Remove Last Element from Array

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy