使用PHP从MS Word文档中提取图像的最简单方法?

cod*_*ama 2 php ms-word

这可以使用PHP从MS Word文档中提取图像吗?如果是这样,怎么样?

要求:绝对是老派的文档支持,但最好是新老两种.

App*_*ker 5

创建一个新的PHP文件并将其命名为extract.php并在其中添加以下代码.

<?php

/*Name of the document file*/
$document = 'attractive_prices.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);
?>
Run Code Online (Sandbox Code Playgroud)

现在创建另一个PHP文件并将其命名为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();
?>
Run Code Online (Sandbox Code Playgroud)

来源:使用PHP从DocX中提取图像