原文链接:http://mengkang.net/71.html
在文章入库的时候用php去执行linux命令,从而在linux命令里面调用casperjs,来完成区域截图的操作。考虑到截屏+拼图是一个较为耗时的操作,所以还是以异步的方式去执行这些操作。
下面是我在linux上的安装过程,如果没有安装git请先yum install git
安装casperjs
我是下载在了home/www/下面,参考链接:http://docs.casperjs.org/en/latest/installation.html#installing-from-git
git clone git://github.com/n1k0/casperjs.git
cd casperjs
ln -sf pwd/bin/casperjs /usr/local/bin/casperjs
安装phantomjs不建议使用源码方式安装!首先是时间长,其次是依赖qt和webkit
我yum安装报错如下:
QMAKESPEC has not been set, so configuration cannot be deduced.
Error processing project file: /mnt/hgfs/xxx/phantomjs/phantomjs.pro
因为没有安装qt才会报这个错。官方自身也说了不建议使用源码安装,而推荐他们的二进制包,下载地址:http://phantomjs.org/download.html但是官方提供的链接好像很不稳定,我弄了半天都没搞定,大家在网上搜下别人提供的下载地址。
下载后操作很简单,直接把解压好的\bin\phantomjs移动到\usr\local\bin\phantomjs就可以了。测试phantomjs --version 有结果不报错,说明安装OK
安装完毕之后中遇到了php这边了
关于casperjs命令行传参给js脚本的资料请参考http://docs.casperjs.org/en/latest/cli.html#using-the-command-line
php配合casperjs完成长微博的生成
关于php通过popen来执行linux 命令行的时候各种权限是坑,所以提前建议大家分段测试,我这个下面有调试注释代码,大家测试的时候请先打开,测试成功之后再把那段注释加上,否则就不是异步了
class article{
public function dopost(){
$articleId = 123;//文章发表成功之后返回的主键id,或者是需要修改的文章的id
$data = array('id'=>$articleId);
$generate_image_url = 'http://xxxx';//即访问下面的genrate_image方法
$this->send_request_by_fsockopen($generate_image_url,$data);
}
protected function genrate_image(){
$id = intval($_POST['id']);
putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs");//如果不加这句就会报错“Fatal: [Errno 2] No such file or directory; did you install phantomjs?”,详情参考http://mengkang.net/87.html
popen('/usr/local/bin/casperjs test.js '.$id.' 2>&1','r');
/**调试使用下面的代码,会提示权限等各种错误,例如“sh: /usr/local/bin/casperjs: Permission denied”
$handle = popen('/usr/local/bin/casperjs screenshot.js '.$id.' 2>&1','r');
$read = stream_get_contents($handle);
echo $read;**/
pclose($handle);
}
/**
* 异步请求
* 参考 http://mengkang.net/33.html
*/
protected function send_request_by_fsockopen($url,$post_data=array()){
$url_array = parse_url($url);
$hostname = $url_array['host'];
$port = isset($url_array['port'])? $url_array['port'] : 80;
$requestPath = $url_array['path'] ."?". $url_array['query'];
$fp = fsockopen($hostname, $port, $errno, $errstr, 10);
if (!$fp) {
echo "$errstr ($errno)";
return false;
}
$method = "GET";
if(!empty($post_data)){
$method = "POST";
}
$header = "$method $requestPath HTTP/1.1\r\n";
$header.="Host: $hostname\r\n";
if(!empty($post_data)){
$_post = strval(NULL);
foreach($post_data as $k => $v){
$_post[]= $k."=".urlencode($v);//必须做url转码以防模拟post提交的数据中有&符而导致post参数键值对紊乱
}
$_post = implode('&', $_post);
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";//POST数据
$header .= "Content-Length: ". strlen($_post) ."\r\n";//POST数据的长度
$header.="Connection: Close\r\n\r\n";//长连接关闭
$header .= $_post; //传递POST数据
}else{
$header.="Connection: Close\r\n\r\n";//长连接关闭
}
fwrite($fp, $header);
fclose($fp);
}
}
test.js(也就是要php调用的在linux命令行里执行的casperjs的代码)对应的脚本代码var casper = require('casper').create();
if(casper.cli.has(0)){
var id = casper.cli.get(0);
//casper.echo(id);
casper.start("http://mengkang.net/"+id+".html", function() {
this.captureSelector(id+'.png', '#blog');
});
casper.run();
}else{
casper.exit();
}
最终生成的图片的效果
需要注意,如果截图是这样:

那么就表示差字体,需要在主机上安装下字体
建议测试的时候先在命令行里测试casperjs test.js是否ok,关于这个帖子大家感觉我有说得不清楚的欢迎更贴交流。
最佳答案
