1. Go to this page and download the library: Download daandesmedt/phpghostscript library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
daandesmedt / phpghostscript example snippets
use daandesmedt\PHPGhostscript\Devices\DeviceTypes;
use daandesmedt\PHPGhostscript\Devices\JPEG;
$ghostscript = new Ghostscript();
// JPEG
$ghostscript->setDevice(DeviceTypes::JPEG);
$ghostscript->setDevice(DeviceTypes::JPEG_GREY);
// PNG
$ghostscript->setDevice(DeviceTypes::PNG4);
$ghostscript->setDevice(DeviceTypes::PNG_256);
$ghostscript->setDevice(DeviceTypes::PNG_16M);
$ghostscript->setDevice(DeviceTypes::PNG_ALPHA);
$ghostscript->setDevice(DeviceTypes::PNG_GREY);
$ghostscript->setDevice(DeviceTypes::PNG_MONO);
$ghostscript->setDevice(DeviceTypes::PNG_MONO_D);
// PDF
$ghostscript->setDevice(DeviceTypes::PDF);
// or create device
$device = new JPEG();
// set device
$ghostscript->setDevice($device);
// Create JPEG device
$device = new JPEG();
// set JPEG device specific quality
$device->setQuality(100);
use daandesmedt\PHPGhostscript\Ghostscript;
use daandesmedt\PHPGhostscript\Devices\JPEG;
use daandesmedt\PHPGhostscript\Devices\DeviceTypes;
use daandesmedt\PHPGhostscript\Devices\JPEGGrey;
use daandesmedt\PHPGhostscript\Devices\PNG;
$ghostscript = new Ghostscript();
$ghostscript
// set Ghostscript binary path
->setBinaryPath('C:\Program Files\gs\gs9.27\bin\gswin64.exe')
// set output device
->setDevice(DeviceTypes::JPEG)
// set input & output file
->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'SinglePageHorizontal.pdf')
->setOutputFile(__DIR__ . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . 'SinglePageHorizontal.jpg');
// render
if (true === $ghostscript->render()) {
echo 'success';
} else {
echo 'error';
}
// file produce output files as : 'export-1.jpg', ... 'export-10.jpg', ...
$ghostscript->setOutputFile(__DIR__ . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . 'export-%01d.jpg');
// file produce output files as : 'export-001.jpg', ... 'export-010.jpg', ...
$ghostscript->setOutputFile(__DIR__ . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . 'export-%03d.jpg');
$ghostscript->setPages(int $startPage, int $endPage);