批量导出图片(生成压缩包)

/导出图片/
public function exportimg(){
$img_array = [
[‘id’=>1,‘cover_img’=>‘https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png’],
[‘id’=>2,‘cover_img’=>‘https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png’],
[‘id’=>3,‘cover_img’=>‘https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png’],
[‘id’=>4,‘cover_img’=>‘https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png’],
];
$arr = [];
l o c a l = ′ . / d o w i m g / ′ ; / / 图 片 下 载 本 地 路 径 f o r e a c h ( local = './dow_img/';//图片下载本地路径 foreach( local=./dowimg/;//foreach(img_array as k k = > kk=> kk=>vv){
/将远程图片下载到本地/
$file_name = $vv[‘id’].’.jpg’;//图片名称
t h i s − > h t t p T o l o c a l ( this->httpTolocal( this>httpTolocal(vv[‘cover_img’], l o c a l , local, local,file_name);
$src = l o c a l . local. local.file_name;
a r r [ ] = [ ′ n a m e ′ = > arr[] = ['name'=> arr[]=[name=>file_name,‘src’=> s r c , ′ i d ′ = > src,'id'=> src,id=>vv[‘id’]];
}
$name = ‘zip名称’;//zip名称
d i r = ′ . / z i p / ′ ; / / z i p 所 在 目 录 / ∗ 判 断 是 否 存 在 目 录 ∗ / i f ( ! f i l e e x i s t s ( dir = './zip/';//zip所在目录 /*判断是否存在目录*/ if(!file_exists( dir=./zip/;//zip//if(!fileexists(dir)){
mkdir($dir,777);
}
$zip = new \ZipArchive();
$file_name = d i r . dir. dir.name.’.zip’;

    $res = $zip->open($file_name,\ZipArchive::CREATE);//创建zip
    if($res){
        foreach($arr as $a){
            $zip_dir = 'dow_img/'.$a['id'].'_img/';//压缩到指定文件夹路径
            $zip->addFile($a['src'], $zip_dir.$a['name']);
        }
        $zip->close();
        //清空(擦除)缓冲区并关闭输出缓冲
        ob_end_clean();
        //下载建好的.zip压缩包
        header("Access-Control-Allow-Origin: *"); // 允许任意域名发起的跨域请求
        header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With');
        header("Content-Type: application/force-download");//告诉浏览器强制下载
        header("Content-Transfer-Encoding: binary");//声明一个下载的文件
        header('Content-Type: application/zip');//设置文件内容类型为zip
        header('Content-Disposition: attachment; filename='.$name.'.zip');//声明文件名
        header('Content-Length: '.filesize($file_name));//声明文件大小
        error_reporting(0);
        //将欲下载的zip文件写入到输出缓冲
        readfile($file_name);
        //将缓冲区的内容立即发送到浏览器,输出
        flush();
        unlink($file_name);//导出zip 后,原先的文件夹清空(视情况而操作)
        foreach($arr as $d){
            unlink($d['src']);//删除下载下来的本地图片
        }
        exit;
    }else{
        $zip->close();
        $this->error('无法创建zip');
    }
}

//下载远程图片保存到本地
function getimage($url, $save_dir = ‘’, $filename = ‘’, KaTeX parse error: Expected '}', got 'EOF' at end of input: …{ if (trim(url) == ‘’) {
return array(‘file_name’ => ‘’, ‘save_path’ => ‘’, ‘error’ => 1);
}
if (trim($save_dir) == ‘’) {
KaTeX parse error: Expected 'EOF', got '}' at position 22: …ir = './'; }̲ if (trim(filename) == ‘’) {//保存文件名
e x t = s t r r c h r ( ext = strrchr( ext=strrchr(url, ‘.’);
if ($ext != ‘.gif’ && $ext != ‘.jpg’) {
return array(‘file_name’ => ‘’, ‘save_path’ => ‘’, ‘error’ => 3);
}
$filename = time() . KaTeX parse error: Expected 'EOF', got '}' at position 10: ext; }̲ if (0 !== …save_dir, ‘/’)) {
KaTeX parse error: Expected 'EOF', got '}' at position 22: …ir .= '/'; }̲ //创建保存目录 …save_dir) && !mkdir($save_dir, 0777, true)) {
return array(‘file_name’ => ‘’, ‘save_path’ => ‘’, ‘error’ => 5);
}

//获取远程文件所采用的方法
if ($type) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $img = curl_exec($ch);
    curl_close($ch);
} else {
    ob_start();
    readfile($url);
    $img = ob_get_contents();
    ob_end_clean();
}
//$size=strlen($img);
//文件大小

$fp2 = @fopen($save_dir . $filename, 'a');
fwrite($fp2, $img);
fclose($fp2);
unset($img, $url);
return array('file_name' => $filename, 'save_path' => $save_dir . $filename, 'error' => 0);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值