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

发布于 2024-12-06 17:05:51 字数 357 浏览 1 评论 0原文

我正在尝试使用 PHP 下载 图像,然后使用 GD 对其进行编辑。我找到了很多图片链接的解决方案,但这个是下载链接。

编辑:

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

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

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.

Edit:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

究竟谁懂我的在乎 2024-12-13 17:05:51
$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);
人生戏 2024-12-13 17:05:51

以下是直接将图像保存到文件的选项(而不是使用imagecreatefromstring):

<?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

Here is an option to directly save the image to a file (instead of using imagecreatefromstring):

<?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
情仇皆在手 2024-12-13 17:05:51

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

curl

查看精美的 php 手册。

fopen - depends on your php settings if url fopen is allowed.

or curl

see the fine php manual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文