<?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;

	public function __construct($from, $to) {
		$this->from = $from;
		$this->to = $to;
		$this->timeout = 5;
	}

	public function convert($infile, $target = null, $params = array()) {
		$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
		 */
		$imagick->setResolution(36,36);
		if($imagick->readImage($infile.'[0]')) {
			if(!empty($params['width']))
				$imagick->scaleImage((int) $params['width'], 0);
			$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();
			}
		}
		return false;
	}
}