今天接到了一個從Excel內讀取圖片的需求,在網上查找了一些資料,基本實現了本身的需求,不過因爲查到的一些代碼比較久遠,不能直接移植到本身的項目裏,須要稍加改動一下。php
這裏介紹一下分別使用phpspreadsheet和PHPExcel擴展庫來實現讀取Excel內圖片的功能:html
首先安裝phpspreadsheet,因爲線上服務器PHP版本是PHP5.6,因此須要安裝兼容PHP5.6的版本,這裏安裝1.8.2版本shell
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; }
能夠看到,圖片被讀取並存到了本地服務器中composer
PHPExcel實現從Excel文件裏讀取內容的方法和phpspreadsheet幾乎同樣,筆記phpspreadsheet就是在PHPExcel基礎上寫的,不過PHPExcel因爲已經被廢棄了,因此建議優先使用phpspreadsheet,若是原來項目裏一直使用了PHPExcel也能夠繼續使用PHPExcel的方法ide
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; }
參考文章:ui
PHPExcel數據導入(含圖片)excel