alexisf / previewgenerator
PHP 库,用于生成多种文件类型的预览图像
0.1.1
2019-05-31 20:53 UTC
Requires
- php: >=7.0
- intervention/image: ^2.4
README
此库提供类,用于生成以下文件的预览图像:
- PDF 文件 (.pdf)
- 图像文件 (.jpg, .jpeg, .png, .gif, .psd)
- 文档文件 (.doc, .docx, .odt, .xls, .xlsx, .ods, .odp, .ppt, .pptx, .csv, .odg)
- 视频文件 (.mp4, .m4v, .mov, .avi, .wmv)
- 字体文件 (.ttf, .otf)
- 文本文件 (.txt)
- 矢量文件 (.svg, .eps, .ps, .svgz, .wmf, .emf, .ai)
安装
使用 composer
此包可以使用 composer 安装
composer require alexisf/previewgenerator
需求
该包使用 (根据您要转换的文件格式)
- imagemagick (用于图像、字体和文本文件)
# on Linux / Debian : apt-get install imagemagick
# on Mac OS :
brew install imagemagick
- php-imagick (由 image.intervention.io 库要求)
# on Linux / Debian : apt-get install php-imagick php -m | grep imagick service apache2 restart
# on Mac OS :
pecl install imagick
- pdftoppm (用于 PDF 文件) 请确保已安装:
`
which pdftoppm`
。有关安装信息,请参阅: poppler-utils# on Linux / Debian : apt-get install poppler-utils
# on Mac OS :
brew install poppler
- libreoffice (用于文档文件)
# on Linux / Debian : apt-get install libreoffice
# on Mac OS :
# Install Libreoffice on your Mac, then create a symlink in PATH
cd /usr/local/bin/
ln -s /Applications/LibreOffice.app/Contents/MacOS/soffice libreoffice
- ffmpeg (用于视频文件)
# on Linux / Debian : apt-get install ffmpeg
# on Mac OS :
brew install ffmpeg
用法
请参阅 示例文件 获取更多信息及配置。
// include composer autoload
require __DIR__ .'/../vendor/autoload.php';
// relative or absolute path to files to convert
$files = [
'./files/test.pdf',
'./files/test.jpg',
'./files/test.psd',
//...
];
foreach($files as $file){
try {
// configuration array as template
$options = array(
'tempFolder' => '../temp/', // temporary folder
'exports' => [
'mini' => [ // first generated image configuration
'width' => 200,
'height' => 200,
'mode' => 'cover',// 4 resize mode : cover, contain, preserveWidth, preserveHeight
'path' => '',
],
//..... next image config
],
);
$output_path = dirname($file).'/mini/'; // folder to store the images
// create destination folder if needed
if(!file_exists($output_path)){
mkdir($output_path);
}
// complete the informations with path
$options['exports']['mini']['path'] = $output_path.basename($file).'_mini.jpg'; // path of the first image to produce
$options['exports']['preview']['path'] = $output_path.basename($file).'_preview.jpg'; // path of the second image to produce
$obj = new Alexisf\PreviewGenerator\PreviewGenerator($file, $options);
// generate the images
$obj->processing();
} catch (Exception $e) {
echo '', $file, "\n";
echo 'Exception : ', $e->getMessage(), "\n";
}
}
许可
LGPL v3。请参阅 许可文件 获取更多信息。