问题
Hello I am trying to understand what offset_y means in facebooks graph API https://developers.facebook.com/docs/graph-api/reference/cover-photo/.
I have tried understanding this related post, but cannot. how to compute Facebook graph api cover offset_y to pixel?
For example, this event. https://www.facebook.com/events/1119146318216486/. produces an '"offset_y": 20' when calling the graph api
But, the actual offset is -4px:
Any help would be greatly appreciated thanks.
回答1:
The values for offset_x
and offset_y
are percentages of the size of the original image, not pixel values.
The percentages will range from 0 to 100, and will be a percentage of the overflow offset value of the resized image for the space.
For example, I have an image that is 720x480 px. The cover photo space is 851x315 px, so when the photo is resized to fit that space, it is 851x567.33 px. If I drag that image halfway down when I am positioning it for my cover, the offset_y
that is returned will be 50
. This translates to 50% of the "leftover" space that doesn't fit into the cover photo slot.
The "leftover" vertical (y) space will be 567.33 - 315 = 252.33 px. 50% of that space is 126.167. My top
offset, in this case, will be -126.167 px.
So offset_x
and offset_y
are percentages of the pixel movement required to position the resized image into the photo space on Facebook.
// These values retreived ahead of time (Graph API, etc.)
var offset_x = 0;
var offset_y = 50;
var fbCoverPhotoWidth = 851; // Known value, in pixels
var fbCoverPhotoHeight = 315; // Known value, in pixels
// Create an image from the cover photo URL returned by the Graph API
var img = new Image();
img.src = "https://scontent.xx.fbcdn.net/v/t1.0-0/p480x480/your-photo-url-here.jpg";
// Calculate the scaling ratio
var ratio = Math.max(fbCoverPhotoWidth / img.width, fbCoverPhotoHeight / img.height);
// Convert the offset percentages to pixel values
var offset_x_pixels = Math.ceil(((img.width * ratio) - fbCoverPhotoWidth) * (offset_x / 100));
var offset_y_pixels = Math.ceil(((img.height * ratio) - fbCoverPhotoHeight) * (offset_y / 100));
console.log(offset_x_pixels);
console.log(offset_y_pixels);
来源:https://stackoverflow.com/questions/47238129/facebook-graph-api-offset-y-offset-x