从 PHP URL 保存图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/724391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving image from PHP URL
提问by riad
I need to save an image from a PHP URL to my PC.
Let's say I have a page, http://example.com/image.php, holding a single "flower" image, nothing else. How can I save this image from the URL with a new name (using PHP)?
我需要将图像从 PHP URL 保存到我的 PC。假设我有一个页面,http://example.com/image.php只有一个“花”图像,没有别的。如何使用新名称(使用 PHP)从 URL 保存此图像?
回答by vartec
If you have allow_url_fopenset to true:
如果您已allow_url_fopen设置为true:
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
Else use cURL:
否则使用cURL:
$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
回答by Halil ?zgür
copy('http://example.com/image.php', 'local/folder/flower.jpg');
回答by soulmerge
$content = file_get_contents('http://example.com/image.php');
file_put_contents('/my/folder/flower.jpg', $content);
回答by Sam152
Here you go, the example saves the remote image to image.jpg.
好了,该示例将远程图像保存到 image.jpg。
function save_image($inPath,$outPath)
{ //Download images from remote server
$in= fopen($inPath, "rb");
$out= fopen($outPath, "wb");
while ($chunk = fread($in,8192))
{
fwrite($out, $chunk, 8192);
}
fclose($in);
fclose($out);
}
save_image('http://www.someimagesite.com/img.jpg','image.jpg');
回答by stoefln
Vartec's answerwith cURLdidn't work for me. It did, with a slight improvement due to my specific problem.
Vartec的回答与卷曲并没有为我工作。确实如此,但由于我的具体问题而略有改进。
e.g.,
例如,
When there is a redirect on the server (like when you are trying to save the facebook profile image) you will need following option set:
当服务器上有重定向时(例如当您尝试保存 facebook 个人资料图片时),您将需要以下选项集:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
The full solution becomes:
完整的解决方案变为:
$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
回答by Andrew
I wasn't able to get any of the other solutions to work, but I was able to use wget:
我无法使用任何其他解决方案,但我能够使用 wget:
$tempDir = '/download/file/here';
$finalDir = '/keep/file/here';
$imageUrl = 'http://www.example.com/image.jpg';
exec("cd $tempDir && wget --quiet $imageUrl");
if (!file_exists("$tempDir/image.jpg")) {
throw new Exception('Failed while trying to download image');
}
if (rename("$tempDir/image.jpg", "$finalDir/new-image-name.jpg") === false) {
throw new Exception('Failed while trying to move image file from temp dir to final dir');
}
回答by zloctb
See file()PHP Manual:
请参阅file()PHP 手册:
$url = 'http://mixednews.ru/wp-content/uploads/2011/10/0ed9320413f3ba172471860e77b15587.jpg';
$img = 'miki.png';
$file = file($url);
$result = file_put_contents($img, $file)
回答by karl
$img_file='http://www.somedomain.com/someimage.jpg'
$img_file=file_get_contents($img_file);
$file_loc=$_SERVER['DOCUMENT_ROOT'].'/some_dir/test.jpg';
$file_handler=fopen($file_loc,'w');
if(fwrite($file_handler,$img_file)==false){
echo 'error';
}
fclose($file_handler);
回答by Ibrahim
Create a folder named images located in the path you are planning to place the php script you are about to create. Make sure it has write rights for everybody or the scripts won't work ( it won't be able to upload the files into the directory).
在您计划放置要创建的 php 脚本的路径中创建一个名为 images 的文件夹。确保它对每个人都有写权限,否则脚本将无法工作(它无法将文件上传到目录中)。
回答by sen
$data = file_get_contents('http://example.com/image.php');
$img = imagecreatefromstring($data);
imagepng($img, 'test.png');

