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

时间:2014-09-30 08:21:53

标签: 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);
?>

3 个答案:

答案 0 :(得分:1)

您是将display.php?..添加到传递给saveImage的路径中。 saveImage需要一个普通的路径。

错:

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

正确:

saveImage($filename);

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

答案 1 :(得分:0)

对于将图像从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

答案 2 :(得分:0)

注意:首先你需要创建文件夹&#34; docimages&#34;

 <?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");

    ?>