PHP多个图片压缩成ZIP的方法

这篇文章主要为大家详细介绍了PHP多个图片压缩成ZIP的方法,可将多个文件压缩成一个zip压缩包,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

本文实例为大家分享了PHP多个图片压缩成ZIP的具体代码,供大家参考,具体内容如下。

可将多个文件压缩成一个zip压缩包,文件名可自定义(兼容中文文件名)

  1. <?php
  2. header("Content-type: text/html; charset=utf-8");
  3. class zipfile {
  4. var $datasec = array ();
  5. var $ctrl_dir = array ();
  6. var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
  7. var $old_offset = 0;
  8. function unix2_dostime($unixtime = 0){
  9. $timearray = ($unixtime == 0) ? getdate () : getdate($unixtime);
  10. if ($timearray ['year'] < 1980){
  11. $timearray ['year'] = 1980;
  12. $timearray ['mon'] = 1;
  13. $timearray ['mday'] = 1;
  14. $timearray ['hours'] = 0;
  15. $timearray ['minutes'] = 0;
  16. $timearray ['seconds'] = 0;
  17. }
  18. return (($timearray ['year'] - 1980) << 25) | ($timearray ['mon'] << 21) | ($timearray ['mday'] << 16) | ($timearray ['hours'] << 11) | ($timearray ['minutes'] << 5) | ($timearray ['seconds'] >> 1);
  19. }
  20. function add_file($data, $name, $time = 0){
  21. $name = urldecode($name);
  22. //判断文件名中是否含有中文
  23. if (preg_match("/[\x7f-\xff]/", $name)) {
  24. $name = $this->iconv_to_utf8($name,'GB2312');
  25. }
  26. $name = str_replace('\\', '/', $name);
  27. $dtime = dechex($this->unix2_dostime($time));
  28. $hexdtime = '\x' . $dtime [6] . $dtime [7] . '\x' . $dtime [4] . $dtime [5] . '\x' . $dtime [2] . $dtime [3] . '\x' . $dtime [0] . $dtime [1];
  29. eval('$hexdtime = "' . $hexdtime . '";');
  30. $fr = "\x50\x4b\x03\x04";
  31. $fr .= "\x14\x00";
  32. $fr .= "\x00\x00";
  33. $fr .= "\x08\x00";
  34. $fr .= $hexdtime;
  35. $unc_len = strlen($data);
  36. $crc = crc32($data);
  37. $zdata = gzcompress($data);
  38. $zdata = substr(substr($zdata, 0, strlen($zdata)- 4), 2);
  39. $c_len = strlen($zdata);
  40. $fr .= pack('V', $crc);
  41. $fr .= pack('V', $c_len);
  42. $fr .= pack('V', $unc_len);
  43. $fr .= pack('v', strlen($name));
  44. $fr .= pack('v', 0);
  45. $fr .= $name;
  46. $fr .= $zdata;
  47. $fr .= pack('V', $crc);
  48. $fr .= pack('V', $c_len);
  49. $fr .= pack('V', $unc_len);
  50. $this->datasec [] = $fr;
  51. $cdrec = "\x50\x4b\x01\x02";
  52. $cdrec .= "\x00\x00";
  53. $cdrec .= "\x14\x00";
  54. $cdrec .= "\x00\x00";
  55. $cdrec .= "\x08\x00";
  56. $cdrec .= $hexdtime;
  57. $cdrec .= pack('V', $crc);
  58. $cdrec .= pack('V', $c_len);
  59. $cdrec .= pack('V', $unc_len);
  60. $cdrec .= pack('v', strlen($name));
  61. $cdrec .= pack('v', 0);
  62. $cdrec .= pack('v', 0);
  63. $cdrec .= pack('v', 0);
  64. $cdrec .= pack('v', 0);
  65. $cdrec .= pack('V', 32);
  66. $cdrec .= pack('V', $this->old_offset);
  67. $this->old_offset += strlen($fr);
  68. $cdrec .= $name;
  69. $this->ctrl_dir[] = $cdrec;
  70. }
  71. function add_path($path, $l = 0){
  72. $d = @opendir($path);
  73. $l = $l > 0 ? $l : strlen($path) + 1;
  74. while($v = @readdir($d)){
  75. if($v == '.' || $v == '..'){
  76. continue;
  77. }
  78. $v = $path . '/' . $v;
  79. if(is_dir($v)){
  80. $this->add_path($v, $l);
  81. } else {
  82. $this->add_file(file_get_contents($v), substr($v, $l));
  83. }
  84. }
  85. }
  86. function file(){
  87. $data = implode('', $this->datasec);
  88. $ctrldir = implode('', $this->ctrl_dir);
  89. return $data . $ctrldir . $this->eof_ctrl_dir . pack('v', sizeof($this->ctrl_dir)) . pack('v', sizeof($this->ctrl_dir)) . pack('V', strlen($ctrldir)) . pack('V', strlen($data)) . "\x00\x00";
  90. }
  91. function add_files($files){
  92. foreach($files as $file){
  93. if (is_file($file)){
  94. $data = implode("", file($file));
  95. $this->add_file($data, $file);
  96. }
  97. }
  98. }
  99. function output($file){
  100. $fp = fopen($file, "w");
  101. fwrite($fp, $this->file ());
  102. fclose($fp);
  103. }
  104. /**
  105. * 转码为UTF-8
  106. * @param $keyword 要转码的字符串
  107. */
  108. function iconv_to_utf8($keyword, $to='UTF-8'){
  109. $encode = mb_detect_encoding($keyword, array('ASCII','UTF-8','GBK','GB2312'));
  110. if($encode != $to){
  111. $keyword = iconv($encode, $to, $keyword);
  112. }
  113. return $keyword;
  114. }
  115. }
  116. $dfile = tempnam('/tmp', 'tmp');//产生一个临时文件,用于缓存下载文件
  117. $zip = new zipfile();
  118. //----------------------
  119. $filename = 'image.zip'; //下载的默认文件名
  120. $filename = $filename;
  121. $host = 'http://test.love11.com';
  122. //$dir_name = $host.'/images/point_qrcode/';
  123. $image = array(
  124. array('image_src' => 'test2.jpg', 'image_name' => '中文1.jpg'),
  125. array('image_src' => 'weixin.jpg', 'image_name' => '中文2.jpg'),
  126. );
  127. foreach($image as $v){
  128. $zip->add_file(file_get_contents($dir_name.urlencode($v['image_src'])), $v['image_name']);
  129. // 添加打包的图片,第一个参数是图片内容,第二个参数是压缩包里面的显示的名称, 可包含路径
  130. // 或是想打包整个目录 用 $zip->add_path($image_path);
  131. }
  132. //----------------------
  133. $zip->output($dfile);
  134. // 下载文件
  135. ob_clean();
  136. header('Pragma: public');
  137. header('Last-Modified:'.gmdate('D, d M Y H:i:s') . 'GMT');
  138. header('Cache-Control:no-store, no-cache, must-revalidate');
  139. header('Cache-Control:pre-check=0, post-check=0, max-age=0');
  140. header('Content-Transfer-Encoding:binary');
  141. header('Content-Encoding:none');
  142. header('Content-type:multipart/form-data');
  143. header('Content-Disposition:attachment; filename="'.$filename.'"'); //设置下载的默认文件名
  144. header('Content-length:'. filesize($dfile));
  145. $fp = fopen($dfile, 'r');
  146. while(connection_status() == 0 && $buf = @fread($fp, 8192)){
  147. echo $buf;
  148. }
  149. fclose($fp);
  150. @unlink($dfile);
  151. @flush();
  152. @ob_flush();
  153. exit();
  154. ?>