从 Facebook 图片 url 获取图片名称

发布于 2024-12-17 14:17:48 字数 969 浏览 0 评论 0原文

当用户通过 Facebook 登录时,我想使用 Facebook 个人资料图像作为我的网站的个人资料图像。我正在使用omniauth gem 进行Facebook 登录。登录后,omniauth 以哈希形式给出 Facebook 响应,如下所示:

{
  "user_info" => {
                   "last_name" => "..........", 
                   "image" => "http://graph.facebook.com/100002356868026/picture?type=square",
                   "first_name" => "........", 
                   "email" => "[email protected]"
                  },
  "uid" => "..........."
}

这里,我可以使用 request.omniauth["user_info"]["image"] 访问图像 url,并可以保存它使用

require 'open-uri'
image_url = request.omniauth["user_info"]["image"]
image_data = open(image_url).read
File.open("image.png", "wb") { |file| file << image_data }

我想从 url 获取图像名称。但是,Facebook url 不包含名称(它是重定向到 url 包含图像名称的另一个页面的链接)。如何从 url 中解析图片名称?

I want to use Facebook profile image as the profile image for my site when the user logins via Facebook. I am using omniauth gem for Facebook login. After login, omniauth gives Facebook response in the form of a Hash which looks like this:

{
  "user_info" => {
                   "last_name" => "..........", 
                   "image" => "http://graph.facebook.com/100002356868026/picture?type=square",
                   "first_name" => "........", 
                   "email" => "[email protected]"
                  },
  "uid" => "..........."
}

Here, I can access the image url using request.omniauth["user_info"]["image"] and can save it using

require 'open-uri'
image_url = request.omniauth["user_info"]["image"]
image_data = open(image_url).read
File.open("image.png", "wb") { |file| file << image_data }

I want to get the image name from the url. But, the Facebook url doesnt contain the name (It is a link which redirects to another page whose url contains the image name). How can I parse the image name from the url?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初雪 2024-12-24 14:17:48

我认为你无法从 url 中获取最终的图像名称。该图像存储在内容分发网络 (CDN) 中,该网络不是通过 facebook id 而是通过其他索引来分发图像。

为什么你想要最终的图像名称?您可以通过引用的图像参数来获取图像,它将直接返回用户的图像数据(如果可用,否则返回虚拟图像)。

如果您想将图像存储在服务器上,您很可能会选择自己的命名方案。

重要提示:请仔细查看 Facebook 开发者条款!在您的服务器上存储用户图像可能会违反这些条款。

我建议根本不要在服务器上处理图像,而只包含图像参数,因为它位于 html 图像标记中(如果可能)。

I don't think you can get the final image name from the url. The image is stored at a content delivery network (CDN), which delivers the image not by facebook id, but by some other index.

Why would you want the final image name anyway? You can obtain the image by following the image parameter you quoted, it will return the user's image data (if available, else a dummy image is returned) directly.

If you want to store the images on your server, you most likely would choose your own naming scheme.

Important: Please check the facebook developer terms carefully! Storing user images on your server may violate these terms.

I would recommend not to process the image on your server at all but to just include the image parameter as it is in your html image tag, if possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文