Use curl to open GD image
I want to add some watermarks on a image, but this image is on the other server, which created by GD lib, that means I'm opening a url like (http://www.abc.com/image.php), however, the remote server was disable the allow_url_open, and I have no way to enable this. So here is my code using 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);
}
开发者_如何学编程And I test with the code and get this
Warning: imagecreatefromstring() [function.imagecreatefromstring]: Data is not in a recognized format in D:\AppServ\www\index2.php on line 25
after that, I check the content I get, it got a html file, as follow, it seems it redirect to somewhere, instead of get an image, however, when I open the link in the browser, it did show the image correctly.
<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>
Please help~ thanks a lot~!
You need to open content-type for images/jpeg, something like this:
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');
That should work fine.
精彩评论