简体   繁体   English

有没有一种方法可以检测从重定向的img src加载的图像类型?

[英]is there a way to detect what type of image is being loaded from an img src that is redirected?

I'm using the this facebook service to return profile pictures. 我正在使用这个Facebook服务来返回个人资料图片。

<img src="http://graph.facebook.com/username/picture">

If the fb user did not set a profile picture, the service redirects to this facebook anonymous person image: http://static.ak.fbcdn.net/rsrc.php/v2/yo/r/UlIqmHJn-SK.gif . 如果fb用户未设置个人资料图片,则该服务将重定向到该Facebook匿名人图片: http://static.ak.fbcdn.net/rsrc.php/v2/yo/r/UlIqmHJn-SK.gif : http://static.ak.fbcdn.net/rsrc.php/v2/yo/r/UlIqmHJn-SK.gif I would like to avoid presenting those. 我想避免介绍这些内容。

Is there a way, using javascript, to detect if the image returned is http://static.ak.fbcdn.net/rsrc.php/v2/yo/r/UlIqmHJn-SK.gif . 有没有一种方法可以使用javascript检测返回的图像是否为http://static.ak.fbcdn.net/rsrc.php/v2/yo/r/UlIqmHJn-SK.gif That address is from a CDN, so I wonder if we can just check the file size is <= 390B and mime-type is image/gif? 该地址来自CDN,所以我想知道是否可以仅检查文件大小<= 390B并且mime-type是image / gif?

EDIT: Unfortunately the solutions involving only modern browsers (HTML5 and canvas) might not work for me since the requirement is that we still need to support back to ie7. 编辑:不幸的是,仅涉及现代浏览器(HTML5和canvas)的解决方案可能不适用于我,因为要求是我们仍然需要支持回到ie7。

Yes there is, you can simply make your request with AJAX and parse the MIME-type yourself. 是的,您可以使用AJAX发出请求,然后自己解析MIME类型。 Indeed, Facebook includes this header: 实际上,Facebook包含以下标头:

Access-Control-Allow-Origin: *

This authorizes CORS (Cross Origin Resource Sharing), which is now supported by most browsers. 这授权了CORS (跨源资源共享),现在大多数浏览器都支持它。

If you absolutely must support old browsers, have a look at the Open Graph documentation: 如果您绝对必须支持旧的浏览器,请查看Open Graph文档:

https://developers.facebook.com/docs/reference/api/using-pictures/ https://developers.facebook.com/docs/reference/api/using-pictures/

Instead of directly fetching the picture, you can just fetch its metadata with JSONP. 除了直接获取图片,您还可以使用JSONP获取其元数据。 This should work in IE7: 这应该在IE7中有效:

function checkPictureUrl(userName, cb) {
  $.ajax({
    url: 'http://graph.facebook.com/' + userName + '/picture?redirect=false',
    dataType: 'jsonp',
    success: function (json) {
      // user only has a default profile picture
      if(json.data.is_silhouette)
        return cb(null);
      else
        return cb(null, json.data.url);
    },
    error: function (err) {
      cb(err);
    }
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM