Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用curl打开GD图像_Php_Image_Curl - Fatal编程技术网

Php 使用curl打开GD图像

Php 使用curl打开GD图像,php,image,curl,Php,Image,Curl,我想在一个图像上添加一些水印,但是这个图像在另一个服务器上,它是由GD lib创建的,这意味着我正在打开一个url,如(http://www.abc.com/image.php)但是,远程服务器禁用了allow_url_open,我没有办法启用它。这是我使用curl的代码 function loadimg($url) { $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch,

我想在一个图像上添加一些水印,但是这个图像在另一个服务器上,它是由GD lib创建的,这意味着我正在打开一个url,如(http://www.abc.com/image.php)但是,远程服务器禁用了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()

之后,我检查我得到的内容,它得到一个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>

window.location=”http://ifastnet.com/notify2.html";


demil1.byetcluster.com于2011年2月7日星期一18:03:15 GMT生成(Lusca/Lusca_HEAD-r14756)

请帮忙~非常感谢~

您需要打开图像/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');

这应该行得通。

请给我们一个比“它不起作用”更好的问题指示。例如,一个错误代码就好了。此外,远程服务器返回错误消息而不是映像的原因有很多。你真的应该考虑树立一个榜样,让每个人都能重现这个问题。嗨,菲哈格,谢谢你的建议,请再读一遍。