Facebook 图形 API:offset_y 和 offset_x

3

你提到的那个问题已经五年了。现在文档似乎更明确地说明了那个值的实际含义,相比之前。如果有什么的话,我会以此作为起点来理解它。但是从我迄今为止在所有SO问题中遇到的情况来看,整个事情似乎或多或少都是一个谜团,没有人能够成功地理解它或将其应用于实际用例[顺便问一下,你的确切用例是什么?],所以祝你好运;-) - CBroe
1个回答

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

百分比范围从0到100,并且是调整大小后的图像溢出偏移值的百分比。

例如,我有一张720x480像素的图片。封面照片空间为851x315像素,因此当将照片调整大小以适应该空间时,其大小为851x567.33像素。如果我将该图像拖动到位置时,处于封面的中心位置,则返回的offset_y将为50。这相当于剩余空间的50%,该空间无法适合封面照片插槽。

“剩余”的垂直(y)空间将为567.33-315 = 252.33像素。该空间的50%为126.167。在这种情况下,我的top偏移量将为-126.167像素。

因此,offset_xoffset_y是定位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);


很高兴能帮忙!祝你在这个项目中好运。 - Scott Kearney

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接