2021-10-06 13:03:03 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Implementation of conversion service pdf class
|
|
|
|
*
|
|
|
|
* @category DMS
|
|
|
|
* @package SeedDMS
|
|
|
|
* @license GPL 2
|
|
|
|
* @version @version@
|
|
|
|
* @author Uwe Steinmann <uwe@steinmann.cx>
|
|
|
|
* @copyright Copyright (C) 2021 Uwe Steinmann
|
|
|
|
* @version Release: @package_version@
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once("inc/inc.ClassConversionServiceBase.php");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of conversion service pdf class
|
|
|
|
*
|
|
|
|
* @category DMS
|
|
|
|
* @package SeedDMS
|
|
|
|
* @author Uwe Steinmann <uwe@steinmann.cx>
|
|
|
|
* @copyright Copyright (C) 2021 Uwe Steinmann
|
|
|
|
* @version Release: @package_version@
|
|
|
|
*/
|
|
|
|
class SeedDMS_ConversionServicePdfToImage extends SeedDMS_ConversionServiceBase {
|
|
|
|
/**
|
|
|
|
* timeout
|
|
|
|
*/
|
|
|
|
public $timeout;
|
|
|
|
|
2024-09-17 12:29:43 +00:00
|
|
|
public function __construct($from, $to) { /* {{{ */
|
2023-01-05 07:05:46 +00:00
|
|
|
parent::__construct();
|
2021-10-06 13:03:03 +00:00
|
|
|
$this->from = $from;
|
|
|
|
$this->to = $to;
|
|
|
|
$this->timeout = 5;
|
2024-09-17 12:29:43 +00:00
|
|
|
} /* }}} */
|
2021-10-06 13:03:03 +00:00
|
|
|
|
2024-09-17 12:29:43 +00:00
|
|
|
public function getInfo() { /* {{{ */
|
2021-10-08 10:13:31 +00:00
|
|
|
return "Convert with imagick php functions";
|
2024-09-17 12:29:43 +00:00
|
|
|
} /* }}} */
|
2021-10-08 10:13:31 +00:00
|
|
|
|
|
|
|
public function getAdditionalParams() { /* {{{ */
|
|
|
|
return [
|
|
|
|
['name'=>'width', 'type'=>'number', 'description'=>'Width of converted image'],
|
|
|
|
['name'=>'page', 'type'=>'number', 'description'=>'Page of Pdf document'],
|
|
|
|
];
|
|
|
|
} /* }}} */
|
|
|
|
|
2024-09-17 12:29:43 +00:00
|
|
|
public function convert($infile, $target = null, $params = array()) { /* {{{ */
|
2021-10-06 13:03:03 +00:00
|
|
|
$start = microtime(true);
|
|
|
|
$imagick = new Imagick();
|
|
|
|
/* Setting a smaller resolution will speed up the conversion
|
|
|
|
* A resolution of 72,72 will create a 596x842 image
|
2022-12-08 13:45:31 +00:00
|
|
|
* Setting it to 36,36 will create a 298x421 image which should
|
|
|
|
* be sufficient in most cases, but keep in mind that images are
|
|
|
|
* not scaled up. Hence, a width of 400px still results in a 298px
|
|
|
|
* wide image
|
2021-10-06 13:03:03 +00:00
|
|
|
*/
|
2022-12-08 13:45:31 +00:00
|
|
|
$imagick->setResolution(72,72);
|
2021-10-08 10:13:31 +00:00
|
|
|
$page = 0;
|
|
|
|
if(!empty($params['page']) && intval($params['page']) > 0)
|
|
|
|
$page = intval($params['page'])-1;
|
2021-10-19 10:18:16 +00:00
|
|
|
try {
|
|
|
|
if($imagick->readImage($infile.'['.$page.']')) {
|
|
|
|
if(!empty($params['width']))
|
|
|
|
$imagick->scaleImage(min((int) $params['width'], $imagick->getImageWidth()), 0);
|
2023-02-15 07:27:37 +00:00
|
|
|
/* Remove alpha channel and set to white */
|
2023-03-11 17:59:02 +00:00
|
|
|
$imagick->setImageBackgroundColor('white');
|
|
|
|
/* Setting the color-type and bit-depth produces much smaller images
|
|
|
|
* because the default depth appears to be 16 bit
|
|
|
|
*/
|
|
|
|
$imagick->setOption('png:color-type', 6);
|
|
|
|
$imagick->setOption('png:bit-depth', 8);
|
|
|
|
$imagick->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
|
|
|
|
$imagick->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
|
2021-10-19 10:18:16 +00:00
|
|
|
$imagick->setImageFormat('png');
|
|
|
|
$end = microtime(true);
|
|
|
|
if($this->logger) {
|
|
|
|
$this->logger->log('Conversion from '.$this->from.' to '.$this->to.' with pdf service took '.($end-$start).' sec.', PEAR_LOG_INFO);
|
|
|
|
}
|
|
|
|
if($target) {
|
|
|
|
return $imagick->writeImage($target);
|
|
|
|
} else {
|
|
|
|
return $imagick->getImageBlob();
|
|
|
|
}
|
2024-03-27 18:49:52 +00:00
|
|
|
} else {
|
|
|
|
if($this->logger) {
|
|
|
|
$this->logger->log('Conversion from '.$this->from.' to '.$this->to.' with pdf service failed: could not read input file.', PEAR_LOG_ERR);
|
|
|
|
}
|
2021-10-06 13:03:03 +00:00
|
|
|
}
|
2021-10-19 10:18:16 +00:00
|
|
|
} catch (ImagickException $e) {
|
2023-09-01 10:10:27 +00:00
|
|
|
if($this->logger) {
|
|
|
|
$this->logger->log('Conversion from '.$this->from.' to '.$this->to.' with pdf service failed: '.$e->getMessage(), PEAR_LOG_ERR);
|
|
|
|
}
|
2023-01-05 07:05:46 +00:00
|
|
|
$this->success = false;
|
2021-10-19 10:18:16 +00:00
|
|
|
return false;
|
2021-10-06 13:03:03 +00:00
|
|
|
}
|
|
|
|
return false;
|
2024-09-17 12:29:43 +00:00
|
|
|
} /* }}} */
|
2021-10-06 13:03:03 +00:00
|
|
|
}
|
|
|
|
|