Php 如何返回绝对url

Php 如何返回绝对url,php,Php,我一直在尝试在php生成的图像中插入一个图像(使用imagecreatefromX(),其中x=gif、jpeg或png),使用GET周长作为源url。我尝试让它检查MIME类型,方法是: $pic = $_GET['pic']; switch(returnMIMEType($pic)) { case "image/gif": $profileImage = @imagecreatefromgif($pic) or die('Unable to load gif im

我一直在尝试在php生成的图像中插入一个图像(使用imagecreatefromX(),其中x=gif、jpeg或png),使用GET周长作为源url。我尝试让它检查MIME类型,方法是:

$pic = $_GET['pic'];

switch(returnMIMEType($pic)) {

    case "image/gif":
        $profileImage = @imagecreatefromgif($pic) or die('Unable to load gif image.');
        break;

    case "image/jpg":
        $profileImage = @imagecreatefromjpeg($pic) or die('Unable to load jpeg image.');
        break;

    case "image/png":
        $profileImage = @imagecreatefrompng($pic) or die('Unable to load png image.');
        break;

    default:
        $pic = 'http://www.example.com/image.gif';
        $profileImage = @imagecreatefromgif($pic) or die('Unable to load gif image.');
        break;   
}

$profileImageWidth = imagesx($profileImage);
$profileImageHeight = imagesy($profileImage);

imagecopyresized($image, $profileImage, 0, 0, 0, 0, 25, 25, $profileImageWidth, $profileImageHeight);
returnMIMEType()只是我用来返回MIME类型的一个简单函数

当url以这种格式给出时:http://graph.facebook.com/123456789/picture“,它应该会返回Facebook上用户个人资料图片的jpg图像,但我假设,这就是我遇到的问题,PHP正在查看的是确切的URL,而不是URL”http://graph.facebook.com/123456789/picture“也重定向。我怎样才能让它理解等待重定向

谢谢你对此事的考虑

下面是它给出的错误:

Warning: mime_content_type() [function.mime-content-type]: File or path not found
'http://graph.facebook.com/123456789/picture'

重定向不应该是一个问题。很可能,ini设置设置为false,因此不允许使用文件函数打开URL,并且由于您使用的是
@
,错误被抑制。请尝试再次检查您是否启用了
允许url\u fopen
,如果不起作用,请尝试删除
@
,以获得更有意义的错误消息

我尝试使用url和'imagecreatefromgif(),它对我来说效果很好

编辑:fileinfo函数不喜欢URL。请尝试以下操作以更改mime类型:

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_buffer($finfo, file_get_contents($url));

当你尝试这个的时候会发生什么?第一步,删除
@
,以获取有意义的错误消息。重定向是一种可能性,但它可能有其他原因导致文章出现错误。但是,删除“@”并没有改变输出。我尝试使用ini_set(),但没有效果。我没有使用fopen,只是imagecopyresized();。我以前应该使用fopen吗?抱歉,在您编辑错误消息之前写下了
allow\u url\u fopen
适用于大多数打开文件的函数,但这似乎不是问题所在。试试我发布的更新代码。谢谢,效果很好!虽然webbiedave提到节省带宽,但首先解决问题的是您。感谢你们俩。:)