What are some common pitfalls when using the getImageSize() function in PHP and how can they be avoided?

Common pitfalls when using the getImageSize() function in PHP include not checking if the function returns false (indicating an error) before attempting to access the image dimensions, and not handling cases where the function may not work with certain image types. To avoid these pitfalls, always check for false return values and use additional functions like exif_imagetype() to verify the image type before calling getImageSize().

$imagePath = 'image.jpg';

// Check if the file exists
if (file_exists($imagePath)) {
    // Get the image type
    $imageType = exif_imagetype($imagePath);

    // Check if the image type is supported
    if ($imageType) {
        // Get the image size
        $imageSize = getImageSize($imagePath);

        // Check if the function returned false
        if ($imageSize !== false) {
            // Access the image dimensions
            $width = $imageSize[0];
            $height = $imageSize[1];
            
            // Use the dimensions as needed
            echo "Image dimensions: $width x $height";
        } else {
            echo "Error getting image size.";
        }
    } else {
        echo "Unsupported image type.";
    }
} else {
    echo "File not found.";
}