当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP读取Excel内的图片(phpspreadsheet和PHPExcel扩展库)

PHP读取Excel内的图片(phpspreadsheet和PHPExcel扩展库)

2020年03月09日  | 移动技术网IT编程  | 我要评论

训龙记2电影,玉蒲团之初入桃源洞,大内邪仙

今天接到了一个从excel内读取图片的需求,在网上查找了一些资料,基本实现了自己的需求,不过由于查到的一些代码比较久远,不能直接移植到自己的项目里,需要稍加改动一下。

这里介绍一下分别使用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读取excel内的图片,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网