将两个文件的对比差异生成图片

将两个文件的对比差异生成图片

作者:admin |  时间:2015-02-09 |  浏览:309 |  0 条评论

github地址:https://github.com/jishipu/CreateDiffPic

实现方法:生成两个文件的对比网页,然后用PhantomJS将网页生成图片。详情请见example.php:

<?php
    define("SITE_PATH", dirname(__FILE__));
    $old_path = SITE_PATH."/log/old";
    $new_path = SITE_PATH."/log/new";
    $js_path = SITE_PATH."/diff/deal.js";
    $request_url = "http://10.16.15.225/diff/index.php"; //根据自己的网络设置,即访问diff目录下的index.php文件
    $img_path = SITE_PATH."/log/diff.png";
    $command = "/usr/bin/phantomjs {$js_path} {$request_url} {$img_path}";
    shell_exec($command);
  ?>

diff目录下index.php源码:

<?php
define("SITE_PATH", dirname(__DIR__));
$base = @file_get_contents(SITE_PATH."/log/old");
$new = file_get_contents(SITE_PATH."/log/new");
?>
<html>
  <head>
  <script type='text/javascript' src='dojo.js'></script>
  <script type='text/javascript' src='diffview.js'></script>
  <script type='text/javascript' src='difflib.js'></script>
  <script type='text/javascript' src='jsbeautify.js'></script>
  <link rel="stylesheet" type="text/css" href="diffview.css">
  <style type='text/css'>
     .txt {display:none}
     #diffoutput {width:100%}
  </style>
  </head>
  <body>
    <div class='txt'>
    	<textarea id="baseText"><?=$base?></textarea>
    	<textarea id="newText"><?=$new?></textarea>
	  </div>
    <div id="diffoutput"> </div>
  <script type='text/javascript'>
    var $ = dojo.byId;
    dojo.require("dojo.io");
    function diffUsingJS() {
                var base_text = js_beautify($("baseText").value, 4, '');
    		var new_text = js_beautify($("newText").value, 4, '');
    		var base = difflib.stringAsLines(base_text);
    		var newtxt = difflib.stringAsLines(new_text);
    		var sm = new difflib.SequenceMatcher(base, newtxt);
    		var opcodes = sm.get_opcodes();
    		var diffoutputdiv = $("diffoutput");
    		while (diffoutputdiv.firstChild)
    			diffoutputdiv.removeChild(diffoutputdiv.firstChild);
    		diffoutputdiv.appendChild(diffview.buildView({baseTextLines: base,newTextLines: newtxt,opcodes: opcodes,baseTextName: "Base Text",newTextName: "New Text",contextSize: 10,viewType: 0}));
    }
    diffUsingJS();
  </script>
  </body>
</html>

相关推荐

发表评论

电子邮件地址不会被公开。

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>