Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fyrye/d0b0d1e8672e255beec225d458848b36 to your computer and use it in GitHub Desktop.
Save fyrye/d0b0d1e8672e255beec225d458848b36 to your computer and use it in GitHub Desktop.
Font Awesome File Icons: Mapping MIME Types to correct icon classes
<?php
/**
* Get font awesome file icon class for specific MIME Type
* @see https://gist.github.com/guedressel/0daa170c0fde65ce5551
* @param string|null $mime_type
* @return string
*/
function font_awesome_file_icon_class(?string $mime_type): string
{
// List of official MIME Types: http://www.iana.org/assignments/media-types/media-types.xhtml
static $font_awesome_file_icon_classes = [
// Images
'image' => 'fa-file-image',
// Audio
'audio' => 'fa-file-audio',
// Video
'video' => 'fa-file-video',
// Documents
'application/pdf' => 'fa-file-pdf',
'application/msword' => 'fa-file-word',
'application/vnd.ms-word' => 'fa-file-word',
'application/vnd.oasis.opendocument.text' => 'fa-file-word',
'application/vnd.openxmlformats-officedocument.wordprocessingml' => 'fa-file-word',
'application/vnd.ms-excel' => 'fa-file-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml' => 'fa-file-excel',
'application/vnd.oasis.opendocument.spreadsheet' => 'fa-file-excel',
'application/vnd.ms-powerpoint' => 'fa-file-powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml' => 'ffa-file-powerpoint',
'application/vnd.oasis.opendocument.presentation' => 'fa-file-powerpoint',
'text/plain' => 'fa-file-alt',
'text/html' => 'fa-file-code',
'application/json' => 'fa-file-code',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'fa-file-word',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'fa-file-excel',
'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'fa-file-powerpoint',
// Archives
'application/gzip' => 'fa-file-archive',
'application/zip' => 'fa-file-archive',
'application/x-zip-compressed' => 'fa-file-archive',
// Misc
'application/octet-stream' => 'fa-file-archive',
];
return $font_awesome_file_icon_classes[$mime_type] ??
$font_awesome_file_icon_classes[\substr($mime_type, 0, \strpos($mime_type, '/') ? : \strlen($mime_type))] ??
'fa-file';
}
@fyrye
Copy link
Author

fyrye commented Feb 20, 2019

Updated to php >= 7.0 with type-hinting, null coalescing operator ?? and substr to retrieve value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment