PHP讀取Excel中圖片的方法

        今天接到了一個從Excel內(nèi)讀取圖片的需求,在網(wǎng)上查找了一些資料,基本實現(xiàn)了自己的需求,不過由于查到的一些代碼比較久遠,里面一些庫方法已經(jīng)被移除不存在了,所以不能直接移植到自己的項目里,需要稍加改動一下。

        10年積累的成都做網(wǎng)站、網(wǎng)站建設經(jīng)驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡服務。我雖然不認識你,你也不認識我。但先網(wǎng)站設計后付款的網(wǎng)站建設流程,更有巴宜免費網(wǎng)站建設讓你可以放心的選擇與我們合作。

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

        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)) { //如果目錄不存在則遞歸創(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);//反向獲取單個字符
        
                $int = ord($char);
                $ten += ($int-65)*pow(26,$i-1);
            }
            return $ten;
        }

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

        PHP讀取Excel中圖片的方法

        PHPExcel

        PHPExcel實現(xiàn)從Excel文件里讀取內(nèi)容的方法和phpspreadsheet幾乎一樣,畢竟phpspreadsheet就是在PHPExcel基礎上寫的,不過PHPExcel由于已經(jīng)被廢棄了,所以建議優(yōu)先使用phpspreadsheet,如果原來項目里一直使用了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ā)生錯誤:"'.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;
        }

        以上就是PHP如何讀取Excel內(nèi)的圖片(附代碼)的詳細內(nèi)容,更多請關注創(chuàng)新互聯(lián)其它相關文章!

        當前標題:PHP讀取Excel中圖片的方法
        當前URL:http://www.muchs.cn/article26/gdgdjg.html

        成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供建站公司網(wǎng)站設計公司、網(wǎng)站內(nèi)鏈、小程序開發(fā)定制網(wǎng)站、外貿(mào)建站

        廣告

        聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

        h5響應式網(wǎng)站建設