Get Image Height and Width as integer values?

Get Image Height and Width as integer values?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Getting Image Dimensions as Integers in PHP

The getimagesize() function is a staple for inspecting image metadata in PHP, but it often trips up developers expecting simple integers for width and height. While the function successfully retrieves the dimensions, the return value is not always what you anticipate if you treat it strictly as an array of integers. The function returns an array where the first element ([0]) holds the width and the second ([1]) holds the height. However, these values are technically returned as strings, not integers.

This distinction matters significantly when performing arithmetic operations or passing dimensions to APIs that enforce strict type requirements. If you attempt to add two widths together or validate a dimension against a specific integer range without explicit casting, PHP will throw a TypeError in modern versions (8.1+) or produce unexpected results due to string comparison logic.

To resolve this, you must explicitly cast the values. The most straightforward approach is using (int) type juggling on the array elements returned by getimagesize().

$imageData = getimagesize($imagePath);

if ($imageData !== false) {
    $width = (int)$imageData[0];
    $height = (int)$imageData[1];

    // Now these are safe to use in math or strict comparisons
    if ($width + $height > 2000) {
        echo "Large image detected";
    }
}

The reason this happens lies in how getimagesize() works internally. It reads the file header (EXIF data for JPEGs, PNG headers, etc.) and decodes the binary information into a human-readable format. To maintain compatibility across different PHP versions and ensure that the function behaves consistently regardless of server architecture, the standard library returns these values as strings. This historical design choice ensures that legacy scripts relying on string comparisons (common in older CMS architectures) continue to work without modification.

However, relying on implicit type coercion is a fragile pattern. In modern development, especially within frameworks like Laravel, strict typing is enforced by default. Passing string dimensions to a service method expecting integers will break the application immediately if declare(strict_types=1); is active in your configuration.

For projects built with Laravel, you can leverage its helper methods or cast values within your controllers and services. If you are working with the Image facade or creating custom resizing logic, ensuring the dimensions are integers prevents subtle bugs where a string "500" might be treated differently than the integer 500 in conditional logic.

use Illuminate\Support\Facades\Storage;

$sourcePath = Storage::path('uploads/photo.jpg');
$size = getimagesize($sourcePath);

// Explicit casting ensures type safety for API calls or further processing
$dimensions = [
    'width' => (int)$size[0],
    'height' => (int)$size[1],
];

There are scenarios where getimagesize() might return false, indicating that the file does not exist, is not a valid image, or the server lacks permission to read it. Always check for this failure state before attempting to extract dimensions. Attempting to cast false to an integer results in 0, which could silently mask errors if your code doesn't validate the initial return value.

Another consideration is performance when processing large batches of images. While casting a few integers is negligible, wrapping this logic in a loop for thousands of files requires attention to memory usage and error handling. If you are building an image optimization pipeline, ensure that the casted values are used immediately or stored in a typed data structure like a DTO (Data Transfer Object) rather than loose variables to maintain code clarity.

If you find yourself constantly dealing with this type conversion overhead, consider creating a dedicated utility method or a service class that encapsulates the logic of validating the file path and returning a strongly typed ImageSize object containing integer properties. This abstraction keeps your core business logic clean and ensures that image dimensions are always handled consistently throughout the application.

For more advanced needs involving EXIF data extraction beyond just basic dimensions, libraries like getid3 or PHP's native exif_read_data() can provide additional metadata, though they often require GD library support or specific file headers to be present. Always verify that your server environment has the necessary extensions enabled before relying on these functions in production code.

Our network