如何从word文件中提取图像并将图像保存在php中的另一个位置


how to extract image from word file and save image in another location in php

我希望图像形成word文件,并将这些图片保存在另一个文件夹中。

$document = 'hindiquestion.docx';
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'])) {
              saveImage('display.php?filename=".$filename."&index=".$i."');/* here this function save file in folder but not with image only corrupt file save in folder*/
              echo "<image src='display.php?filename=".$filename."&index=".$i."' /><hr />";/*Display images if present by using display.php*/
           }//image serach
        }
    }
}

function saveImage($path)
{
    copy($path, rand().'-img.jpg');    
}
readZippedImages($document);
?>

您正在添加display.php?。。传递到saveImage的路径。saveImage需要一个纯路径。

错误:

saveImage('display.php?filename=".$filename."&index=".$i."');

正确:

saveImage($filename);

此外,如果图像是png,则将其保存为.jpg,这是不好的(除非它只是用于测试)。

有关将图像从word文件保存到某个文件夹(PowerShell)的脚本,请参阅https://gallery.technet.microsoft.com/How-to-save-from-word-file-46b72800

Add-Type -Assembly “system.io.compression.filesystem” 
#copy and renaming the doc to .zip file 
$ZipFile = $env:temp + "'" + [guid]::NewGuid().ToString() + ".zip" 
Copy-Item -Path $WordFilePath -Destination $ZipFile
#extract all file to tmp folder
$TmpFolder = $env:temp + "'" + [guid]::NewGuid().ToString()
[io.compression.zipfile]::ExtractToDirectory($ZipFile, $TmpFolder) 
#copy image files from media folder to destination 
Get-ChildItem "$TmpFolder'word'media'" -recurse | Copy-Item -destination $DestinationFolder

注意:首先需要创建文件夹"docimages"

 <?php
    class DImages {
        private $file;
        private $indexes = [ ];
        /** Local directory name where images will be saved */
        private $savepath = 'docimages';
        public function __construct( $filePath ) {
            $this->file = $filePath;
            $this->extractImages();
        }
        function extractImages() {
            $ZipArchive = new ZipArchive;
            if ( true === $ZipArchive->open( $this->file ) ) {
                for ( $i = 0; $i < $ZipArchive->numFiles; $i ++ ) {
                    $zip_element = $ZipArchive->statIndex( $i );
                    if ( preg_match( "([^'s]+('.(?i)(jpg|jpeg|png|gif|bmp))$)", $zip_element['name'] ) ) {
                        $imagename                   = explode( '/', $zip_element['name'] );
                        $imagename                   = end( $imagename );
                        $this->indexes[ $imagename ] = $i;
                    }
                }
            }
        }
        function saveAllImages() {
            if ( count( $this->indexes ) == 0 ) {
                echo 'No images found';
            }
            foreach ( $this->indexes as $key => $index ) {
                $zip = new ZipArchive;
                if ( true === $zip->open( $this->file ) ) {
                    file_put_contents( dirname( __FILE__ ) . '/' . $this->savepath . '/' . $key, $zip->getFromIndex( $index ) );
                }
                $zip->close();
            }
        }
    }
    $DImages = new DImages("testdoc.docx");
    ?>