我需要从PHP URL保存图像到我的PC。 假设我有一个页面http://example.com/image.php,上面只有一个“花”图像,没有别的。我如何保存这个图像从一个新名称的URL(使用PHP)?
当前回答
这里的答案都没有提到URL图像可以压缩(gzip)这一事实,而且在本例中都不起作用。
有两种解决方案可以帮助你解决这个问题:
第一个是使用cURL方法并设置curl_setopt CURLOPT_ENCODING, ":
// ... image validation ...
// Handle compression & redirection automatically
$ch = curl_init($image_url);
$fp = fopen($dest_path, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
// Exclude header data
curl_setopt($ch, CURLOPT_HEADER, 0);
// Follow redirected location
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Auto detect decoding of the response | identity, deflate, & gzip
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_exec($ch);
curl_close($ch);
fclose($fp);
它确实有效,但从对不同图像(png, jpg, ico, gif, svg)的数百个测试来看,这不是最可靠的方法。
最有效的方法是检测图像url是否有内容编码(例如gzip):
// ... image validation ...
// Fetch all headers from URL
$data = get_headers($image_url, true);
// Check if content encoding is set
$content_encoding = isset($data['Content-Encoding']) ? $data['Content-Encoding'] : null;
// Set gzip decode flag
$gzip_decode = ($content_encoding == 'gzip') ? true : false;
if ($gzip_decode)
{
// Get contents and use gzdecode to "unzip" data
file_put_contents($dest_path, gzdecode(file_get_contents($image_url)));
}
else
{
// Use copy method
copy($image_url, $dest_path);
}
有关gzdecode的更多信息,请参阅此线程。到目前为止,这工作得很好。如果有什么可以做得更好,请在下面的评论中告诉我们。
其他回答
$data = file_get_contents('http://example.com/image.php');
$img = imagecreatefromstring($data);
imagepng($img, 'test.png');
在这里,示例将远程图像保存到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');
这里的答案都没有提到URL图像可以压缩(gzip)这一事实,而且在本例中都不起作用。
有两种解决方案可以帮助你解决这个问题:
第一个是使用cURL方法并设置curl_setopt CURLOPT_ENCODING, ":
// ... image validation ...
// Handle compression & redirection automatically
$ch = curl_init($image_url);
$fp = fopen($dest_path, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
// Exclude header data
curl_setopt($ch, CURLOPT_HEADER, 0);
// Follow redirected location
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Auto detect decoding of the response | identity, deflate, & gzip
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_exec($ch);
curl_close($ch);
fclose($fp);
它确实有效,但从对不同图像(png, jpg, ico, gif, svg)的数百个测试来看,这不是最可靠的方法。
最有效的方法是检测图像url是否有内容编码(例如gzip):
// ... image validation ...
// Fetch all headers from URL
$data = get_headers($image_url, true);
// Check if content encoding is set
$content_encoding = isset($data['Content-Encoding']) ? $data['Content-Encoding'] : null;
// Set gzip decode flag
$gzip_decode = ($content_encoding == 'gzip') ? true : false;
if ($gzip_decode)
{
// Get contents and use gzdecode to "unzip" data
file_put_contents($dest_path, gzdecode(file_get_contents($image_url)));
}
else
{
// Use copy method
copy($image_url, $dest_path);
}
有关gzdecode的更多信息,请参阅此线程。到目前为止,这工作得很好。如果有什么可以做得更好,请在下面的评论中告诉我们。
我不能得到任何其他解决方案的工作,但我能够使用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');
}
创建一个名为images的文件夹,位于您计划放置将要创建的php脚本的路径中。确保它对每个人都有写权限,否则脚本将无法工作(它将无法将文件上传到目录中)。