duanao6704 2011-08-16 16:48
浏览 48

从图像目录创建多个HTML文件

I have a large body of technical illustrations that I need to Map area to form an online manual. I have already made the base engine for linking each file together and navigating forward and backwards. But due to the large amount of images I wanted to create a group of base HTML files that I could then go through and edit the relevant sections.

What I wanted was to search the directory (images/schematic/) and create a HTML file for each image (imagefilename.png). The file would use the name of the image (imagefilename). The body of the html file would be similar to as follows.

    $html='
       <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head> 
           <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
           <title>'.$imageFileName.'</title>
        </head> 
        <body>
          <div id="image-holder"> 
             <div class="img-title">changeMeLater</div>
             <img src="'.$imageDir .$FullImageFileName.'" width="640" height="751" alt="'.$imageFileName.'-diagram" usemap="#mapFor-'.$imageFileName.'"> 

             <map name="'.$imageFileName.'-imageMap" id="mapFor-'.$imageFileName.'"> 
         </map>
         </div> 
         <div id="partsDescription-tabContent" class="content"></div>    
         <div id="instructions-tabContent" class="content"></div>    
         <div id="notes-tabContent" class="content"></div>    
         <div id="stockControl-tabContent" class="content"></div>
        </body> 
        </html> ';

I found this code which did display a list of the files, and found out that it is the 'basename' that is removing the directory name.

      <?php
          $directory = "./images/schematic/";
          $directory = (!strstr($directory,"*") || $directory =="./" ) ? $directory."*" : $directory;
          $files = glob($directory);
          for($i=0;$i<sizeof($files) ; $i++){
          echo basename($files[$i])."<br/>
";
         }?>

But I do not understand how to do something to each return value from the image file search. As you can probably tell I have little to no idea when it comes to php but I am trying. If someone could point me in the right direction explaining how and why I would appreciate it.

Ok I have had a bit of a breakthrough. This code does work. Now I would like to tweek it. Suggestions on how to improve it are more than welcome.

      <?php

       $directory = "./images/schematic/";

       $directory = (!strstr($directory,"*") || $directory =="./" ) ? $directory."*" : $directory;

       $files = glob($directory);

       for($i=0;$i<sizeof($files) ; $i++)
        {
       $fileTitle= basename($files[$i], ".png");
       $imageSrc= $files[$i];


       $html='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>'.$pageTitle.'</title> 
      </head>  
      <body> 

     <div id="image-holder">
     <div class="img-title">'.$fileTitle.'</div>
     <img src=".'.$imageSrc.'" width="640" height="751" style="border: none;" alt="'.$fileTitle.'-diagram" usemap="#mapFor-'.$fileTitle.'">
     <map name="'.$fileTitle.'-imageMap" id="mapFor-'.$fileTitle.'">

    <!-------------------------------- '.$pageTitle.' Mapping -------------------------------------> 

    </map> 
    </div>

    <div id="partsDescription-tabContent" class="content"></div>    
    <div id="instructions-tabContent" class="content"></div>    
    <div id="notes-tabContent" class="content"></div>    
    <div id="stockControl-tabContent" class="content"></div>

    </body>
    </html>';


    $myfile = fopen ('./newdirectory/'.$fileTitle.'.html', 'w') or die("Can not open file");
    $outputFile = $html;
    fputs($myfile, $outputFile);
    fclose($myfile);

    };

    echo ("OK Files created")

   ?>

I have cobbled this together from various tutorials I found across the web. I would like it to only look image files, preferably .png and .jpeg. Can I get it to skip a file that exists (I am thinking for further down the line in case I have area mapped the images and it is overwritten.) I realize this may look messy but I am a complete newb to this.

  • 写回答

2条回答 默认 最新

  • douliu8327 2011-08-16 18:47
    关注

    The supplied code is giving you the file name. You just need to change the line.

    echo basename($files[$i])."<br/>
    ";
    

    and replace it with something like this.

    echo "<img src='/images/schematic/" . basename($files[$i]) . "'></img><br />
    ";
    

    That would then output your images one below the other.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用