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

时间:2014-10-01 05:43:45

标签: php image ms-word

  

成功读取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();

1 个答案:

答案 0 :(得分:0)

请试试这个

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