Ger*_*obs 1 javascript php zip
我有几个链接到图像的URL,我希望能够使用这些图像制作.zip文件.用户基本上可以下载自己的图像.
图像不在我的服务器上,是否有办法以每个用户使用自己的带宽的方式压缩这些文件?
如果没有,这个问题的最佳解决方案是什么?(PHP或Javascript)
编辑:为什么2个downvotes?我不是要求代码.我有两个问题:1)我可以在不使用服务器带宽的情况下下载图像并将其压缩吗?2)如果不是,什么是最好的解决方案.
1.要下载图像,请http://example.com/image.php将图像作为test.jpg
a)如果你将allow_url_fopen设置为true:
$url = 'http://example.com/image.php';
$img = '/tempfolder/test.jpg';
file_put_contents($img, file_get_contents($url));
Run Code Online (Sandbox Code Playgroud)
b)否则使用cURL:
$ch = curl_init('http://example.com/image.php');
$fp = fopen('/tempfolder/test.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
Run Code Online (Sandbox Code Playgroud)
2.要压缩所有文件,您可以使用ziparchive创建zip.
$files = array('test.jpg', 'test1.jpg');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
Run Code Online (Sandbox Code Playgroud)
3.要使用以下行来传输zip文件,
$zipfilename = 'file.zip';
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=file.zip');
header('Content-Length: ' . filesize($zipfilename));
Run Code Online (Sandbox Code Playgroud)