使用PHP的网站截图
现在正在研究一个通过url截图的小概念。通过引用大量使用WKHTMLOTIMAGE的网站。目前正在使用Mac。已成功安装wkhtmltoimage,还尝试了使用PHP的网站截图,php,screenshot,wkhtmltoimage,Php,Screenshot,Wkhtmltoimage,现在正在研究一个通过url截图的小概念。通过引用大量使用WKHTMLOTIMAGE的网站。目前正在使用Mac。已成功安装wkhtmltoimage,还尝试了 wkhtmltoimage www.google.com ggss.png 在候机楼。成功输出网站截图。但是当我尝试使用PHP执行上述命令时,我没有看到输出图像或任何错误。下面是我试过的代码 <?php $output = shell_exec('wkhtmltoimage http://www.bbc.com bbc.jpg');
wkhtmltoimage www.google.com ggss.png
在候机楼。成功输出网站截图。但是当我尝试使用PHP执行上述命令时,我没有看到输出图像或任何错误。下面是我试过的代码
<?php
$output = shell_exec('wkhtmltoimage http://www.bbc.com bbc.jpg');
?>
任何帮助都将不胜感激请尝试指定命令的完整路径
wkhtmltoimage
编辑
要获取命令wkhtmltoimage
完整路径,请运行以下命令:wkhtmltoimage
所以你必须像这样:
<?php
$output = shell_exec('/full_path_to_wkhtmltoimage_here/wkhtmltoimage http://www.bbc.com /full_path_to_img_here/bbc.jpg');
?>
Ok最终通过浏览器通过php执行shell命令。所以我想我可以分享一些可能对某人有用的东西。所以真正的问题是许可 所以当我在终端输出上使用whoami命令时,是macuser。 但是当我尝试在php输出中使用shell_exec执行命令时,没有人响应。这是因为apache没有权限。因此,我通过PHP执行了以下shell命令 在/etc中找到httpd.conf文件并查找 用户无人 群非群 将nobody更改为要设置为要执行的用户的用户名。对我来说,它的用户是macuser 然后执行以下命令。(以确保我在终端中以su的身份执行)
- cd/directory/of/htdocs(对于me cd/Applications/XAMPP/xamppfiles/htdocs)
- 找到-exec chown macuser:macuser{}\李>
- cd
- chown macuser htdocs
<?php
$output = shell_exec('/usr/local/bin/wkhtmltoimage http://www.google.com /Applications/XAMPP/xamppfiles/htdocs/demotasks/google.jpg');
?>
谢谢你 使用PHP拍摄屏幕截图的另一种方法是使用Google的,它不需要任何类型的身份验证。它现在是免费开放的,所以好好利用它吧 此处提供了相同的实现详细信息: 源代码
<?php
// Creating a proxy to use GET request to hit the Google Page Speed API and receive a screenshot.
// Check if the URL parameter for our proxy is set.
if (!empty($_GET['url'])) {
// Make sure the given value is a URL.
if (filter_var($_GET['url'], FILTER_VALIDATE_URL)) {
// Hit the Google PageSpeed Insights API.
// Catch: Your server needs to allow file_get_contents() to make this run. Or you need to use cURL.
$googlePagespeedResponse = file_get_contents("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?screenshot=true&url={$_GET['url']}");
// Convert the JSON response into an array.
$googlePagespeedObject = json_decode($googlePagespeedResponse, true);
// Grab the Screenshot data.
$screenshot = $googlePagespeedObject['screenshot']['data'];
// Replace Google's anamolies.
$screenshot = str_replace(array('_','-'), array('/','+'), $screenshot);
// Build the Data URI scheme and spit out an <img /> Tag.
echo "<img src=\"data:image/jpeg;base64,{$screenshot}\" alt=\"Screenshot\" />";
} else {
// If not a valid URL.
echo "Given URL is not valid.";
}
} else {
// URL not set.
echo "You need to specify the URL.";
}
?>
我的网站硬编码截图:
我建议使用API,如
例如,如果创建帐户,则可以调用API
// The parameters.
$token = 'YOUR_TOKEN';
$url = urlencode('https://github.com');
$width = 1920;
$height = 1080;
$output = 'image';
// Create the query URL.
$query = "https://screenshotapi.net/api/v1/screenshot";
$query .= "?token=$token&url=$url&width=$width&height=$height&output=$output";
// Call the API.
$image = file_get_contents($query);
// Store the screenshot image.
file_put_contents('./screenshot.png', $image);
查看以了解更多信息。查看系统错误日志以了解可能发生的情况。这不是答案,伙计,请使用注释this@Saqueib这个问题类似:。我认为问题完全可以试着举个例子并参考类似的例子answer@KarthikNk否,请尝试命令
wkhtmltoimage
的完整路径,而不是文件!您可以使用where is wkhtmltoimage
@stepozer-right找到完整路径。。在终端中使用此路径/usr/local/bin/wkhtmltoimage www.google.com/Applications/XAMPP/xampfiles/htdocs/demostasks/phpexec.png可以完美地工作,但在php中使用shell_exec的相同路径时仍然没有输出,您有一个输入错误$googlePagespeedObject['screenshot']['data']@PraveenKumarPurushothaman你好,Praveen。我已经使用你的黑客软件7个多月了。但是现在它不起作用了。它不会生成我提供的web URL的屏幕截图。@Dulithdescosta感谢您的报告。。。让我看看谷歌是否发现了我们的滥用,让我找一个替代方案。@Dulithdescosta我刚刚检查过它是否有效。你有没有超过你的限制?检查你的控制台是否有错误…@PraveenKumarPurushothaman哦,是吗?有没有办法检查它的极限?我一直在谷歌上搜索,但还是没找到。我需要以适当的结束方式注销。呵呵。非常感谢普拉文。