我在我的drupal网站上有一个Facebook风格的聊天,但是脚本缺乏用户图片集成。我设法获得了用户图片,但是如果用户没有设置图片,并且使用的是默认图片,我会在日志中得到一个损坏的图片和一个404 not found。我曾尝试为没有图片集的用户获取gravatar,但无法使其工作。
/**
* This function returns the URL of the avatar of the specified user ID.
*
* @param userid the user ID of the user
* @param image if the image includes more than just a user ID, this param is passed
* in from the avatar row in the buddylist and get user details functions.
* @return the link of the user ID's profile
*/
function get_avatar($image, $user_id, $account) {
return "http://mypage.com/files/pictures/picture-" . ($user_id) . ".jpg";
if (empty($account->picture)) {
return "http://www.gravatar.com/avfatar/" . md5($image) . "?d=identicon";
}
}发布于 2015-04-30 04:31:50
你的问题是,你立即返回你的图像,无论它是否在那里:
function get_avatar($image, $user_id, $account)
{
//THE NEXT LINE RETURNS FROM THE FUNCTION REGARDLESS
return "http://mypage.com/files/pictures/picture-" . ($user_id) . ".jpg";
//THIS CODE NEVER RUNS
if (empty($account->picture)) {
return "http://www.gravatar.com/avfatar/" . md5($image) . "?d=identicon";
}
}您需要做的是:
function get_avatar($image, $user_id, $account)
{
$imgurl ="http://mypage.com/files/pictures/picture-" . ($user_id) . ".jpg";
if (!is_imgurl_good($imgurl)) {
$imgurl = "http://www.gravatar.com/avfatar/" . md5($image) . "?d=identicon";
}
return $imgurl;
}在上面的代码中,您必须编写一个函数is_imgurl_good,该函数返回一个指示图像是否良好的boolean。如果是,则为true,否则为false。有许多方法可以编写该函数,这超出了本问题的范围,但您可以使用以下方法来测试该方法:
function is_imgurl_good($imgurl) {
return false;//Check that if this returns false the previous function works
//return true; //Comment out the first line and uncomment this one to show the reverse case.
//put your actual code here to decide whether $imgurl is a valid image.
}https://stackoverflow.com/questions/29951493
复制相似问题