Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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中从301重定向下载链接获取图像?_Php_Image_Curl_Download_Gd - Fatal编程技术网

如何在PHP中从301重定向下载链接获取图像?

如何在PHP中从301重定向下载链接获取图像?,php,image,curl,download,gd,Php,Image,Curl,Download,Gd,我正在尝试用PHP下载图像,用GD编辑它。我找到了许多图像链接的解决方案,但这是一个下载链接 编辑: 这是我当前的代码。它显示“301永久移动”。是否需要设置卷发选项?-如果允许url fopen,则取决于您的php设置 或 请参阅fine php手册。这里有一个选项可以直接将图像保存到文件中(而不是使用imagecreatefromstring): $curl = curl_init("http://minecraft.net/skin/Notch.png"); // Moved? Fear

我正在尝试用PHP下载图像,用GD编辑它。我找到了许多图像链接的解决方案,但这是一个下载链接

编辑:

这是我当前的代码。它显示“301永久移动”。是否需要设置卷发选项?

-如果允许url fopen,则取决于您的php设置


请参阅fine php手册。

这里有一个选项可以直接将图像保存到文件中(而不是使用
imagecreatefromstring
):

$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);

下载链接和图像链接之间没有区别。
$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
$fileName = '/some/local/path/image.jpg';
$fileUrl  = 'http://remote.server/download/link';
$ch = curl_init($fileUrl); // set the url to open and download
$fp = fopen($fileName, 'wb'); // open the local file pointer to save downloaded image
curl_setopt($ch, CURLOPT_FILE, $fp); // tell curl to save to the file pointer
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // tell curl to follow 30x redirects
curl_exec($ch); // fetch the image and save it with curl
curl_close($ch); // close curl
fclose($fp); // close the local file pointer