(推荐学习:php视频教程)
这里介绍一下分别使用phpspreadsheet和phpexcel扩展库来实现读取excel内图片的功能:
phpspreadsheet
首先安装phpspreadsheet,由于线上服务器php版本是php5.6,所以需要安装兼容php5.6的版本,这里安装1.8.2版本
composer require phpoffice/phpspreadsheet=1.8.2
然后就可以在项目里使用了
use phpoffice\phpspreadsheet\cell\coordinate;use phpoffice\phpspreadsheet\iofactory;$imagefilepath = './uploads/imgs/'; //图片本地存储的路径if (!file_exists($imagefilepath)) { //如果目录不存在则递归创建 mkdir($imagefilepath, 0777, true);}try { $inputfilename = './files/1.xlsx'; //包含图片的excel文件 $objread = iofactory::createreader('xlsx'); $objspreadsheet = $objread->load($inputfilename); $objworksheet = $objspreadsheet->getsheet(0); $data = $objworksheet->toarray(); foreach ($objworksheet->getdrawingcollection() as $drawing) { list($startcolumn, $startrow) = coordinate::coordinatefromstring($drawing->getcoordinates()); $imagefilename = $drawing->getcoordinates() . mt_rand(1000, 9999); switch ($drawing->getextension()) { case 'jpg': case 'jpeg': $imagefilename .= '.jpg'; $source = imagecreatefromjpeg($drawing->getpath()); imagejpeg($source, $imagefilepath . $imagefilename); break; case 'gif': $imagefilename .= '.gif'; $source = imagecreatefromgif($drawing->getpath()); imagegif($source, $imagefilepath . $imagefilename); break; case 'png': $imagefilename .= '.png'; $source = imagecreatefrompng($drawing->getpath()); imagepng($source, $imagefilepath, $imagefilename); break; } $startcolumn = abc2decimal($startcolumn); $data[$startrow-1][$startcolumn] = $imagefilepath . $imagefilename; } dump($data);die();} catch (\exception $e) { throw $e;}public function abc2decimal($abc){ $ten = 0; $len = strlen($abc); for($i=1;$i<=$len;$i++){ $char = substr($abc,0-$i,1);//反向获取单个字符 $int = ord($char); $ten += ($int-65)*pow(26,$i-1); } return $ten;}
可以看到,图片被读取并存到了本地服务器中
phpexcel
phpexcel实现从excel文件里读取内容的方法和phpspreadsheet几乎一样,毕竟phpspreadsheet就是在phpexcel基础上写的,不过phpexcel由于已经被废弃了,所以建议优先使用phpspreadsheet,如果原来项目里一直使用了phpexcel也可以继续使用phpexcel的方法
use phpexcel_iofactory;use phpexcel_cell;try { $inputfilename = './files/1.xlsx'; $inputfiletype = phpexcel_iofactory::identify($inputfilename); $objreader = phpexcel_iofactory::createreader($inputfiletype); $objphpexcel = $objreader->load($inputfilename);} catch (\exception $e) { die('加载文件发生错误:"'.pathinfo($inputfilename,pathinfo_basename).'": '.$e->getmessage());}$sheet = $objphpexcel->getsheet(0);$data = $sheet->toarray(); //该方法读取不到图片,图片需单独处理$imagefilepath = './uploads/imgs/'; //图片本地存储的路径if (!file_exists($imagefilepath)) { mkdir($imagefilepath, 0777, true);}//处理图片foreach ($sheet->getdrawingcollection() as $img) { list($startcolumn, $startrow) = phpexcel_cell::coordinatefromstring($img->getcoordinates()); //获取图片所在行和列 $imagefilename = $img->getcoordinates() . mt_rand(1000, 9999); switch($img->getextension()) { case 'jpg': case 'jpeg': $imagefilename .= '.jpeg'; $source = imagecreatefromjpeg($img->getpath()); imagejpeg($source, $imagefilepath.$imagefilename); break; case 'gif': $imagefilename .= '.gif'; $source = imagecreatefromgif($img->getpath()); imagejpeg($source, $imagefilepath.$imagefilename); break; case 'png': $imagefilename .= '.png'; $source = imagecreatefrompng($img->getpath()); imagejpeg($source, $imagefilepath.$imagefilename); break; } $startcolumn = abc2decimal($startcolumn); $data[$startrow-1][$startcolumn] = $imagefilepath . $imagefilename;}var_dump($data);public function abc2decimal($abc){ $ten = 0; $len = strlen($abc); for($i=1;$i<=$len;$i++){ $char = substr($abc,0-$i,1);//反向获取单个字符 $int = ord($char); $ten += ($int-65)*pow(26,$i-1); } return $ten;}
本文来自,php问题栏目,欢迎学习!
以上就是php如何读取excel内的图片(附代码)的详细内容。
