您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關PHP如何讀取Excel圖片對象并保存替換為相對路徑,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
PHP利用PhpSpreadsheet 和 xlswriter 讀取Excel圖片對象,保存替換為相對路徑
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2021/1/11 0011 * Time: 8:59 */ namespace App\Services; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Exception; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; use Vtiful\Kernel\Excel; /** * 讀取Excel圖片并保存其路徑 * Class ExcelImagePathServer * @package App\Services */ class ExcelImagePathServer { /** * @var string */ protected $relative_path = '/images'; /** * @var Spreadsheet */ protected $spreadsheet; /** * @var Excel */ protected $xls_writer; /** * @var Excel */ protected $sheet_writer; /** * @var string */ protected $image_path; /** * ExcelImagePathServer constructor. * @param string $excel_file * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception */ public function __construct($excel_file) { $reader = IOFactory::createReader('Xlsx'); $this->spreadsheet = $reader->load($excel_file); $config = ['path' => dirname($excel_file)]; $this->xls_writer = new Excel($config); $this->image_path = dirname($excel_file) . $this->relative_path; if (!is_dir($this->image_path)) { mkdir($this->image_path, 0755); } } /** * @throws Exception */ public function handle() { $write_filename = date('YmdHis') . '.xlsx'; $sheetCount = $this->spreadsheet->getSheetCount(); for ($i = 0; $i < $sheetCount; $i++) { $worksheet = $this->spreadsheet->getSheet($i); $data = $worksheet->toArray(); $sheetNames = $this->spreadsheet->getSheetNames(); var_dump($sheetCount, $sheetNames); // 讀取并修改 foreach ($worksheet->getDrawingCollection() as $drawing) { /**@var $drawing Drawing* */ list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates()); $image_filename = "/{$i}-" . $drawing->getCoordinates(); $image_suffix = $this->saveImage($drawing, $image_filename); $image_name = ltrim($this->relative_path, '/') . "{$image_filename}.{$image_suffix}"; var_dump($image_name); $startColumn = $this->ABC2decimal($startColumn); $data[$startRow - 1][$startColumn] = $image_name; } // 寫入文件 if ($i == 0) { $this->sheet_writer = $this->xls_writer->fileName($write_filename, $sheetNames[$i])->data($data); } else { // 向文件中追加工作表 $this->sheet_writer->addSheet($sheetNames[$i])->data($data); } } // 最后的最后,輸出文件 $filePath = $this->sheet_writer->output(); var_dump($filePath); } /** * 保存圖片 * * @param Drawing $drawing * @param $image_filename * @return string * @throws Exception */ protected function saveImage(Drawing $drawing, $image_filename) { $image_filename .= '.' . $drawing->getExtension(); switch ($drawing->getExtension()) { case 'jpg': case 'jpeg': $source = imagecreatefromjpeg($drawing->getPath()); imagejpeg($source, $this->image_path . $image_filename); break; case 'gif': $source = imagecreatefromgif($drawing->getPath()); imagegif($source, $this->image_path . $image_filename); break; case 'png': $source = imagecreatefrompng($drawing->getPath()); imagepng($source, $this->image_path . $image_filename); break; default: throw new Exception('image format error!'); } return $drawing->getExtension(); } /** * 坐標轉換 * * @param $abc * @return float|int */ protected 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圖片對象并保存替換為相對路徑”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。