Maca
Maca

Reputation: 1689

Retrieving profile image from Google+ API

I am trying to pull JSON data from Google+ API. When I include the image attribute size of the image is set to 50px. How do I change the image size? I don't see it on the docs.

GET https://www.googleapis.com/plus/v1/people/100300281975626912157?fields=image&key={YOUR_API_KEY}

Response

{
 "image": {
  "url": "https://lh3.googleusercontent.com/-U353P5vNuRE/AAAAAAAAAAI/AAAAAAAABKM/a7U7bq251x0/photo.jpg?sz=50"
 }
}

Upvotes: 14

Views: 11538

Answers (5)

Philip Sopher
Philip Sopher

Reputation: 783

Here's what worked for me:

let profilePicture

if (userObject.picture.includes('s96-c')) {

  profilePicture = userObject.picture.replace('s96-c', 's300-c')

} else if (userObject.picture.includes('?sz=')) {

  profilePicture = userObject.picture.substring(0, userObject.picture.indexOf('?')) + '?sz=300'

}

Upvotes: 3

umesh giri
umesh giri

Reputation: 198

There are two kinds of google images:

  1. User-uploaded profile Image (ending with s96-c)
  1. Google generated image (ending with photo.jpg)

replace {anyhighernumber} with your desired resolution eg:1500

Upvotes: 11

Rasmus Søborg
Rasmus Søborg

Reputation: 3695

At the end of your URL string there's a attribute called sz=50

I just tried to change the attribute, and as result the image's size is changed aswell.

Try these two urls:

https://lh3.googleusercontent.com/-U353P5vNuRE/AAAAAAAAAAI/AAAAAAAABKM/a7U7bq251x0/photo.jpg?sz=50

and

https://lh3.googleusercontent.com/-U353P5vNuRE/AAAAAAAAAAI/AAAAAAAABKM/a7U7bq251x0/photo.jpg?sz=150

See the difference?

Upvotes: 19

G. I. Joe
G. I. Joe

Reputation: 1653

Just have to change the sz suffix indicating the wnated size, here is the suffix:

https://lh3.googleusercontent.com/-U353P5vNuRE/AAAAAAAAAAI/AAAAAAAABKM/a7U7bq251x0/photo.jpg?sz=150

If you cut the param, you would get the default photo sized

https://lh3.googleusercontent.com/-U353P5vNuRE/AAAAAAAAAAI/AAAAAAAABKM/a7U7bq251x0/photo.jpg

And an automatic change in the string: https://jsfiddle.net/upyL4onm/3/

var newSize="300"
var str = "https://lh3.googleusercontent.com/-U353P5vNuRE/AAAAAAAAAAI/AAAAAAAABKM/a7U7bq251x0/photo.jpg?sz=50";
var res = str.split("?sz=50")[0]+"?sz="+newSize;

And the console.log(res) will output:

https://lh3.googleusercontent.com/-U353P5vNuRE/AAAAAAAAAAI/AAAAAAAABKM/a7U7bq251x0/photo.jpg?sz=300

Upvotes: 5

Just Lucky Really
Just Lucky Really

Reputation: 1401

Because there is no way in the api to do this, you can just use sustr to remove the ?str= and add your own:

$imageUrl = substr($user['image']['url'],0,strpos($user['image']['url']."?sz=","?sz=")) . '?sz=100';

Or for javascript:

iamgeUrl=user[image][url].substr(0,user[image][url].indexOf('?str=')) + '?sz=100';

Upvotes: 2

Related Questions