Using PHP getimagesize() to Retrieve Image Dimensions

The article explains PHP's getimagesize() function, which determines the dimensions, type, and MIME information of various image formats, describes its return values, and provides a complete code example demonstrating how to retrieve image size and output the image with appropriate headers.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Using PHP getimagesize() to Retrieve Image Dimensions

The getimagesize() function in PHP can determine the size, dimensions, and file type of a wide range of image formats such as GIF, JPG, PNG, BMP, and more, returning an array that includes the width, height, and MIME type.

Its return value is an indexed array where index 0 holds the image width and index 1 holds the image height; additional information such as the MIME type is available in the associative element 'mime'.

Example usage:

<?php
$size = getimagesize($filename);
$fp = fopen($filename, "rb");
if ($size && $fp) {
    header("Content-type: {$size['mime']}");
    fpassthru($fp);
    exit;
} else {
    // error handling
}
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPfile-handlinggetimagesizeimage dimensions
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.