What best practices should be followed when retrieving image dimensions for use in a popup using PHP?

When retrieving image dimensions for use in a popup using PHP, it is important to ensure that the image exists and is accessible before attempting to retrieve its dimensions. This can be achieved by using functions like `file_exists()` and `getimagesize()`. It is also recommended to sanitize the input to prevent any potential security vulnerabilities.

$image_path = 'path/to/image.jpg';

if (file_exists($image_path)) {
    $image_info = getimagesize($image_path);
    
    if ($image_info !== false) {
        $image_width = $image_info[0];
        $image_height = $image_info[1];
        
        // Use the image dimensions in your popup code
    } else {
        echo 'Unable to retrieve image dimensions.';
    }
} else {
    echo 'Image not found.';
}