image conversion falls back to gd library if imagick extension is not loaded

This commit is contained in:
Uwe Steinmann 2022-09-13 13:23:48 +02:00
parent e6ebabfc09
commit ad3ecdd586
3 changed files with 61 additions and 18 deletions

View File

@ -15,6 +15,7 @@
is done
- fix sorting in search result
- import of users can handle hidden and disabled flag
- image conversion falls back to gd library if imagick extension is not loaded
--------------------------------------------------------------------------------
Changes in version 5.1.26

View File

@ -28,15 +28,15 @@ class SeedDMS_ConversionServiceImageToImage extends SeedDMS_ConversionServiceBas
*/
public $timeout;
public function __construct($from, $to) {
public function __construct($from, $to) { /* {{{ */
$this->from = $from;
$this->to = $to;
$this->timeout = 5;
}
} /* }}} */
public function getInfo() {
return "Convert with imagick php functions";
}
public function getInfo() { /* {{{ */
return "Convert with imagick or gd php functions";
} /* }}} */
public function getAdditionalParams() { /* {{{ */
return [
@ -44,28 +44,68 @@ class SeedDMS_ConversionServiceImageToImage extends SeedDMS_ConversionServiceBas
];
} /* }}} */
public function convert($infile, $target = null, $params = array()) {
/**
* 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()) { /* {{{ */
$start = microtime(true);
$imagick = new Imagick();
try {
if($imagick->readImage($infile)) {
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();
}
}
} catch (ImagickException $e) {
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) {
$size = getimagesize($infile);
if(!empty($params['width']))
$imagick->scaleImage(min((int) $params['width'], $imagick->getImageWidth()), 0);
$im = imagescale($im, min((int) $params['width'], $size[0]));
$end = microtime(true);
if($this->logger) {
$this->logger->log('Conversion from '.$this->from.' to '.$this->to.' with image service took '.($end-$start).' sec.', PEAR_LOG_INFO);
$this->logger->log('Conversion from '.$this->from.' to '.$this->to.' with gd image service took '.($end-$start).' sec.', PEAR_LOG_INFO);
}
if($target) {
return $imagick->writeImage($target);
return imagepng($im, $target);
} else {
return $imagick->getImageBlob();
ob_start();
var_dump(imagepng($im));
$image = ob_get_clean();
return $image;
}
} else {
return false;
}
} catch (ImagickException $e) {
return false;
}
return false;
}
} /* }}} */
}

View File

@ -23,13 +23,15 @@ if(!empty($settings->_converters['fulltext'])) {
if(extension_loaded('imagick')) {
$conversionmgr->addService(new SeedDMS_ConversionServicePdfToImage('application/pdf', 'image/png'))->setLogger($logger);
$conversionmgr->addService(new SeedDMS_ConversionServiceImageToImage('image/tiff', 'image/png'))->setLogger($logger);
$conversionmgr->addService(new SeedDMS_ConversionServiceImageToImage('image/svg+xml', 'image/png'))->setLogger($logger);
}
if(extension_loaded('gd') || extension_loaded('imagick')) {
$conversionmgr->addService(new SeedDMS_ConversionServiceImageToImage('image/jpeg', 'image/png'))->setLogger($logger);
$conversionmgr->addService(new SeedDMS_ConversionServiceImageToImage('image/png', 'image/png'))->setLogger($logger);
$conversionmgr->addService(new SeedDMS_ConversionServiceImageToImage('image/jpg', 'image/png'))->setLogger($logger);
$conversionmgr->addService(new SeedDMS_ConversionServiceImageToImage('image/gif', 'image/png'))->setLogger($logger);
$conversionmgr->addService(new SeedDMS_ConversionServiceImageToImage('image/tiff', 'image/png'))->setLogger($logger);
$conversionmgr->addService(new SeedDMS_ConversionServiceImageToImage('image/svg+xml', 'image/png'))->setLogger($logger);
}
$conversionmgr->addService(new SeedDMS_ConversionServiceTextToText('text/plain', 'text/plain'))->setLogger($logger);