从word文件中提取图像后,在系统上创建副本


After extracting image from the word file create copy on the system

在成功读取msword文件的图像后,我显示了它。

现在我想复制那些图像。图像复制功能工作正常,但图像格式中断

代码为

$document = 'wordfile.docx';
readZippedImages($document);
function readZippedImages($filename) {
$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'])) {
saveImage('http://localhost/readfileimage/display.php?filename=".$filename."&index=".$i."');
echo "<image src='display.php?filename=".$filename."&index=".$i."' /><hr />";
}
}
}
}
function saveImage($path)
{
copy($path, rand().'-flower.jpg');
}    

这是display.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'])) {
  //$zip->open($path, ZIPARCHIVE::CREATE);
  /*Get the content of the specified index of ZIP archive*/
          echo $zip->getFromIndex($_GET['index']);
      }
      $zip->close();

请尝试此

function saveImage($path) {
  $imageUrl = $path;
  $contents = file_get_contents(trim($imageUrl));
  if ($contents) {
    file_put_contents('/path/to/save/flower.jpg', $contents);
  }
}