2011-02-07 65 views
1

我想在图像上添加一些水印,但是此图像位于由GD lib创建的其他服务器上,这意味着我打开了一个url(http:// www。 abc.com/image.php),但是,远程服务器禁用了allow_url_open,我无法启用它。因此,这里是使用curl使用卷曲打开GD图像

function loadimg($url) { 
    $ch = curl_init(); 

    curl_setopt ($ch, CURLOPT_URL, $url); 
    curl_setopt ($ch, CURLOPT_BINARYTRANSFER, true); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt ($ch, CURLOPT_HEADER, false); 
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0); 

    $rawdata = curl_exec($ch); 
    $image = imagecreatefromstring($rawdata); 

    curl_close($ch); 

    return imagejpeg($image); 
} 

我的代码,我测试的代码,并得到这个

警告:imagecreatefromstring()[function.imagecreatefromstring]:数据是不是在d可识别的格式:\的appserv \ www \ index2.php on line 25

之后,我检查了我得到的内容,得到一个html文件,如下所示,它似乎重定向到某处,而不是获取图像,但是当我打开在浏览器中的链接,它确实显示正确的图像。

<head> 
<meta http-equiv="refresh" content="0;url=http://ifastnet.com/notify2.html" /> 
</head> 
<html> 
<body> 
<script LANGUAGE="JavaScript"> 
window.location="http://ifastnet.com/notify2.html"; 
</script> 
<!-- 399 310 772 188 121 747 908 375 658 989 471 891 842 282 539 788 863 399 310 772 188 121 747 908 375 658 989 471 891 842 282 539 788 863 399 310 772 188 121 747 908 375 658 989 471 891 842 282 539 788 863 526 640 917 51 415 140 573 716 965 688 395 829 76 810 801 733 244 95 205 283 488 189 705 173 743 574 947 608 694 973 886 298 223 449 99 309 936 432 209 623 454 399 310 772 188 121 747 908 375 658 989 471 891 842 282 539 788 863 399 310 772 188 121 747 908 375 658 989 471 891 842 282 539 788 863 399 310 772 188 121 747 908 375 658 989 471 891 842 282 539 788 863 526 640 917 51 415 140 573 716 965 688 395 829 76 810 801 733 244 95 205 283 488 189 705 173 743 574 947 608 694 973 886 298 223 449 99 309 936 432 209 623 454 399 310 772 188 121 747 908 375 658 989 471 891 842 282 539 788 863 399 310 772 188 121 747 908 375 658 989 471 891 842 282 539 788 863 399 310 772 188 121 747 908 375 658 989 471 891 842 282 539 788 863 526 640 917 51 415 140 573 716 965 688 395 829 76 810 801 733 244 95 205 283 488 189 705 173 743 574 947 608 694 973 886 298 223 449 99 309 936 432 209 623 454 399 310 772 188 121 747 908 375 658 989 471 891 842 282 539 788 863 399 310 772 188 121 747 908 375 658 989 471 891 842 282 539 788 863 399 310 772 188 121 747 908 375 658 989 471 891 842 282 539 788 863 526 640 917 51 415 140 573 716 965 688 395 829 76 810 801 733 244 95 205 283 488 189 705 173 743 574 947 608 694 973 886 298 223 449 99 309 936 432 209 623 454 399 310 772 188 121 747 908 375 658 989 471 891 842 282 539 788 863 399 310 772 188 121 747 908 375 658 989 471 891 842 282 539 788 863 399 310 772 188 121 747 908 375 658 989 471 891 842 282 539 788 863 526 640 917 51 415 140 573 716 965 688 395 829 76 810 801 733 244 95 205 283 488 189 705 173 743 574 947 608 694 973 886 298 223 449 99 309 936 432 209 623 454 --> 
</body> 
</html> 

<BR clear="all"> 
<HR noshade size="1px"> 
<ADDRESS> 
Generated Mon, 07 Feb 2011 18:03:15 GMT by demil1.byetcluster.com (Lusca/LUSCA_HEAD-r14756) 
</ADDRESS> 
</BODY></HTML> 

请大家帮忙〜多谢〜!

+0

请给我们一个比“没有用”更好的指示问题。例如,一个错误代码会很好。另外,远程服务器返回错误消息而不是图像的原因有很多种。你真的应该考虑设置一个例子,这样每个人都可以重现这个问题。 – phihag 2011-02-07 11:34:22

回答

-1

您需要打开内容类型为图像/ JPEG,这样的事情:

function loadimg($url) { 
    $ch = curl_init(); 

    curl_setopt ($ch, CURLOPT_URL, $url);  
    curl_setopt ($ch, CURLOPT_BINARYTRANSFER, true); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt ($ch, CURLOPT_HEADER, false); 
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0); 

    $rawdata = curl_exec($ch); 
    $image = imagecreatefromstring($rawdata); 

    curl_close($ch); 

    return imagejpeg($image); 
} 

header('Content-Type: image/jpeg'); 

loadimg('IMG_URL'); 

这应该很好地工作。