LinkStack/resources/views/components/favicon.blade.php

31 lines
1.1 KiB
PHP
Raw Normal View History

2022-11-24 17:23:49 +01:00
<?php
2023-07-10 18:35:20 +02:00
use App\Models\Link;
2023-11-23 15:19:10 +01:00
if (!function_exists('getFavIcon')) {
function getFavIcon($id)
{
try {
$link = Link::find($id);
$url = $link->link;
2023-11-23 15:19:10 +01:00
// Use Google's Favicon API
$faviconUrl = 'http://www.google.com/s2/favicons?sz=256&domain=' . $url;
2023-11-23 15:19:10 +01:00
// Get the favicon and save it to the desired location
$favicon = file_get_contents($faviconUrl);
$filename = $id . '.png';
2023-11-23 15:19:10 +01:00
$filepath = base_path('assets/favicon/icons') . '/' . $filename;
file_put_contents($filepath, $favicon);
2023-11-23 15:19:10 +01:00
return url('assets/favicon/icons/' . $filename);
2023-11-23 15:19:10 +01:00
} catch (Exception $e) {
// Handle the exception by copying the default SVG favicon
$defaultIcon = base_path('assets/linkstack/icons/website.svg');
$filename = $id . '.svg';
$filepath = base_path('assets/favicon/icons') . '/' . $filename;
copy($defaultIcon, $filepath);
2023-07-10 18:35:20 +02:00
2023-11-23 15:19:10 +01:00
return url('assets/favicon/icons/' . $filename);
}
2023-07-10 18:35:20 +02:00
}
}