Skip to content

Instantly share code, notes, and snippets.

@onigetoc
Last active October 11, 2018 15:23
Show Gist options
  • Save onigetoc/732554cb3b9de053ad0aae7f01b01648 to your computer and use it in GitHub Desktop.
Save onigetoc/732554cb3b9de053ad0aae7f01b01648 to your computer and use it in GitHub Desktop.
Get Favicon V3
<?php
/********* LIVE DEMO: https://goo.gl/shr5Cv *********/
echo "<h1>Get Favicon</h1>";
//$url = "https://mrmondialisation.org";
//$url = "http://stackoverflow.com/";
//$url = "http://changera.blogspot.ca/";
//$url = "http://veteranstoday.com";
//$url = "http://2012un-nouveau-paradigme.com/"; // No favicon
//$url = "http://www.stopmensonges.com/";
//$url = "http://reseauinternational.net";
//$url = "http://revision3.com/diggnation/"; // should be http://revision3.com
//$url = "https://en.blog.wordpress.com/";
$url = "https://mrmondialisation.org/favicon.ico";
$favicon = getFavicon($url);
echo $favicon ;
echo "<br>";
$rss_icon = "https://s14.postimg.org/5ig97nc69/feed_icon.png";
echo '<img src="' . $favicon . '" onerror="this.src=\''.$rss_icon.'\';">';
function getFavicon($image_url) {
// Get base URL
$urlData = parse_url($image_url);
$url = $urlData[ "scheme" ] . "://" . $urlData[ "host" ];
// First check if image with favicon.ico exist (It will work 8 out 10 times)
$faviconURL = $url."/favicon.ico";
$imgURL = checkImageSize($faviconURL);
if ($imgURL) {
return $imgURL;
} else {
//return false;
return urlSheme($image_url);
}
//return $imgURL ;
}
/*************************************/
echo "<br>";
// First check if image with .ico exist (It will work 8 out 10 times)
function checkImageSize($image_url) {
//getimagesize($image_url); // for showing error
echo "checkImageSize<br>";
if ($data = @getimagesize($image_url)) {
return $image_url;
} else {
//return false;
return parseFavicon($image_url);
}
}
function urlSheme($url) {
echo "urlSheme<br>";
$urlData = parse_url($url);
$url = $urlData[ "scheme" ] . "://" . $urlData[ "host" ];
//$html = file_get_contents($url);
$html = getContent($url);
$link = parseFavicon($html);
$path = $link;
if ((substr($link, 0, 7) == 'http://') || (substr($link, 0, 8) == 'https://')) {
// the current link is an "absolute" URL - parse it to get just the path
$parsed = parse_url($link);
$path = $parsed['path'];
}
if(substr($url, -1) == '/') {
$url = substr($url, 0, -1);
}
$re = '#^(?:https?:)?//(?:[a-z0-9-]+\.)*((?:[a-z0-9-]+\.)[a-z]+)#i';
// Get full URL Path
if (preg_match($re, $path)) $url = "";
$favicon = $url . $path;
return $favicon;
}
function parseFavicon($html) {
echo "parseFavicon<br>";
// Get the 'href' attribute value in a <link rel="icon" ... />
// Also works for IE style: <link rel="shortcut icon" href="http://www.example.com/myicon.ico" />
// And for iOS style: <link rel="apple-touch-icon" href="somepath/image.ico">
$matches = array();
// Search for <link rel="icon" type="image/png" href="http://example.com/icon.png" />
preg_match('/<link.*?rel=("|\').*icon("|\').*?href=("|\')(.*?)("|\')/i', $html, $matches);
if (count($matches) > 4) {
return trim($matches[4]);
}
// Order of attributes could be swapped around: <link type="image/png" href="http://example.com/icon.png" rel="icon" />
preg_match('/<link.*?href=("|\')(.*?)("|\').*?rel=("|\').*icon("|\')*?/i', $html, $matches);
if (count($matches) > 2) {
return trim($matches[2]);
}
// No match
return null;
}
// Get HTML content
function getContent($url) { // Find RSS feed in a web page url - a litle bit slower
echo "getContent Curl<br>";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_TIMEOUT,1000);
curl_setopt($ch, CURLOPT_URL, urldecode($url));
$content = curl_exec($ch);
if( ($content = curl_exec($ch) ) === false)
{
$content = @file_get_contents($url);
if($content === FALSE) {
// handle error here...
echo "error: file_get_contents <br>";
}
return $content ;
}
else
{
return $content ;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment