How can GD be used to read an icon in PHP?

To read an icon using GD in PHP, you can use the imagecreatefromstring function to create an image resource from the icon file. This function supports various image formats, including ICO files which are commonly used for icons. Once you have the image resource, you can manipulate or display the icon as needed.

$iconData = file_get_contents('icon.ico');
$iconImage = imagecreatefromstring($iconData);

if($iconImage !== false){
    // Icon loaded successfully, you can now manipulate or display it
    // For example, you can output the icon as a PNG image
    header('Content-Type: image/png');
    imagepng($iconImage);
    
    // Free up memory by destroying the image resource
    imagedestroy($iconImage);
} else {
    echo 'Failed to load icon.';
}