snappy是一个字符串压缩工具,应用挺广泛和便利,下面这个是这个压缩组件的PHP拓展版本。我们有在项目中使用,感觉还是挺不错的一个功能。压缩率大概在50%多。
下载地址:
https://github.com/kjdev/php-ext-snappy
下面的解释来自官方github(加了一点翻译,辅助大家理解哈):
Build(编译方式)
phpize
./configure
make
make install
|
Configration(配置方式)
|
php.ini: extension=snappy.so |
Function : snappy_compress(压缩函数)
|
string snappy_compress( string $data ) parameters data: The data to compress. return values //返回被压缩的字符串,如果发生错误,则返回false The compressed string or FALSE if an error occurred. |
Function : snappy_uncompress(解压缩函数)
string snappy_uncompress( string $data ) parameters name: //传入的参数:已经被压缩过的“特殊字符” The data compressed by snappy_gzcompress(). return values //返回未压缩前的字符串,如果发生错误,则发挥false The original uncompressed data or FALSE on error. |
Example(例子)
$compressed = snappy_compress( 'Compress me' ); $uncompressed = snappy_uncompress($compressed); echo $uncompressed; |
pdf&html快照的压缩功能
Snappy is a PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. It uses the excellent webkit-based wkhtmltopdf and wkhtmltoimageavailable on OSX, linux, windows.
You will have to download wkhtmltopdf 0.12.x in order to use Snappy.
Snappy是一个PHP5功能库,允许从一个URL或HTML形成缩略图、PDF和快照。它采用基于wkhtmltopdf and wkhtmltoimage,在OSX,Linux,Windows等系统上可用。你必须下载wkhtmltopdf 0.12。x才可以使用Snappy。
https://github.com/knplabs/snappy
require_once '/path/to/snappy/src/autoload.php';
use Knp\Snappy\Pdf;
$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
// or you can do it in two steps (或者你可以使用2个步骤来完成)
$snappy = new Pdf();
$snappy->setBinary('/usr/local/bin/wkhtmltopdf');
// Display the resulting pdf in the browser(在浏览器中展示PDF文件的结果)
// by setting the Content-type header to pdf(通过设置HTTP协议的Content-type头来展示PDF)
$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
echo $snappy->getOutput('http://www.github.com');
原文地址:http://hansionxu.blog.163.com/blog/static/24169810920149309562547/