Как получить изображение из ссылки на перенаправление 301 в PHP? - PullRequest
4 голосов
/ 26 сентября 2011

Я пытаюсь загрузить это изображение с помощью PHP, чтобы отредактировать его с помощью GD. Я нашел много решений для ссылок на изображения, но это ссылка для скачивания.

Edit:

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

Это мой текущий код. Отображается «301 перемещено навсегда». Есть ли CURLOPT, которые я должен установить?

Ответы [ 3 ]

6 голосов
/ 26 сентября 2011
$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);
0 голосов
/ 08 мая 2014

Здесь можно напрямую сохранить изображение в файл (вместо использования 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
0 голосов
/ 26 сентября 2011

fopen - зависит от ваших настроек php, если URL-fopen разрешен.

или curl

см. Подробное руководство по php.

...