duanji1899 2012-11-24 07:20
浏览 91
已采纳

如何使用cURL将图像资源从URL加载到PHP脚本?

I'm using the Google Charts service to generate some QR codes that I afterward need to manipulate (e.g. rotate, scale) in a PHP script and merge with other images to generate one final image.

How do I correctly load such a resource (from a URL) into a PHP script, in a way that will allow me to manipulate it?

An example URL is: https://chart.googleapis.com/chart?cht=qr&chs=500x500&chl=xghsdfgsdfg&choe=UTF-8&chld=L|0

I currently have the following code to retrieve the image using cURL:

function getImage($url){
        $ch = curl_init ($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
        $resource = curl_exec($ch);
        curl_close ($ch);

        return $resource;
}

But when I use it like this:

$image = imagecreatefrompng(getImage("https://chart.googleapis.com/chart?cht=qr&chs=500x500&chl=xghsdfgsdfg&choe=UTF-8&chld=L|0"));

The following error is returned:

Warning: imagecreatefrompng(‰PNG  ) [function.imagecreatefrompng]: failed to open stream: No such file or directory in /home/picselbc/public_html/projects/cakemyface/preview.php on line 383
https://chart.googleapis.com/chart?cht=qr&chs=500x500&chl=xghsdfgsdfg&choe=UTF-8&chld=L|0
  • 写回答

2条回答 默认 最新

  • dqellle310167 2012-11-24 07:23
    关注

    What you need instead of imagecreatefrompng() is imagecreatefromstring(), because the former expects a filename instead of the file contents itself.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系