我想在图像上添加一些水印,但是这个图像位于由GD lib创建的另一台服务器上,这意味着我正在打开一个网址(http://www.abc.com/image。但是,远程服务器禁用了allow_url_open,我无法启用它。所以这是我的代码使用curl
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]:数据在第25行的D:\ AppServ \ www \ index2.php中不是可识别的格式
之后,我检查了我得到的内容,它得到了一个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 :(得分:-1)
您需要为images / 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');
这应该可以正常工作。