概述如果常用微博的话可以发现有个长微博工具,可以将一段文字转成图片,然后发在微博上,比如锤子,长微博工具www.changweibo.com这里以PHP为例,使用PHP来对页面截图。一个demoerror_reporting(7);define('ROOT',getcwd());define('BASEDIR',parse_url($_SERVER['REQUEST_UR
如果常用微博的话可以发现有个长微博工具,可以将一段文字转成图片,然后发在微博上,比如锤子,长微博工具 www.changweibo.com
这里以 PHP 为例,使用 PHP 来对页面截图。
一个 demo
error_reporting(7);
define(\’ROOT\’, getcwd());
define(\’BASEDIR\’, parse_url($_SERVER[\’REQUEST_URI\’], PHP_URL_PATH));
if ($_POST[\’text\’]) {
$ret = array();
$text = $_POST[\’text\’];
try {
$ret[\’imgurl\’] = text2img($text, (array) $_REQUEST[\’config\’]);
}
catch (Exception $e) {
$ret[\’imgurl\’] = print_r($e, true);
}
echo str_replace(\’\\\\/\’, \’/\’, Json_encode($ret));
exit(0);
}
function text2img($text, $options = array())
{
//http://yuncode.net/code/c_51dd01a4d547f26
$rows = substr_count($text, \”\\n\”) + 1;
$Font_path = $options[\’Fontfile\’] ? $options[\’Fontfile\’] : ROOT . \’/simhei.ttf\’;//cp /c/windows/Fonts/simhei.ttf .
if (!file_exists($Font_path))
throw new Exception(\” 找不到字体文件:$Font_path \”);
$Font_size = $options[\’Fontsize\’] ? $options[\’Fontsize\’] : 12;
$padding = $options[\’padding\’] ? $options[\’padding\’] : 30;
$row_plus_height = $options[\’row_plus_height\’] ? $options[\’row_plus_height\’] : 4;
$border = 1;
$im_wIDth = 430;
$im_height = ($row_plus_height + ($Font_size * 4) / 3) * $rows + ($padding + $border) * 2;
$im = @imagecreatetruecolor($im_wIDth, $im_height);
if (!$im)
throw new Exception(\”初始化图片失败,请检查 GD 配置\”);
imagefilledrectangle($im, $border, $border, ($im_wIDth – 2 * $border), ($im_height – 2 * $border), imagecolorallocate($im, 255, 255, 255));
imagettftext($im, $Font_size, 0, ($border + $padding), (($Font_size * 4) / 3 + $border + $padding), imagecolorallocate($im, 0, 0, 0), $Font_path, $text);
$base_path = \’/files\’;
$base_filename = date(\”Y-m-d,H-i-s\”) . \’.png\’;
$short_filename = $base_path . \’/\’ . $base_filename;
$short_url = rtrim(BASEDIR, \’/\’) . $short_filename;
@mkdir(ROOT . $base_path, 0777, true);
$filename = ROOT . $short_filename;
if (!imagepng($im, $filename)) {
throw new Exception(\”创建图片时出错。\”);
}
@imagedestroy($im);
return $short_url;
}
?>
<!DOCTYPE HTML><HTML><head>
<Meta http-equiv=\”Content-Type\” content=\”text/HTML; charset=utf-8\” />
<Title>长微博工具</Title>
<script type=\”text/JavaScript\” src=\”http://lib.sinaapp.com/Js/jquery/1.6.2/jquery.min.Js\”></script>
</head>
<body>
<h1>长微博工具</h1>
<p><textarea name=\”text\” ID=\”text\” rows=\”20\” cols=\”60\”></textarea></p>
<pre ID=\”pre\”></pre>
<p><a target=\”_blank\” href=\”\” ID=\”url\”></a></p>
<p><input type=\”button\” ID=\”format\” value=\”格式化\” /><input type=\”button\” ID=\”button\” value=\”生成长微博\” /> <br/><br/><br/><br/> <a href=\”http://changweibo.debake.com/\” >长微博</a></p>
<script type=\”text/JavaScript\”>
BIG_Font_WIDTH = 44;
function f(v,w,c){
c = 0;
w = w || BIG_Font_WIDTH;
return v.replace(/[\\S\\s]/g,function(a){
if(/[\\r\\n]/.test(a)) {
c = -2;
return \”\\r\\n\”;
}
c += /[\\x00-\\xFF]/.test(a) ? 1 : 2;
if(c>=w){
c = 0;
return \”\\r\\n\”+a;
}
return a;
});
}
$(function(){
$(\”#format\”).click(function(){$(\”#text\”).val(f($(\”#text\”).val()));});
$(\”#button\”).click(function(){
if($(\”#text\”).val().length<1){
alert(\”无内容\”);
} else {
$.AJAX({
type:\”POST\”,
url:location.pathname,
data:\”text=\”+$(\”#text\”).val(),
dataType:\”Json\”,
success:function(m){
if(m.message)$(\”#pre\”).INNERHTML(m.message);
if(m.imgurl)$(\”#url\”).attr(\”href\”, m.imgurl).text(m.imgurl);
}
});
}
});
});
</script>
</body>
</HTML>
text2pic
composer require dsgygb/text2pic
touch test.PHP
<?PHP
require \’vendor/autoload.PHP\’;
$transform = new Text2pic\\transform(\’by text2pic\’);
$result = $transform->generate(\”hello world\”);
print_r($result);
#中文字体路径 /c/windows/Fonts/sihei.ttf
$transform=new Text2pic\\transform($by,$uploadsPath,$uploadsUrl,$FontPath);
wkHTMLtopdf
地址 https://wkHTMLtopdf.org/downloads.HTML 下载后有 2 个工具 wkHTMLtoimage 和 wkHTMLtopdf 分别是将网页转图片和pdf 的,具体使用参考 http://blog.csdn.net/qq_14873105/article/details/51394026
#生成图片 https://gist.github.com/vibbow/5702882
wkHTMLtoimage http://www.baIDu.com/ baIDu.jpg
#PHP 调用 使用绝对路径
$r=shell_exec(\”/user/bin/wkHTMLtoimage http://www.baIDu.com/ baIDu.jpg\”);
$r=shell_exec(\”/user/bin/wkHTMLtoimage -q {$filename}.HTML {$filename}.jpg\”);
PHPwkHTMLtopdf
composer require mikehaertl/PHPwkHTMLtopdf
require \’./vendor/autoload.PHP\’;
use mikehaertl\\wkHTMLto\\Image;
// You can pass a filename, a HTML string, an URL or an options array to the constructor
$image = new Image(\’/path/to/page.HTML\’);
$image->saveAs(\’/path/to/page.png\’);//保存
// … or send to clIEnt for inline display
$image->send();//浏览器显示
// … or send to clIEnt as file download
$image->send(\’page.png\’);//浏览器显示并下载
#另外一个库https://github.com/knplabs/snappy
use Knp\\Snappy\\pdf as newpdf;
$snappy = new newpdf(\’wkHTMLtopdf\’);
header(\’Content-Type: application/pdf\’);
header(\’Content-disposition: attachment; filename=\”file.pdf\”\’);
// echo $snappy->getoutput(\’http://www.github.com\’);
$snappy->generateFromHTML(\'<h1>Bill</h1><p>You owe me money, dude.</p>\’, \’bill-123.pdf\’);
more
小人举牌图片生成 https://github.com/jokin1999/HoldUpSign
网页保存为图片及高清截图的优化https://segmentfault.com/a/1190000011425316
https://stackoverflow.com/questions/5663814/how-do-i-get-wkHTMLtopdf-to-execute-via-PHP http://www.jianshu.com/p/4d65857ffe5e
总结
以上是内存溢出为你收集整理的PHP 将网页转图片全部内容,希望文章能够帮你解决PHP 将网页转图片所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
请登录后查看评论内容