Facebook 图 api:offset_y offset_x

facebook graph api: offset_y offset_x

您好,我想了解 offset_y 在 facebook 图表中的含义 API https://developers.facebook.com/docs/graph-api/reference/cover-photo/.

我试过理解这个相关的 post,但不能。 how to compute Facebook graph api cover offset_y to pixel?

比如这次活动。 https://www.facebook.com/events/1119146318216486/。调用图 api

时生成“offset_y”: 20'

但是,实际偏移量是-4px:

如有任何帮助,我们将不胜感激。

offset_xoffset_y 的值是原始图像大小的百分比,而不是像素值。

百分比范围从 0 到 100,并且将是 space.

调整后图像溢出偏移值的百分比

例如,我有一张 720x480 像素的图片。封面照片 space 是 851x315 像素,因此当调整照片大小以适应 space 时,它是 851x567.33 像素。如果我在为封面定位图像时将该图像拖到一半,返回的 offset_y 将是 50。这意味着 50% "leftover" space 不适合封面照片插槽。

"leftover" 垂直 (y) space 将为 567.33 - 315 = 252.33 像素。 space 的 50% 是 126.167。在这种情况下,我的 top 偏移量将为 -126.167 像素。

所以 offset_xoffset_y 是将调整大小的图像定位到 Facebook 上的照片 space 所需的像素移动百分比。

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