PHP读取Excel图片对象,并保存替换为相对路径

 4838

PHP利用PhpSpreadsheet 和 xlswriter 读取Excel图片对象,保存替换为相对路径

  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Administrator
  5.  * Date: 2021/1/11 0011
  6.  * Time: 8:59
  7.  */
  8. namespace App\Services; 
  9. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  10. use PhpOffice\PhpSpreadsheet\Exception;
  11. use PhpOffice\PhpSpreadsheet\IOFactory;
  12. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  13. use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
  14. use Vtiful\Kernel\Excel; 
  15. /**
  16.  * 读取Excel图片并保存其路径
  17.  * Class ExcelImagePathServer
  18.  * @package App\Services
  19.  */
  20. class ExcelImagePathServer
  21. {
  22.     /**
  23.      * @var string
  24.      */
  25.     protected $relative_path = '/images'; 
  26.     /**
  27.      * @var Spreadsheet
  28.      */
  29.     protected $spreadsheet; 
  30.     /**
  31.      * @var Excel
  32.      */
  33.     protected $xls_writer; 
  34.     /**
  35.      * @var Excel
  36.      */
  37.     protected $sheet_writer; 
  38.     /**
  39.      * @var string
  40.      */
  41.     protected $image_path; 
  42.     /**
  43.      * ExcelImagePathServer constructor.
  44.      * @param string $excel_file
  45.      * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
  46.      */
  47.     public function __construct($excel_file)
  48.     {
  49.         $reader = IOFactory::createReader('Xlsx');
  50.         $this->spreadsheet = $reader->load($excel_file); 
  51.         $config = ['path' => dirname($excel_file)];
  52.         $this->xls_writer = new Excel($config); 
  53.         $this->image_path = dirname($excel_file) . $this->relative_path;
  54.         if (!is_dir($this->image_path)) {
  55.             mkdir($this->image_path, 0755);
  56.         }
  57.     } 
  58.     /**
  59.      * @throws Exception
  60.      */
  61.     public function handle()
  62.     {
  63.         $write_filename = date('YmdHis') . '.xlsx';
  64.         $sheetCount = $this->spreadsheet->getSheetCount();
  65.         for ($i = 0; $i < $sheetCount; $i++) {
  66.             $worksheet = $this->spreadsheet->getSheet($i);
  67.             $data = $worksheet->toArray();
  68.             $sheetNames = $this->spreadsheet->getSheetNames();
  69.             var_dump($sheetCount, $sheetNames);
  70.             // 读取并修改
  71.             foreach ($worksheet->getDrawingCollection() as $drawing) {
  72.                 /**@var $drawing Drawing* */
  73.                 list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates());
  74.                 $image_filename = "/{$i}-" . $drawing->getCoordinates();
  75.                 $image_suffix = $this->saveImage($drawing, $image_filename);
  76.                 $image_name = ltrim($this->relative_path, '/') . "{$image_filename}.{$image_suffix}";
  77.                 var_dump($image_name);
  78.                 $startColumn = $this->ABC2decimal($startColumn);
  79.                 $data[$startRow - 1][$startColumn] = $image_name;
  80.             } 
  81.             // 写入文件
  82.             if ($i == 0) {
  83.                 $this->sheet_writer = $this->xls_writer->fileName($write_filename, $sheetNames[$i])->data($data);
  84.             } else {
  85.                 // 向文件中追加工作表
  86.                 $this->sheet_writer->addSheet($sheetNames[$i])->data($data);
  87.             }
  88.         }
  89.         // 最后的最后,输出文件
  90.         $filePath = $this->sheet_writer->output();
  91.         var_dump($filePath);
  92.     } 
  93.     /**
  94.      * 保存图片
  95.      *
  96.      * @param Drawing $drawing
  97.      * @param $image_filename
  98.      * @return string
  99.      * @throws Exception
  100.      */
  101.     protected function saveImage(Drawing $drawing, $image_filename)
  102.     {
  103.         $image_filename .= '.' . $drawing->getExtension();
  104.         switch ($drawing->getExtension()) {
  105.             case 'jpg':
  106.             case 'jpeg':
  107.                 $source = imagecreatefromjpeg($drawing->getPath());
  108.                 imagejpeg($source, $this->image_path . $image_filename);
  109.                 break;
  110.             case 'gif':
  111.                 $source = imagecreatefromgif($drawing->getPath());
  112.                 imagegif($source, $this->image_path . $image_filename);
  113.                 break;
  114.             case 'png':
  115.                 $source = imagecreatefrompng($drawing->getPath());
  116.                 imagepng($source, $this->image_path . $image_filename);
  117.                 break;
  118.             default:
  119.                 throw new Exception('image format error!');
  120.         }
  121.         return $drawing->getExtension();
  122.     } 
  123.     /**
  124.      * 坐标转换
  125.      *
  126.      * @param $abc
  127.      * @return float|int
  128.      */
  129.     protected function ABC2decimal($abc)
  130.     {
  131.         $ten = 0;
  132.         $len = strlen($abc);
  133.         for ($i = 1; $i <= $len; $i++) {
  134.             $char = substr($abc, 0 - $i, 1);//反向获取单个字符
  135.             $int = ord($char);
  136.             $ten += ($int - 65) * pow(26, $i - 1);
  137.         }
  138.         return $ten;
  139.     }
  140. }

以上就是PHP读取Excel图片对象,并保存替换为相对路径的详细内容。更多关注站长图库教程栏目。



本文网址:https://www.zztuku.com/detail-8623.html
站长图库 - PHP读取Excel图片对象,并保存替换为相对路径
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐