主頁(yè) > 知識(shí)庫(kù) > PHP讀取Excel內(nèi)的圖片(phpspreadsheet和PHPExcel擴(kuò)展庫(kù))

PHP讀取Excel內(nèi)的圖片(phpspreadsheet和PHPExcel擴(kuò)展庫(kù))

熱門標(biāo)簽:科大訊飛語(yǔ)音識(shí)別系統(tǒng) 阿里云 Linux服務(wù)器 服務(wù)器配置 Mysql連接數(shù)設(shè)置 銀行業(yè)務(wù) 團(tuán)購(gòu)網(wǎng)站 電子圍欄

今天接到了一個(gè)從Excel內(nèi)讀取圖片的需求,在網(wǎng)上查找了一些資料,基本實(shí)現(xiàn)了自己的需求,不過由于查到的一些代碼比較久遠(yuǎn),不能直接移植到自己的項(xiàng)目里,需要稍加改動(dòng)一下。

這里介紹一下分別使用phpspreadsheet和PHPExcel擴(kuò)展庫(kù)來(lái)實(shí)現(xiàn)讀取Excel內(nèi)圖片的功能:

 

PHPSpreadsheet

首先安裝phpspreadsheet,由于線上服務(wù)器PHP版本是PHP5.6,所以需要安裝兼容PHP5.6的版本,這里安裝1.8.2版本

composer require phpoffice/phpspreadsheet=1.8.2

然后就可以在項(xiàng)目里使用了

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
$imageFilePath = './uploads/imgs/'; //圖片本地存儲(chǔ)的路徑
if (!file_exists($imageFilePath)) { //如果目錄不存在則遞歸創(chuàng)建
 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);//反向獲取單個(gè)字符
  $int = ord($char);
  $ten += ($int-65)*pow(26,$i-1);
 }
 return $ten;
}

可以看到,圖片被讀取并存到了本地服務(wù)器中

 

PHPExcel

PHPExcel實(shí)現(xiàn)從Excel文件里讀取內(nèi)容的方法和phpspreadsheet幾乎一樣,畢竟phpspreadsheet就是在PHPExcel基礎(chǔ)上寫的,不過PHPExcel由于已經(jīng)被廢棄了,所以建議優(yōu)先使用phpspreadsheet,如果原來(lái)項(xiàng)目里一直使用了PHPExcel也可以繼續(xù)使用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('加載文件發(fā)生錯(cuò)誤:"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
$sheet = $objPHPExcel->getSheet(0);
$data = $sheet->toArray(); //該方法讀取不到圖片,圖片需單獨(dú)處理
$imageFilePath = './uploads/imgs/'; //圖片本地存儲(chǔ)的路徑
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);//反向獲取單個(gè)字符
  $int = ord($char);
  $ten += ($int-65)*pow(26,$i-1);
 }
 return $ten;
}

 總結(jié)

以上所述是小編給大家介紹的PHP讀取Excel內(nèi)的圖片,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

您可能感興趣的文章:
  • PHP使用PhpSpreadsheet操作Excel實(shí)例詳解
  • ThinkPHP5與單元測(cè)試PHPUnit使用詳解
  • PHP單元測(cè)試配置與使用方法詳解
  • PHPUnit + Laravel單元測(cè)試常用技能
  • PHP使用phpunit進(jìn)行單元測(cè)試示例
  • 使用PHPUnit進(jìn)行單元測(cè)試并生成代碼覆蓋率報(bào)告的方法
  • PHP單元測(cè)試框架PHPUnit用法詳解
  • php使用unset()刪除數(shù)組中某個(gè)單元(鍵)的方法
  • PhpSpreadsheet設(shè)置單元格常用操作匯總

標(biāo)簽:廣元 江蘇 棗莊 萍鄉(xiāng) 衡水 大理 衢州 蚌埠

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP讀取Excel內(nèi)的圖片(phpspreadsheet和PHPExcel擴(kuò)展庫(kù))》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266