42

Is there any way of building a profile image url with user id or screen name? I store user ids in database but i don't want to store profile image url.

edit:
I don't want to make a api call too. I want to put user_id inside a url like

<img src="https://twitter.com/users/profile_pic?user_id=123"> Is there a url to do this?

mcan
  • 1,914
  • 3
  • 32
  • 53

10 Answers10

93

With API 1.1 you can achieve this using these URLs:

Official twitter documentation Profile Images and Banners

Example

https://twitter.com/TwitterEng/profile_image?size=original

will redirect to

https://pbs.twimg.com/profile_images/875168599299637248/84CkAq6s.jpg
Ben Everard
  • 13,652
  • 14
  • 67
  • 96
Cristiana Chavez
  • 11,349
  • 5
  • 55
  • 54
  • 5
    One note, if you do this on a mobile browser, it won't redirect correctly. Part of twitter's attempt to redirect everyone to m.twitter.com – jfred Sep 13 '16 at 16:49
  • 3
    Caution! If you do this and the screen_name has changed as well then twitter will redirect your mage request to the home page. Get's messy if you are popping these straight into – Dave Sumter Oct 06 '16 at 09:27
  • 3
    is there a similar URL for the background image? it's not profile_background_image like you would think (which matches a key the API returns). – faceyspacey.com Nov 11 '16 at 09:44
  • couldn't fit it in a comment so i've posted an answer with essentially what i ended up going with – Zander Brown Jan 06 '17 at 23:31
  • 6
    Today it seems like https://twitter.com/TwitterEng/profile_image?size=original does not load and give a Twitter error page. – adriaan Jun 08 '20 at 12:17
  • 2
    It seems this endpoint response to User-Agent differently, `Googlebot/2.1 (+http://www.google.com/bot.html)` yields image, while others return error – Chun-Fu Chao Jul 02 '20 at 14:20
  • 8
    I tried accessing my profile picture `https://twitter.com/Rashid_Jorvee/profile_image?size=original` , but, it is showing error `Sorry, that page doesn’t exist!` – Rashid Sep 11 '20 at 11:04
  • Désolé, cette page n'existe pas ! – Dimitri Kopriwa Apr 11 '21 at 12:05
  • It's no longer working even with Googlebot user-agent now – Chun-Fu Chao Sep 07 '21 at 03:18
28

As of June 2020, both the accepted answer and avatars.io no longer work. Here are two alternatives:

unavatar.io

(formerly unavatar.now.sh)

Unavatar can get pictures from quite a few different places including Twitter. Replace [screen_name] in the URL below with the Twitter username you want.

<img src="https://unavatar.io/twitter/[screen_name]" />

For example:

<img src="https://unavatar.io/twitter/jack" width="100" height"100" />

If the demo above ever stops working, it's probably because unavatar.io is no longer available.

Unavatar is open source though, so if it does go down, you can deploy it yourself from the GitHub repo — it even has "Deploy to Vercel/Heroku" buttons. The code to fetch Twitter avatars specifically is here, so you could also use that as part of your own backend.


twivatar.glitch.me

⚠️ As of July 2021 this option no longer works, see the one above instead!

If you want an alternative, you can also use twivatar.glitch.me. Replace [screen_name] in the URL below with the Twitter username you want.

<img src="https://twivatar.glitch.me/[screen_name]" />

For example:

<img src="https://twivatar.glitch.me/jack" width="100" height"100" />

If the demo above ever stops working, it's probably because twivatar.glitch.me is no longer available.

By the way, I didn't build either of these services, they were both made by other people.

Ethan
  • 3,410
  • 1
  • 28
  • 49
  • Thought this is nice does not returns the actual image URL from Twitter, meaning if this "service" goes dark as avatars.io did you are back at square one. – d70rr3s Sep 07 '20 at 15:46
  • @d70rr3s you're right, but the person asking the question asked if there was a URL-only solution they could use, without having to store the actual image URL in a database or make an API call. So the only way to "return the actual image URL from Twitter" would be to do a 301 redirect, which would mean you're still relying on the service anyway. However, I've updated my answer to include multiple alternatives, including one you can one-click-deploy, in case either one ever disappears like avatars.io did. Hope that helps! – Ethan Sep 08 '20 at 07:58
  • 1
    Update: looks like unavatar.now.sh has been fixed and is back up! @VictorMota – Ethan Jul 04 '21 at 01:31
  • in 2023 unavatar.io doesnt seem to work anymore :/ – swyx Feb 17 '23 at 07:09
21

Introducing the easiest way to get a Twitter Profile Image without using the Twitter API:

Using http://avatars.io/

As @AlexB, @jfred says, it doesn't work at all on mobile devices.

And it's quite a hard way to get a redirected URL using common frameworks like PHP or JavaScript in your single page.

Simply call http://avatars.io/twitter/ruucm at your image tag, like

<img src="https://avatars.io/twitter/ruucm" alt="twt_profile" border="0" width="259"/>

I've tested it with Angular 2+ and it works without any problem.

Sebastian
  • 2,874
  • 25
  • 31
ruucm
  • 493
  • 7
  • 15
3

Based on the answer by @Cristiana214

The following PHP snippet can be used to make the https://twitter.com/[screen_name]/profile_image?size=normal trick work on mobile.

Due to twitters redirect to the mobile version of the site links such as https://twitter.com/[screen_name]/profile_image?size=normal get broken on mobile devices

So the script gets the redirect response (to the user avatar) extracts the address then redirects the page itself

if (!isset($_GET['id'])) $_GET['id'] = 'twitter';
$urlget = curl_init();
curl_setopt($urlget, CURLOPT_URL, 'https://twitter.com/' . $_GET['id'] . '/profile_image?size=normal'); 
curl_setopt($urlget, CURLOPT_HEADER, true);
curl_setopt($urlget, CURLOPT_RETURNTRANSFER, 1); 
$res = curl_exec($urlget);
preg_match_all("/location: (.*)/", $res, $found);
header('Location: ' . $found[1][0]);

So this could be accesses as twitteravatar.php?id=twitter which (at time of writing) reloads to https://pbs.twimg.com/profile_images/767879603977191425/29zfZY6I_normal.jpg

Not pretty but works.

Zander Brown
  • 637
  • 8
  • 16
3

As of February 20, 2020 it would appear this is impossible. Using the API seems like the only option at the moment. For more info see my question I've opened here: Twitter profile picture images now blocked on most domains

Anthony
  • 13,434
  • 14
  • 60
  • 80
3

I found such a solution with C#:

    public string Text_toTextFinder(string text, string Fromhere, string Here)
    {
        int start = text.IndexOf(Fromhere) + Fromhere.Length;
        int finish = text.IndexOf(Here, start);
        return text.Substring(start, finish - start);
    }
    string getPhotoURL(string UserName, string size ="x96")
    {
        using (WebClient client = new WebClient())
        {
            client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
            string htmlCode = client.DownloadString("https://twitter.com/" + UserName);
            return Text_toTextFinder(Text_toTextFinder(htmlCode, "<td class=\"avatar\">", "</td>"), "src=\"", "\"").Replace("normal",size);
        }
    }

For use:

MessageBox.Show(getPhotoURL("screen_name")); //size = 96x96
MessageBox.Show(getPhotoURL("screen_name","normal"));
MessageBox.Show(getPhotoURL("screen_name","200x200"));
MessageBox.Show(getPhotoURL("screen_name","400x400"));
2

You can get it using the users/show method of the Twitter API -- it does exactly what you described. You give it a the ID or the screen name, and it returns a bunch of data, including profile_image_url.

dnet
  • 1,411
  • 9
  • 19
  • 1
    i don't want to make a api call too. I want to put user_id inside a url like `` is it possible? – mcan Aug 22 '13 at 16:08
  • 3
    there's no way for that as far as I know -- Twitter has been quite obnoxious about their API in the last few years – dnet Aug 23 '13 at 07:33
-1

There is no way to do that. In fact Twitter doesn't provide a url to do that like facebook does ( https://graph.facebook.com//?fields=picture)

The issue is report but the status is: 'WontFix', take a look:

https://code.google.com/p/twitter-api/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Stars%20Type%20Bug%20Status%20Summary%20Opened%20Modified%20Component&groupby=&sort=&id=242#makechanges

poramo
  • 546
  • 5
  • 7
-2

Well I'm using a tricky way via PHP Dom Parser

include('simple_html_dom.php');
$html = file_get_html('http://twitter.com/mnckry');
$img = array();

foreach($html->find('img.size73') as $e)
    $img[] = $e->src;

foreach($html->find('.profile-header-inner') as $e)
    $img[] = str_replace("')", "", str_replace("url('", "", $e->{'data-background-image'}));



echo $img[0];//Avatar
echo "<br>";
echo end($img);//ProfileBG

This will give you something like this; https://pbs.twimg.com/profile_images/378800000487958092/e04a191de329fcf8d000ca03073ad594_bigger.png

to get 2 other size; for big version remove, "_bigger" for smaller version replace "_bigger" with "_normal"

siniradam
  • 2,727
  • 26
  • 37
-6

With version 1.1, use http://a0.twimg.com/profile_images/XXXXX/afpecvf41m8f0juql78p_normal.png where XXXXX is the User Id

MVCdragon
  • 93
  • 2
  • 8