Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
getimagesize() function in PHP
The getimagesize() function in PHP is used to retrieve the dimensions and other information about an image file. It can work with both local files and remote URLs, returning an array containing width, height, image type, and additional metadata.
Syntax
getimagesize(file_name, img_info)
Parameters
file_name − The path to the image file (local or remote URL)
img_info (optional) − An array to store extended information from the image file. Currently supports only JFIF files
Return Value
The function returns an indexed array with the following elements ?
- [0] − Image width in pixels
- [1] − Image height in pixels
- [2] − Image type constant (IMAGETYPE_XXX)
- [3] − HTML width/height attributes string
- bits − Number of bits per color
- mime − MIME type of the image
Example
Here's how to get image information from a remote URL ?
<?php
$image_info = getimagesize("http://www.tutorialspoint.com/images/tp-logo-diamond.png");
print_r($image_info);
?>
Array
(
[0] => 205
[1] => 120
[2] => 3
[3] => width="205" height="120"
[bits] => 8
[mime] => image/png
)
Accessing Individual Values
You can access specific image properties directly ?
<?php
// Create a simple image data string (1x1 PNG)
$png_data = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==');
// Save to temporary file
$temp_file = 'data://image/png;base64,' . base64_encode($png_data);
$info = getimagesize($temp_file);
echo "Width: " . $info[0] . "
";
echo "Height: " . $info[1] . "
";
echo "MIME Type: " . $info['mime'] . "
";
?>
Conclusion
The getimagesize() function is essential for validating and processing images in PHP applications. It provides comprehensive image metadata without loading the entire image into memory, making it efficient for image handling tasks.
