php结合phantomjs实现网页全屏截图
发布时间:2017-04-24, 14:11:35 分类:PHP | 编辑 off 网址 | 辅助
正文 2923字数 463,713阅读
php代码<?php
if (isset($_GET['url']))
{
set_time_limit(0);
$url = trim($_GET['url']);
$filePath = md5($url).'.png';
if (is_file($filePath))
{
exit($filePath);
}
$command = "phantomjs snap.js {$url} {$filePath}";
@exec($command);
exit($filePath);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<title>快照生成-www.oicqzone.com</title>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
form {
padding: 20px;
}
div {
margin: 20px 0 0;
}
input {
width: 200px;
padding: 4px 2px;
}
#placeholder {
display: none;
}
</style>
</head>
<body>
<form action="" id="form">
<input type="text" id="url" />
<button type="submit">生成快照</button>
<div>
<img src="" alt="" id="placeholder" />
</div>
</form>
<script>
$(function(){
$('#form').submit(function(){
if (typeof($(this).data('generate')) !== 'undefined' && $(this).data('generate') === true)
{
alert('正在生成网站快照,请耐心等待...');
return false;
}
$(this).data('generate', true);
$('button').text('正在生成快照...').attr('disabled', true);
$.ajax({
type: 'GET',
url: '?',
data: 'url=' + $('#url').val(),
success: function(data){
$('#placeholder').attr('src', data).show();
$('#form').data('generate', false);
$('button').text('生成快照').attr('disabled', false);
}
});
return false;
});
});
</script>
</body>
</html>
Run code
Cut to clipboard
以下保存为js文件,放在phantomjs同一目录下面。
var page = require('webpage').create();
var args = require('system').args;
var url = args[1];
var filename = args[2];
page.open(url, function () {
page.render(filename);
phantom.exit();
});
Run code
Cut to clipboard
我测试了下,大部分网站都能顺利截图,效果还不错,但是有一些登录以后才能访问的页面好像截取的只能是登录页,还有一些不能截取成功,一些图片加载的好像也截取不成功。
(支付宝)给作者钱财以资鼓励 (微信)→
有过 4 条评论 »
3600*24秒(一天), 60*5 秒(5分钟)
function del_file($path = '.') { $current_dir = opendir($path); //opendir()返回一个目录句柄,失败返回false while(($file = readdir($current_dir)) !== false) { //readdir()返回打开目录句柄中的一个条目 $sub_dir = $path . DIRECTORY_SEPARATOR . $file; //构建子目录路径 if($file == '.' || $file == '..') { continue; } else if(is_dir($sub_dir)) { //如果是目录,进行递归 del_file($sub_dir); } else { //如果是文件,判断是24小时以前的文件进行删除 $files = fopen($path.'/'.$file,"r"); $f =fstat($files); fclose($files); if($f['mtime']<(time()-3600*24)){ if(@unlink($path.'/'.$file)){ echo "删除文件【".$path.'/'.$file."】成功!<br />"; }else{ echo "删除文件【".$path.'/'.$file."】失败!<br />"; } } } } } del_file('d:/www');
var page = require('webpage').create(); var args = require('system').args; var url = 'https://lizhenqiu.com/article.php?id='+args[1]+'&getimg_now=1'; var urlname=args[2]; page.viewportSize = { width: 929, height: 768 }; page.open(url, function() { page.render('tmp/lizhenqiu.com-'+urlname+'.jpg'); //jpg图片质量比png差,png格式图片大 phantom.exit(); });
<?php function del_file($path = '.') { $current_dir = opendir($path); //opendir()返回一个目录句柄,失败返回false while(($file = readdir($current_dir)) !== false) { //readdir()返回打开目录句柄中的一个条目 $sub_dir = $path . DIRECTORY_SEPARATOR . $file; //构建子目录路径 if($file == '.' || $file == '..') { continue; } else if(is_dir($sub_dir)) { //如果是目录,进行递归 del_file($sub_dir); } else { //如果是文件,判断是24小时以前的文件进行删除 $files = fopen($path.'/'.$file,"r"); $f =fstat($files); fclose($files); if($f['mtime']<(time()-60*15)){ if(@unlink($path.'/'.$file)){ //echo "删除文件【".$path.'/'.$file."】成功!<br />"; }else{ //echo "删除文件【".$path.'/'.$file."】失败!<br />"; } } } } } del_file('tmp'); $ttime=time().rand(10000,99999); $url=$_GET['get_img_url']+0; $command = "phantomjs snap.js {$url} {$ttime}"; //上面提到的js文件的路径 和执行php同目录 exec($command,$out); //$img_file = 'github.png'; //$img_info = getimagesize($img_file); //$img_src = "data:{$img_info['mime']};base64," . base64_encode(file_get_contents($img_file)); //@unlink ($img_file); echo $ttime; exit; //exit("<img src='{$img_src}' />"); ?>