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

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内的图片,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对黑区网络网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/1932.html