org_heigl/ghostscript

Ghostscript-CLI 的 PHP 包装器

2.4.0 2024-08-01 15:51 UTC

README

CI Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Ghostscript-CLI 的 PHP 包装器

创建此库的主要原因是创建从 PDF 或 Postscript 文件生成的图像。由于这些文件可以是 RGB 或 CMYK 色域,因此库尝试处理颜色,使原始文件和图像在颜色印象上尽可能接近。这对于例如 PNG 仅支持 RGB,而 JPEG 可以是 RGB 或 CMYK 的情况尤为重要。

多页 PDF 文件将创建多个图像!为了避免覆盖您的图像文件,文件名将通过 sprintf 传递,以包含页码的文件名。唯一的参数将是页码。

为什么还需要另一个 Ghostscript 包装器?

目前有 2 个其他包装器,它们都没有满足我的要求

虽然 gravitymedia 库更专注于从输入媒体创建 PDF,但 alchemy 库不易扩展,并且无法以我需要的方式处理 Ghostscript 的参数。

因此,此库试图处理大多数重要的 Ghostscript 开关,并允许您通过实现使用 DriverInterface 的自定义驱动程序来扩展库。有关更多信息,请参阅 文档

安装

建议使用 composer 安装此包。

composer require org_heigl/ghostscript

文档

您可以在 https://heiglandreas.github.io/Org_Heigl_Ghostscript 找到此库的文档

用法

<?php
use Org_Heigl\Ghostscript\Ghostscript;

// Create the Ghostscript-Wrapper
$gs = new Ghostscript ();

// Set the output-device
$gs->setDevice('jpeg')
// Set the input file
   ->setInputFile('path/to/my/ps/or/pdf/file')
// Set the output file that will be created in the same directory as the input
   ->setOutputFile('output')
// Set the resolution to 96 pixel per inch
   ->setResolution(96)
// Set Text-antialiasing to the highest level
   ->setTextAntiAliasing(Ghostscript::ANTIALIASING_HIGH);
// Set the jpeg-quality to 100 (This is device-dependent!)
   ->getDevice()->setQuality(100);
// convert the input file to an image
if (true === $gs->render()) {
    echo 'success';
} else {
    echo 'some error occured';
}