php PHP保存图像文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3693058/
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
PHP save image file
提问by Mick
Possible Duplicate:
Saving image from PHP URL
可能的重复:
从 PHP URL 保存图像
I have an image as a url link from the 3rd party web thumb site (IE http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com) What I would like to do is run a script that takes the image and saves it in a directory on my server using php. How to do this? would I use File Write?
我有一个图像作为来自第 3 方网站拇指网站的 url 链接(IE http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com)我想做的是运行一个使用 php 获取图像并将其保存在我服务器上的目录中的脚本。这该怎么做?我会使用文件写入吗?
回答by Tim Cooper
No need to create a GD resource, as someone else suggested.
无需像其他人建议的那样创建 GD 资源。
$input = 'http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com';
$output = 'google.com.jpg';
file_put_contents($output, file_get_contents($input));
Note: this solution only works if you're setup to allow fopen access to URLs. If the solution above doesn't work, you'll have to use cURL.
注意:此解决方案仅在您设置为允许 fopen 访问 URL 时才有效。如果上述解决方案不起作用,则必须使用 cURL。
回答by Mark Lalor
Note: you should use the accepted answerif possible. It's better than mine.
注意:如果可能,您应该使用已接受的答案。它比我的好。
It's quite easy with the GD library.
使用GD 库很容易。
It's built in usually, you probably have it (use phpinfo()
to check)
它通常是内置的,您可能拥有它(用于phpinfo()
检查)
$image = imagecreatefromjpeg("http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com");
imagejpeg($image, "folder/file.jpg");
The above answer is better (faster) for most situations, but with GD you can also modify it in some form (cropping for example).
上面的答案在大多数情况下更好(更快),但使用 GD 您也可以以某种形式修改它(例如裁剪)。
$image = imagecreatefromjpeg("http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com");
imagecopy($image, $image, 0, 140, 0, 0, imagesx($image), imagesy($image));
imagejpeg($image, "folder/file.jpg");
This only works if allow_url_fopen
is true
(it is by default)
这仅适用allow_url_fopen
是true
(这是默认情况下)