国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

基于linnux+phantomjs實(shí)現(xiàn)生成圖片格式的網(wǎng)頁快照

2024-05-04 23:34:17
字體:
供稿:網(wǎng)友
在代碼區(qū)看到一個(gè)生成站點(diǎn)快照的代碼,看了半天才發(fā)現(xiàn),作者僅僅貼出來業(yè)務(wù)代碼,最核心的生成快照?qǐng)D片的代碼反而沒有給出來。 以前記得google搜索提供站點(diǎn)縮略圖,那時(shí)候覺得好神奇,但是沒有花時(shí)間去做深入的調(diào)研。昨天又遇到了,那就順便調(diào)研下吧。
 

安裝擴(kuò)展:
  (1)下面是我在linux上的安裝過程,如果沒有安裝git請(qǐng)先yum install git
    安裝casperjs

 

復(fù)制代碼代碼如下:

    cd /
    git clone git://github.com/n1k0/casperjs.git
    cd casperjs
    ln -sf /casperjs/bin/casperjs /usr/local/bin/casperjs  //可以忽略 實(shí)際執(zhí)行中php是執(zhí)行 /casperjs/bin/casperjs

  
    (2)安裝phantomjs,下載地址:http://phantomjs.org/download.html
       下載后操作很簡(jiǎn)單,直接把解壓好的/bin/phantomjs移動(dòng)到/usr/local/bin/phantomjs就可以了。/
       測(cè)試phantomjs --version 有結(jié)果不報(bào)錯(cuò),說明安裝OK
 
    (3)安裝字體
      1. 首先獲得一套“微軟雅黑”字體庫(Google一下一大把),包含兩個(gè)文件msyh.ttf(普通)、msyhbd.ttf(加粗);
      2. 在/usr/share/fonts目錄下建立一個(gè)子目錄,例如win,命令如下:

 

 

復(fù)制代碼代碼如下:

# mkdir /usr/share/fonts/win

 

      3. 將msyh.ttf和msyhbd.ttf復(fù)制到該目錄下,例如這兩個(gè)文件放在/root/Desktop下,使用命令:

 

復(fù)制代碼代碼如下:

 # cd /root/Desktop
 # cp msyh.ttf msyhbd.ttf  /usr/share/fonts/win/

 

      4. 建立字體索引信息,更新字體緩存:

 

復(fù)制代碼代碼如下:

   # cd /usr/share/fonts/win
          # mkfontscale  (如果提示 mkfontscale: command not found,需自行安裝 # yum install mkfontscale )
          # mkfontdir
          # fc-cache    (如果提示 fc-cache: command not found,則需要安裝# yum install fontconfig )

 

   至此,字體已經(jīng)安裝完畢!

 

  1. <?php  
  2.   if (isset($_GET['url']))  
  3.   {  
  4.     set_time_limit(0);  
  5.    
  6.     $url = trim($_GET['url']);  
  7.     $filePath = md5($url).'.png';  
  8.     if (is_file($filePath))  
  9.     {  
  10.       exit($filePath);  
  11.     }  
  12.   
  13.     //如果不加這句就會(huì)報(bào)錯(cuò)“Fatal: [Errno 2] No such file or directory; did you install phantomjs?”,詳情參考http://mengkang.net/87.html 
  14.     putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs"); 
  15.     $command = "phantomjs phantomjs.js {$url} {$filePath}";  
  16.     @exec($command);  
  17.    
  18.     exit($filePath);  
  19.   }  
  20. ?>  
  21.    
  22. <!DOCTYPE html>  
  23. <html>  
  24. <head>  
  25. <meta charset="utf-8" />  
  26. <meta name="viewport" content="width=device-width, initial-scale=1.0" />  
  27. <meta name="keywords" content="" />  
  28. <meta name="description" content="" />  
  29. <title>快照生成</title>  
  30. <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>  
  31. <style>  
  32. * {margin: 0; padding: 0; } form {padding: 20px; } div {margin: 20px 0 0; } input {width: 200px; padding: 4px 2px; } #placeholder {display: none; }  
  33. </style> 
  34. </head>  
  35.    
  36. <body>  
  37.   <form action="" id="form">  
  38.     <input type="text" id="url" />  
  39.     <button type="submit">生成快照</button>  
  40.    
  41.     <div>  
  42.       <img src="" alt="" id="placeholder" />  
  43.     </div>  
  44.   </form>  
  45.   <script>  
  46.   $(function(){  
  47.     $('#form').submit(function(){  
  48.       if (typeof($(this).data('generate')) !== 'undefined' && $(this).data('generate') === true)  
  49.       {  
  50.         alert('正在生成網(wǎng)站快照,請(qǐng)耐心等待...');  
  51.         return false;  
  52.       }  
  53.    
  54.       $(this).data('generate', true);  
  55.       $('button').text('正在生成快照...').attr('disabled', true);  
  56.    
  57.       $.ajax({  
  58.         type: 'GET',  
  59.         url: '?',  
  60.         data: 'url=' + $('#url').val(),  
  61.         success: function(data){  
  62.           $('#placeholder').attr('src', data).show();  
  63.           $('#form').data('generate', false);  
  64.           $('button').text('生成快照').attr('disabled', false);  
  65.         }  
  66.       });  
  67.    
  68.       return false;  
  69.     });  
  70.   });  
  71.   </script>  
  72. </body>  
  73. </html> 
?
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 巴楚县| 乌拉特后旗| 含山县| 河曲县| 曲沃县| 高邮市| 武定县| 濮阳市| 鹤岗市| 通榆县| 新乡市| 修文县| 崇文区| 洪泽县| 甘南县| 香港 | 开封市| 剑阁县| 保亭| 奇台县| 西乌珠穆沁旗| 德江县| 那坡县| 抚松县| 古交市| 白玉县| 罗定市| 房山区| 泗洪县| 武穴市| 连山| 西畴县| 临江市| 苏尼特左旗| 福泉市| 哈密市| 鲁甸县| 吉安县| 区。| 绍兴县| 西安市|