Skip to content

Commit 981e281

Browse files
committed
导出 加上来自网络的图片的
1 parent ae3a97b commit 981e281

File tree

1 file changed

+56
-17
lines changed

1 file changed

+56
-17
lines changed

src/Excel.php

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,51 @@ public static function exportData($list = [], $header = [], $filename = '', $suf
6767
// 写入excel
6868
$rowR = Coordinate::stringFromColumnIndex($span);
6969
$sheet->getColumnDimension($rowR)->setWidth(20);
70-
if(in_array($span,$image)&&file_exists($realData) || in_array($rowR,$image)&&file_exists($realData)){ // 如果这一列应该是图片
71-
$drawing = new Drawing();
72-
$drawing->setName('image');
73-
$drawing->setDescription('image');
74-
try{
75-
$drawing->setPath($realData);
76-
}catch(\Exception $e){
77-
echo $e->getMessage();
78-
echo '<br>可能是图片丢失了或者无权限';
79-
die;
70+
if(in_array($span,$image) || in_array($rowR,$image) ){ // 如果这一列应该是图片
71+
if(file_exists($realData)){ // 本地文件
72+
$drawing = new Drawing();
73+
$drawing->setName('image');
74+
$drawing->setDescription('image');
75+
try{
76+
$drawing->setPath($realData);
77+
}catch(\Exception $e){
78+
echo $e->getMessage();
79+
echo '<br>可能是图片丢失了或者无权限';
80+
die;
81+
}
82+
83+
$drawing->setWidth(80);
84+
$drawing->setHeight(80);
85+
$drawing->setCoordinates($rowR . $column);//A1
86+
$drawing->setOffsetX(12);
87+
$drawing->setOffsetY(12);
88+
$drawing->setWorksheet($spreadsheet->getActiveSheet());
89+
}else{ // 可能是 网络文件
90+
$img = self::curlGet($realData);
91+
$file_info = pathinfo($realData);
92+
$extension = $file_info['extension'];// 文件后缀
93+
$dir = '.' . DIRECTORY_SEPARATOR . 'execlImg'. DIRECTORY_SEPARATOR . \date('Y-m-d'). DIRECTORY_SEPARATOR;// 文件夹名
94+
$basename = time(). mt_rand(1000,9999).'.'.$extension;// 文件名
95+
is_dir($dir) OR mkdir($dir, 0777, true); //进行检测文件夹是否存在
96+
file_put_contents($dir.$basename , $img);
97+
$drawing = new Drawing();
98+
$drawing->setName('image');
99+
$drawing->setDescription('image');
100+
try{
101+
$drawing->setPath($dir.$basename);
102+
}catch(\Exception $e){
103+
echo $e->getMessage();
104+
echo '<br>可能是图片丢失了或者无权限';
105+
die;
106+
}
107+
108+
$drawing->setWidth(80);
109+
$drawing->setHeight(80);
110+
$drawing->setCoordinates($rowR . $column);//A1
111+
$drawing->setOffsetX(12);
112+
$drawing->setOffsetY(12);
113+
$drawing->setWorksheet($spreadsheet->getActiveSheet());
80114
}
81-
82-
$drawing->setWidth(80);
83-
$drawing->setHeight(80);
84-
$drawing->setCoordinates($rowR . $column);//A1
85-
$drawing->setOffsetX(12);
86-
$drawing->setOffsetY(12);
87-
$drawing->setWorksheet($spreadsheet->getActiveSheet());
88115
}else{
89116
$sheet->setCellValue($rowR . $column, $realData);
90117
}
@@ -348,4 +375,16 @@ protected static function formattingField($row, $field)
348375

349376
return is_array($row) ? false : $row;
350377
}
378+
379+
public static function curlGet($url)
380+
{
381+
$ch = \curl_init();
382+
\curl_setopt($ch, CURLOPT_URL, $url);
383+
\curl_setopt($ch, CURLOPT_HEADER, 0);
384+
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
385+
\curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 这个是重点 请求https。
386+
$data = \curl_exec($ch);
387+
\curl_close($ch);
388+
return $data;
389+
}
351390
}

0 commit comments

Comments
 (0)