SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Using PHP to grab the favicon from a website

    How do I program a PHP script to grab the favicon from a given site and create a new png image of it?

    I am familiar with creating thumbnails of uploaded images, but I don't know how to grab one from a website or how to handle ico files.

  2. #2
    SitePoint Enthusiast Fallen Angel's Avatar
    Join Date
    Sep 2006
    Location
    UK
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure how you'd implement this (other than thinking through the steps logically you'd need to do - i.e. get the base URL of the site and add "favicon.ico" onto the end of it to get the URL for the image to grab, though that's assuming that all sites have their favicon image in the domain root rather than an 'images' directory or something, which I guess you can't assume).

    Wouldn't there be copyright issues with grabbing a site's favicon if it's not your site though?

    With the converting images thing, I'm thinking you'd probably need to use something like ImageMagick or GD for processing the image. There's a load of info on GD in the PHP manual here. Actually, looking around the GD part of the PHP manual, it looks like you can use GD to create an image from a URL. It looks like you need the imagecreatefromgd2part function.
    "You don't need eyes to see - you need vision"
    ecanus.net: Poetry, smileys and stuff

  3. #3
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the advice. I will have to give that function a try. You are right about the favicon; it's almost always in the root directory. And when it is not the location has to be specified in the source code. I suppose I could program PHP to check for the favicon metatag and get the location if it's not found in the root.

    Also, I don't think that would be a copyright issue. Besides, I am just using them to display beside shared links those sites.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    imagecreatefromgd2part() only loads images in the GD2 file format. You'll need to find some code to read ICO files for GD, use ImageMagick, or use something else.

  5. #5
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why whould you need to resize the favicon? Favicon is usually a 16x16, so you surely don't want to make it any smaller, right..

    Why don't you just copy the favicon from the site, save it on your server and then just point to in in your img tag? Most (if not all) browsers will display the favicon in the browser just fine if you put a tag <img src="/favicon.ico">

    Just try it, create an html page and insert a line like this:
    <img src="http://developer.yahoo.com/favicon.ico"></img>

    That's it, no resizing is necessary
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  6. #6
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Not all favicons are favicon.ico - sites can specify a different file:

    <link rel="icon" href="filename" type="mime type" />

    eg

    <link rel="icon" href="favicon.png" type="image/png" />

  7. #7
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, so png can also be displayed in browser. What I am saying is that you don't really have to resize it, since it's already small and can already be displayed in browsers.
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  8. #8
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, I was just making a general point that you can't assume it's going to be called favicon.ico and in the root of the site.

    I've never used .ico files with the img tag, I assume it works then?

  9. #9
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have never used an ico file with an img tag either. Resizing isn't completely necessary, but some sites do use a 32x32 icon. I suppose if PHP is definitely not capable of resampling an ico file I will look into ImageMagick.

    As for the location of the favicon, for now I will just program the script to grab it from the root directory since almost every site has one there. I could later program my script to find the favicon metatag with regex, and if the tag is not found it will just try grabbing it from the root. I believe this is generally how browsers do it.

  10. #10
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I installed ImageMagick. This command seems to work good:

    convert favicon.ico -resize 16x16 favicon.png

  11. #11
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a way to do this without having to save the ico file to my server?

  12. #12
    SitePoint Enthusiast Fallen Angel's Avatar
    Join Date
    Sep 2006
    Location
    UK
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Without saving it, as far as I can see, the only way you could do it would be to hotlink to the file, but that's a rather big no-no.

    Apart from many site owners have scripts in place to block hotlinking, or instructions in their .htcaccess, or scripts that replace hotlinked images on unauthorised domains with a different image (usually one that says the image is being illegally hotlinked), and in many places it's illegal - classed as bandwidth theft. Last year I got into a rather messy row/conversations lawyers with a large social networking site about illegal hotlinking of images from my site which was costing me a fortune in bandwidth because of hotlinking. In short, I wouldn't recommend it at all.

    I can't think there's any way of displaying an image from another site without saving it. I'd still have some concerns about saving and displaying images from other sites without permission, which strictly speaking would be an infringement of copyright in most countries, but providing that it was only limited to favicons, I shouldn't imagine most site owners would object to that if you're linking to their site, though it might be an idea to put something in your T&Cs/site disclaimer saying that if a site owner objects to their graphic being used that you'll remove it.

    Back to the original question though, have you had a look on somewhere like hotscripts.com? (I've no idea what something like that might be filed under though! ). The only other thing I can think of is, is have you seen a site that's doing something similar to what you're trying to do? It's a long-shot, because I suspect that it will be server-side rather than client side, but it may be worth having a look at the page source to see if you can get any clues as to how they're doing it.
    "You don't need eyes to see - you need vision"
    ecanus.net: Poetry, smileys and stuff


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •