解码base64并传递给zip文件

时间:2017-03-18 07:43:18

标签: php

import pydotplus 
from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)
with open("iris.dot", 'w') as f:
    f = tree.export_graphviz(clf, out_file=f)

dot_data = tree.export_graphviz(clf, out_file=None) 
graph = pydotplus.graph_from_dot_data(dot_data) 
graph.write_pdf("iris.pdf") 

如何使用base64解码我的画布数据并输入我的zip文件,我不能在这里工作,我的目的是让我的画布数据解码为base64并将其压缩在一起。

zip文件没有创建,我不明白为什么。

2 个答案:

答案 0 :(得分:1)

试试这个:

public function store($location){
if($this->zip->open($location, file_exists($location) ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE)){
foreach($this->files as $file){
$this->count++;
$this->image_name ="OrderImg".$this->count.".png";
$file_encoded = base64_encode($file);
$this->zip->addFromString($this->image_name, base64_decode($file_encoded));
}
$this->zip->close();
}
}

即使未解码,您也可以将其作为图像文件添加到zip:

$this->zip->addFromString($this->image_name,$file_encoded);

答案 1 :(得分:0)

我可能会参加派对,但是这样的事你可以尝试:

public function store($location)
{   
    if($this->zip->open($location, file_exists($location) ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE))
    {
      foreach($this->files as $file) // assuming you have file **array of base64 encoded images** here
      {
        $this->count++;
        $this->image_name ="OrderImg".$this->count.".png";
        $this->set = str_replace('data:image/png;base64,', '', $file);
        $this->set = str_replace(' ', '+', $file);
        $this->zip->addFromString($this->image_name, base64_decode($this->set));// assuming you have base64 encoded string here obtained for $file
      }
        $this->zip->close();
  }
}

当您添加尝试使用base64编码的字符串时,您必须使用addFromString()而不是addFile()

对于addfile:

bool ZipArchive::addFile ( string $filename [, string $localname = NULL [, int $start = 0 [, int $length = 0 ]]] )

或只是

bool ZipArchive::addFile ( string $file_name_till_fullpath, string $name_you_want_inarchive )

和addFromString:

bool ZipArchive::addFromString ( string $localname , string $contents )