亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 ZipArchive 保存 PHP 從 DOCX 獲取的圖像

如何使用 ZipArchive 保存 PHP 從 DOCX 獲取的圖像

PHP
蕭十郎 2022-07-16 17:08:19
簡要說明:我有一個 Docx 文件。我在 PHP 中完成了一個簡單的代碼,它提取該文件中的圖像并將其顯示在頁面上。我想要實現的目標:我希望這些圖像應該以相同的名稱和格式保存在我的 php 文件旁邊。我的文件夾有sample.docx(其中有圖像),extract.php(從 docx 中提取圖像)和display.php下面是代碼extract.php<?php    /*Name of the document file*/$document = 'sample.docx';/*Function to extract images*/ function readZippedImages($filename) {    /*Create a new ZIP archive object*/    $zip = new ZipArchive;    /*Open the received archive file*/    if (true === $zip->open($filename)) {        for ($i=0; $i<$zip->numFiles;$i++) {            /*Loop via all the files to check for image files*/            $zip_element = $zip->statIndex($i);                            /*Check for images*/            if(preg_match("([^\s]+(\.(?i)(jpg|jpeg|png|gif|bmp))$)",$zip_element['name'])) {                    /*Display images if present by using display.php*/                echo "<image src='display.php?filename=".$filename."&index=".$i."' /><hr />";            }        }    }}readZippedImages($document);?>display.php<?php/*Tell the browser that we want to display an image*/header('Content-Type: image/jpeg');        /*Create a new ZIP archive object*/$zip = new ZipArchive;/*Open the received archive file*/if (true === $zip->open($_GET['filename'])) {    /*Get the content of the specified index of ZIP archive*/echo $zip->getFromIndex($_GET['index']);}$zip->close();?>我怎樣才能做到這一點?
查看完整描述

1 回答

?
慕容708150

TA貢獻1831條經驗 獲得超4個贊

我不確定您是否需要像這樣多次打開 zip 存檔 - 特別是當另一個實例已經打開但我很想嘗試以下內容時 - 我應該強調它完全未經測試。


測試后更新:如果您這樣做,則無需使用display.php- 似乎在不同的.docx文件上工作正常。返回的數據$zip->getFromIndex產生原始圖像數據(所以我發現),因此由于長度原因,無法將其傳遞到查詢字符串中。我試圖避免不必要地打開/關閉 zip 存檔,因此下面的方法將原始數據添加到輸出數組,然后使用這個內聯的 base64 編碼數據顯示圖像。


<?php    

    #extract.php


    $document = 'sample.docx';


    function readZippedImages($filename) {

        $paths=[];

        $zip = new ZipArchive;

        if( true === $zip->open( $filename ) ) {

            for( $i=0; $i < $zip->numFiles;$i++ ) {

                $zip_element = $zip->statIndex( $i );

                if( preg_match( "([^\s]+(\.(?i)(jpg|jpeg|png|gif|bmp))$)", $zip_element['name'] ) ) {

                    $paths[ $zip_element['name'] ]=base64_encode( $zip->getFromIndex( $i ) );

                }

            }

        }

        $zip->close();

        return $paths;

    }

    $paths=readZippedImages( $document );






    /* to display & save the images */

    foreach( $paths as $name => $data ){

        $filepath=__DIR__ . '/' . $name;

        $dirpath=pathinfo( $filepath, PATHINFO_DIRNAME );

        $ext=pathinfo( $name, PATHINFO_EXTENSION );

        if( !file_exists( $dirpath ) )mkdir( $dirpath,0777, true );

        if( !file_exists( $filepath ) )file_put_contents( $filepath, base64_decode( $data ) );


        printf('<img src="data:image/%s;base64, %s" />', $ext, $data );

    }

?>


查看完整回答
反對 回復 2022-07-16
  • 1 回答
  • 0 關注
  • 157 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號