2021-10-06 13:03:03 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Implementation of conversion service image 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 image class
|
|
|
|
*
|
|
|
|
* @category DMS
|
|
|
|
* @package SeedDMS
|
|
|
|
* @author Uwe Steinmann <uwe@steinmann.cx>
|
|
|
|
* @copyright Copyright (C) 2021 Uwe Steinmann
|
|
|
|
* @version Release: @package_version@
|
|
|
|
*/
|
|
|
|
class SeedDMS_ConversionServiceImageToImage extends SeedDMS_ConversionServiceBase {
|
|
|
|
/**
|
|
|
|
* timeout
|
|
|
|
*/
|
|
|
|
public $timeout;
|
|
|
|
|
2022-09-13 11:23:48 +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;
|
2022-09-13 11:23:48 +00:00
|
|
|
} /* }}} */
|
2021-10-06 13:03:03 +00:00
|
|
|
|
2022-09-13 11:23:48 +00:00
|
|
|
public function getInfo() { /* {{{ */
|
|
|
|
return "Convert with imagick or gd php functions";
|
|
|
|
} /* }}} */
|
2021-10-08 10:12:37 +00:00
|
|
|
|
|
|
|
public function getAdditionalParams() { /* {{{ */
|
|
|
|
return [
|
|
|
|
['name'=>'width', 'type'=>'number', 'description'=>'Width of converted image']
|
|
|
|
];
|
|
|
|
} /* }}} */
|
|
|
|
|
2022-09-13 11:23:48 +00:00
|
|
|
/**
|
|
|
|
* Convert a pixel image into png and scale it
|
|
|
|
*
|
|
|
|
* This method uses imagick and if not available falls back to the gd library.
|
|
|
|
*/
|
|
|
|
public function convert($infile, $target = null, $params = array()) { /* {{{ */
|
2021-10-06 13:03:03 +00:00
|
|
|
$start = microtime(true);
|
2022-09-13 11:23:48 +00:00
|
|
|
if(extension_loaded('imagick')) {
|
|
|
|
$imagick = new Imagick();
|
|
|
|
try {
|
|
|
|
if($imagick->readImage($infile)) {
|
|
|
|
if(!empty($params['width']))
|
|
|
|
$imagick->scaleImage(min((int) $params['width'], $imagick->getImageWidth()), 0);
|
|
|
|
$end = microtime(true);
|
|
|
|
if($this->logger) {
|
|
|
|
$this->logger->log('Conversion from '.$this->from.' to '.$this->to.' with imagick 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 image service failed: could not read input file.', PEAR_LOG_ERR);
|
|
|
|
}
|
|
|
|
$this->success = false;
|
|
|
|
return false;
|
2022-09-13 11:23:48 +00:00
|
|
|
}
|
|
|
|
} catch (ImagickException $e) {
|
2024-03-27 18:49:52 +00:00
|
|
|
if($this->logger) {
|
|
|
|
$this->logger->log('Conversion from '.$this->from.' to '.$this->to.' with image service failed: '.$e->getMessage(), PEAR_LOG_ERR);
|
|
|
|
}
|
|
|
|
$this->success = false;
|
2022-09-13 11:23:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif(extension_loaded('gd')) {
|
|
|
|
$im = null;
|
|
|
|
switch($this->from) {
|
|
|
|
case 'image/jpeg':
|
|
|
|
case 'image/jpg':
|
|
|
|
$im = @imagecreatefromjpeg($infile);
|
|
|
|
break;
|
|
|
|
case 'image/png':
|
|
|
|
$im = @imagecreatefrompng($infile);
|
|
|
|
break;
|
|
|
|
case 'image/gif':
|
|
|
|
$im = @imagecreatefromgif($infile);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if($im) {
|
2024-01-02 16:58:02 +00:00
|
|
|
$width = imagesx($im);
|
2021-10-19 10:18:16 +00:00
|
|
|
if(!empty($params['width']))
|
2024-01-02 16:58:02 +00:00
|
|
|
$im = imagescale($im, min((int) $params['width'], $width));
|
2021-10-19 10:18:16 +00:00
|
|
|
$end = microtime(true);
|
|
|
|
if($this->logger) {
|
2022-09-13 11:23:48 +00:00
|
|
|
$this->logger->log('Conversion from '.$this->from.' to '.$this->to.' with gd image service took '.($end-$start).' sec.', PEAR_LOG_INFO);
|
2021-10-19 10:18:16 +00:00
|
|
|
}
|
|
|
|
if($target) {
|
2022-09-13 11:23:48 +00:00
|
|
|
return imagepng($im, $target);
|
2021-10-19 10:18:16 +00:00
|
|
|
} else {
|
2022-09-13 11:23:48 +00:00
|
|
|
ob_start();
|
2024-01-02 16:58:02 +00:00
|
|
|
echo imagepng($im);
|
2022-09-13 11:23:48 +00:00
|
|
|
$image = ob_get_clean();
|
|
|
|
return $image;
|
2021-10-19 10:18:16 +00:00
|
|
|
}
|
2022-09-13 11:23:48 +00:00
|
|
|
} else {
|
2024-03-27 18:49:52 +00:00
|
|
|
if($this->logger) {
|
|
|
|
$this->logger->log('Conversion from '.$this->from.' to '.$this->to.' with image service failed: could not read input file.', PEAR_LOG_ERR);
|
|
|
|
}
|
|
|
|
$this->success = false;
|
2022-09-13 11:23:48 +00:00
|
|
|
return false;
|
2021-10-06 13:03:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2022-09-13 11:23:48 +00:00
|
|
|
} /* }}} */
|
2021-10-06 13:03:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|