如何从PHP中的301重定向下载链接获取图像? [英] How can get an image from a 301 redirect download link in PHP?

查看:92
本文介绍了如何从PHP中的301重定向下载链接获取图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用PHP下载图像,以使用GD对其进行编辑。我找到了许多图像链接解决方案,但这是一个下载链接。

I'm trying to download this image with PHP to edit it with GD. I found many solutions for image links, but this one is a download link.

编辑:

$curl = curl_init("http://minecraft.net/skin/Notch.png");
$bin = curl_exec($curl);
curl_close($curl);
$img = @imagecreatefromstring($bin);

这是我当前的代码。它显示 301永久移动。我必须设置CURLOPT吗?

This is my current code. It displays "301 Moved Permanently". Are there CURLOPTs I have to set?

推荐答案

$curl = curl_init("http://minecraft.net/skin/Notch.png");
// Moved? Fear not, we'll chase it!
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
// Because you want the result as a string
curl_setopt($curl,  CURLOPT_RETURNTRANSFER, true); 
$bin = curl_exec($curl);
curl_close($curl);
$img = @imagecreatefromstring($bin);

这篇关于如何从PHP中的301重定向下载链接获取图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆