首頁  >  文章  >  後端開發  >  PHP如何讀取Excel內的圖片(附程式碼)

PHP如何讀取Excel內的圖片(附程式碼)

angryTom
angryTom轉載
2019-11-28 14:57:105350瀏覽

PHP如何讀取Excel內的圖片(附程式碼)

今天接到了一個從Excel內讀取圖片的需求,在網路上找了一些資料,基本上實作了自己的需求,不過由於查到的一些程式碼比較久遠,裡面有些函式庫方法已經被移除不存在了,所以不能直接移植到自己的專案裡,需要稍加改動一下。

(推薦學習:PHP影片教學

這裡介紹一下分別使用phpspreadsheet和PHPExcel擴充函式庫來實作讀取Excel內圖片的功能:

PHP如何讀取Excel內的圖片(附程式碼)

PHPSpreadsheet

#先安裝phpspreadsheet,由於線上伺服器PHP版本是PHP5.6,所以需要安裝相容PHP5 .6的版本,這裡安裝1.8.2版本

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;
}

可以看到,圖片被讀取並存到了本地伺服器中

PHP如何讀取Excel內的圖片(附程式碼)

PHPExcel

PHPExcel實作從Excel檔案讀取內容的方法和phpspreadsheet幾乎一樣,畢竟phpspreadsheet就是在PHPExcel基礎上寫的,不過PHPExcel由於已經被廢棄了,所以建議優先使用phpspreadsheet,如果原來專案裡一直使用了PHPExcel也可以繼續使用PHPExcel的方法

use PHPExcel_IOFactory;
use PHPExcel_Cell;

try {
    $inputFileName = &#39;./files/1.xlsx&#39;;
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
} catch (\Exception $e) {
    die(&#39;加载文件发生错误:"&#39;.pathinfo($inputFileName,PATHINFO_BASENAME).&#39;": &#39;.$e->getMessage());
}

$sheet = $objPHPExcel->getSheet(0);
$data = $sheet->toArray(); //该方法读取不到图片,图片需单独处理
$imageFilePath = &#39;./uploads/imgs/&#39;; //图片本地存储的路径
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 &#39;jpg&#39;:
        case &#39;jpeg&#39;:
            $imageFileName .= &#39;.jpeg&#39;;
            $source = imagecreatefromjpeg($img->getPath());
            imagejpeg($source, $imageFilePath.$imageFileName);
            break;
        case &#39;gif&#39;:
            $imageFileName .= &#39;.gif&#39;;
            $source = imagecreatefromgif($img->getPath());
            imagejpeg($source, $imageFilePath.$imageFileName);
            break;
        case &#39;png&#39;:
            $imageFileName .= &#39;.png&#39;;
            $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中文網,php問題欄目,歡迎學習!  

以上是PHP如何讀取Excel內的圖片(附程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:cnblogs.com。如有侵權,請聯絡admin@php.cn刪除