Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download different sizes of an image from Twitter?

I'm using the Twitter Streaming API to get images included in tweets, and show them in my Android app. The media object which represents the image contains a media_url field that is a direct link to the image, and a JSON objects named sizes which contains the different sizes available for that image. The question is, how can I get such resized images? I want to download the thumbnail directly from Twitter, and since the sizes object contains a thumb field it should be possible, but how? I already tried to concat ?size=thumb to the media_url but it didn't work. Media object example:

{"id":897839774635053000,"id_str":"897839774635053056","indices":
[75,98],"media_url":"http://pbs.twimg.com/media/DHXEhKBW0AAKhf3.jpg",
"media_url_https":"https://pbs.twimg.com/media/DHXEhKBW0AAKhf3.jpg",
"url":"https://REMOVED/4NcW6720Zj","display_url":"pic.twitter.com/4NcW6720Zj",
"expanded_url":
"https://twitter.com/darioolly/status/897840123924164608/photo/1",
"type":"photo",

"sizes":{"thumb":{"w":150,"h":150,"resize":"crop"},
         "medium":{"w":720,"h":711,"resize":"fit"},
         "small":{"w":680,"h":672,"resize":"fit"},
         "large":{"w":720,"h":711,"resize":"fit"}}}
like image 553
DevOlly Avatar asked Dec 23 '22 15:12

DevOlly


1 Answers

You can retrieve the media in different sizes by appending a colon + the size key, as specified here

We support different sizes: thumb, small, medium and large. The media_url defaults to medium but you can retrieve the media in different sizes by appending a colon + the size key (for example: http://pbs.twimg.com/media/A7EiDWcCYAAZT1D.jpg:thumb ). Each available size comes with three attributes that describe it: w : the width (in pixels) of the media in this particular size; h : the height (in pixels) of the media in this particular size; resize : how we resized the media to this particular size (can be crop or fit )

like image 53
Alex Avatar answered Jan 30 '23 22:01

Alex