facebook graph api: offset_y offset_x

Viewed 158
1 Answers

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);

Related