用PHP获取远程图像并用GD显示
我的网站中有一个代码可以显示远程 Gravatar 肖像或上传的图像。上传没问题,但是看不到头像。
无法使用 file_get_contents,因为我的主机不允许这样做。
这是文件的开始检查
if(file_exists($arUser['imagem'][0])){
$imgPath = $arUser['imagem'][0]; //Usar a imagem enviada
}elseif(!strlen($arUser['imagem'][0]) && checkRemoteFile('http://www.gravatar.com/avatar/'.md5($arUser['email'][0]).'fs=150')){
$imgPath = 'http://www.gravatar.com/avatar/'.md5($arUser['email'][0]).'fs=150';
}else
$imgPath = '../img/social_noavatar_150.jpg'; //Temporario
所以这不起作用:
$imgData = getimagesize($imgPath);
$src = imagecreatefromwhatever($imgPath);
我知道我应该替换:
$imgPath = 'http://www.gravatar.com/avatar/'.md5($arUser['email'] [0]).'fs=150';
类似于:
$imgPath = GetFileData('http://www.gravatar.com/avatar/'.md5($arUser['email'][0]).'fs=150');
或
*$imgPath = file_get_contents(' http://www.gravatar.com/avatar/'.md5($arUser['email'][0]).'fs=150');*
和我都有错误无法创建图像:
我已经寻找答案,但其他答案不适合我。
抱歉我的英语不好。 :(
笔记:
function imagecreatefromwhatever($image){
$info = pathinfo($image);
$extension = strtolower($info['extension']);
switch($extension) {
case "jpg":
return imagecreatefromjpeg($image);
break;
case "jpeg":
return imagecreatefromjpeg($image);
break;
case "png":
return imagecreatefrompng($image);
break;
case "gif":
return imagecreatefromgif($image);
break;
default:
return imagecreatefromjpeg($image);
}
}
I have a code in my website to show remote Gravatar portraits or uploaded images. Uploaded is ok, but i can't get the gravatar images.
Cant use file_get_contents because it´s not allowed on my host.
Heres the start check for the file
if(file_exists($arUser['imagem'][0])){
$imgPath = $arUser['imagem'][0]; //Usar a imagem enviada
}elseif(!strlen($arUser['imagem'][0]) && checkRemoteFile('http://www.gravatar.com/avatar/'.md5($arUser['email'][0]).'fs=150')){
$imgPath = 'http://www.gravatar.com/avatar/'.md5($arUser['email'][0]).'fs=150';
}else
$imgPath = '../img/social_noavatar_150.jpg'; //Temporario
So this doesn´t work:
$imgData = getimagesize($imgPath);
$src = imagecreatefromwhatever($imgPath);
I know I should replace:
$imgPath = 'http://www.gravatar.com/avatar/'.md5($arUser['email'][0]).'fs=150';
with something like:
$imgPath = GetFileData('http://www.gravatar.com/avatar/'.md5($arUser['email'][0]).'fs=150');
or
*$imgPath = file_get_contents('http://www.gravatar.com/avatar/'.md5($arUser['email'][0]).'fs=150');*
Got error with both and I can´t create the image:
I´ve searched for the answer but the others didn't fited to me.
Sorry for my bad english. :(
Note:
function imagecreatefromwhatever($image){
$info = pathinfo($image);
$extension = strtolower($info['extension']);
switch($extension) {
case "jpg":
return imagecreatefromjpeg($image);
break;
case "jpeg":
return imagecreatefromjpeg($image);
break;
case "png":
return imagecreatefrompng($image);
break;
case "gif":
return imagecreatefromgif($image);
break;
default:
return imagecreatefromjpeg($image);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,如果 file_get_content 在您的主机上不可用,那么您可能会不走运。如果这是您主机上的一项安全功能,那么您将找不到任何允许您从另一台服务器获取数据的功能。
您可能只想绕过该功能并将图像 src url 设置为头像 URL。像这样的东西:(
请仔细检查url,我在fs之前添加了一个&,因为它更有意义,但我不知道gravatar api url是什么样的)
这样,客户端浏览器将发出请求并不是你的服务器。
Well, if file_get_content is not available on your host, you might be out of luck. If this is a security feature on your host then you won't find a single function that allows you to get data from another server.
You might want to simply bypass the function and set your image src url to the gravatar one. Something like :
(Please double check the url, I added a & before the fs as it makes more sense, but I don't know how gravatar api url looks like)
This way it is the client browser that will make the request and not your server.
我发现最简单的方法是转移位置:
The easiest way I have found is to transfer the location:
如果服务器上的 PHP 安全性阻止通过 file_get_contents() 远程抓取文件,那么下一个最佳选择是 CURL 调用来获取文件内容。
If your PHP security on your server prevents remote grabbing of files via file_get_contents(), then your next best option is a CURL call to get the file contents fed in perhaps.
如果您的主机不允许 file_get_contents,则其他方法可能不起作用。根据我的经验,主机将阻止来自脚本的任何外部套接字连接。所以先跟楼主确认一下。
假设这是问题所在,您仍然可以将带有 src 属性的标签回显到您尝试在脚本中获取的同一位置。
如果您不想使用该标签,也可以使用相同的技巧将 url 放入样式中。
If your host doesn't allow file_get_contents it's likely other methods will not work. From my experience the hosts will prevent any external socket connections from script. So check with the host first.
Assuming that is the issue, you could still echo out an tag with the src attribute to the same location you're trying to get in script.
You could also use the same trick to put the url into a style if you don't want to use the tag.