使用 PHP 检查 url 是否返回图像?
我基本上检索用户的头像图像并将其用于我网站上的个人资料。如果用户没有 Gravatar 帐户或未将电子邮件分配给其 Gravatar 帐户,则会显示默认图像,而不是 Gravatar。
我的问题是,如果 gravatars 网站出现故障并且我无法检索用户的 gravatar 图像,那么在 php 中我是否可以检查 gravatar 是否返回图像,如果不显示一些文本或其他内容?
显然我可以做一个简单的 if 语句;但我不确定在 PHP 中检查 gravatar 是否返回图像。
如果我无法检查图像是否从 Gravatar 返回,我想检查链接是否在线(可访问)就可以了。
更新:
我忘了提及这是我从 gravatar 检索图像的方式:
function get_gravatar($email, $s = 50, $r = 'pg')
{
$defaultimage='mm';
$url='http://www.gravatar.com/avatar/';
$url.=md5(strtolower(trim($email)));
$url.="?s=$s&d=$defaultimage&r=$r";
return $url;
}
然后显示我这样做:
<a href="http://www.gravatar.com" target="_blank">
<img class="profileimgright profileimgframe" alt="Profile Image" src="<?php echo get_gravatar($_SESSION['email'], 180, 'pg') ?>" />
</a>
I basically retrieve a user's gravatar image and uses that for their profiles on my site. If a user doesn't have a gravatar account or email is not assigned to their gravatar account a default image is shown instead from gravatar.
My question is, if for example gravatars website went down and I could not retrieve the user's gravatar image, is there anyway in php I can check to see if gravatar is returning an image and if not display a some text or something?
Obviously I can do a simple if statement; but checking in PHP if gravatar is returning an image is what I am not sure on.
If I cannot check if a image is being returned from Gravatar, checking if the link is online (accessible) would be fine I guess.
UPDATE:
I forgot to mention this is how i retrieve the image from gravatar:
function get_gravatar($email, $s = 50, $r = 'pg')
{
$defaultimage='mm';
$url='http://www.gravatar.com/avatar/';
$url.=md5(strtolower(trim($email)));
$url.="?s=$s&d=$defaultimage&r=$r";
return $url;
}
Then to display I do:
<a href="http://www.gravatar.com" target="_blank">
<img class="profileimgright profileimgframe" alt="Profile Image" src="<?php echo get_gravatar($_SESSION['email'], 180, 'pg') ?>" />
</a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然这并不能回答你的确切问题,但我猜你实际上可能不需要担心是否从他们那里获得图像,因为你可以告诉 Gravatar 使用特定图像作为“默认” '如果他们没有的话。这样,您的代码始终会调用 Gravatar,它们将确定是显示用户的真实图像还是您指定的默认图像。
在 http://en.gravatar.com/site/implement/images/ 上,请参阅“默认图像”部分。
While this doesn't answer your precise question, I'm guessing you actually probably don't need to worry about whether or not you're getting an image from them, because you can tell Gravatar to use a specific image as the 'default' if they don't have one. That way, your code just always calls Gravatar and they will determine whether to show the user's real image or your specified default.
On http://en.gravatar.com/site/implement/images/, see the 'Default Image' section.
最简单的方法是检查外部 url 上是否存在文件
将通过 fopen
那么你可以这样称呼它
问候
the simplest way to go would be to check if a file exists on an external url
would be via fopen
then you could call it like this
Regards
您可以使用 getimagesize 检查 url 是否返回图像:
http://php.net/manual/en/function.getimagesize.php
如果不是,它将返回 false。
示例:
you can check whether a url is returning an image with getimagesize:
http://php.net/manual/en/function.getimagesize.php
It will return false if it's not.
Examples: