5

将一段文字转成图片,比如常用的长微博工具 这里用 PHP 来处理。

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);

clipboard.png

wkhtmltopdf

地址 下载后有 2 个工具 wkhtmltoimage 和 wkhtmltopdf 分别是将网页转图片和pdf 的,具体使用参考

#生成图片 https://gist.github.com/vibbow/5702882
/user/bin/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

# 一个 PHP 库 https://github.com/mikehaertl/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://stackoverflow.com/questions/5663814/how-do-i-get-wkhtmltopdf-to-execute-via-php 
http://www.jianshu.com/p/4d65857ffe5e
http://yuncode.net/code/c_51dd01a4d547f26
https://github.com/niklasvh/html2canvas 
网页保存为图片及高清截图的优化https://segmentfault.com/a/1190000011425316

苏生不惑
18.9k 声望1.9k 粉丝