您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

利用php实现读取excel中的图片

2025/1/6 0:52:50发布21次查看
要实现读取excel中的图片,可以通过phpspreadsheet来实现。phpspreadsheet是一个纯php编写的库,并引入了命名空间、psr规范等。
使用composer安装phpspreadsheet
composer require phpoffice/phpspreadsheet
github下载:
https://github.com/phpoffice/phpspreadsheet
(免费视频教程推荐:php视频教程)
excel图片如下图:
项目实例:
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;}
结果如图:
相关文章教程推荐:php教程
以上就是利用php实现读取excel中的图片的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product