I want the images form word file and save those picture in another folder.
$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);
?>
You are adding display.php?.. to the path passed to saveImage. saveImage expects a plain path.
Wrong:
saveImage('display.php?filename=".$filename."&index=".$i."');
Correct:
saveImage($filename);
Also, if the image is a png, you save it as .jpg which is not good (unless it's just for testing).
For the scripts of save images from word file into a certain folder (PowerShell), see 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
Note : first you need to create folder "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");
?>