Skip to content

Commit

Permalink
handles background-color for subdefs of transparent docs (#46)
Browse files Browse the repository at this point in the history
- for transparent TIFF only !
 - for now works only with Imagick !
  • Loading branch information
jygaulier authored Sep 14, 2023
1 parent 5c7277c commit 9097b7b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 24 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"php": ">=5.3.3",
"alchemy/ghostscript": "~0.4.0",
"alchemy/mediavorus": "^0.4.4",
"imagine/imagine": "^0.9.0",
"imagine/imagine": "^0.10.0",
"monolog/monolog": "~1.0",
"neutron/temporary-filesystem": "^2.1.1",
"php-ffmpeg/php-ffmpeg": "^v0.15",
Expand Down
11 changes: 11 additions & 0 deletions src/MediaAlchemyst/Specification/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Image extends AbstractSpecification
protected $flatten = false;
protected $imageCodec = 'jpeg';
protected $page = 1;
protected $backgroundColor = null;

const RESIZE_MODE_INBOUND = ImageInterface::THUMBNAIL_INSET;
const RESIZE_MODE_INBOUND_FIXEDRATIO = 'inset_fixedRatio';
Expand Down Expand Up @@ -164,4 +165,14 @@ public function getPage()
{
return $this->page;
}

public function setBackgroundColor($color)
{
$this->backgroundColor = $color;
}

public function getBackgroundColor()
{
return $this->backgroundColor;
}
}
83 changes: 60 additions & 23 deletions src/MediaAlchemyst/Transmuter/Image2Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace MediaAlchemyst\Transmuter;

use Imagine\Exception\Exception as ImagineException;
use Imagine\Image\AbstractImage;
use Imagine\Image\AbstractImagine;
use Imagine\Image\ImageInterface;
use Imagine\Image\Palette\RGB;
use MediaVorus\Exception\ExceptionInterface as MediaVorusException;
Expand Down Expand Up @@ -66,32 +68,61 @@ public function execute(SpecificationInterface $spec, MediaInterface $source, $d
if (file_exists($tmpFile)) {
$source = $this->container['mediavorus']->guess($tmpFile);
}
} elseif ($source->getFile()->getMimeType() === 'image/tiff') {
$image = $this->container['imagine']->open($source->getFile()->getRealPath());
}
elseif ($source->getFile()->getMimeType() === 'image/tiff') {

/** @var AbstractImagine $imagine */
$imagine = $this->container['imagine'];
$image = $imagine->open($source->getFile()->getRealPath());


// find the biggest layer by w*h, keep index(es) of each layer by size
$layersBySize = array();
$maxLayerSize = 0;
$layers = $image->layers();
/** @var ImageInterface $layer */
foreach ($layers as $i=>$layer) {
$sizeKey = '_' . ($layerSize = $layer->getSize()->getWidth() * $layer->getSize()->getHeight());
if($layerSize > $maxLayerSize) {
$maxLayerSize = $layerSize;
}
if(!array_key_exists($sizeKey, $layersBySize)) {
$layersBySize[$sizeKey] = array();
}
$layersBySize[$sizeKey][] = $i;
}

$layers = array();
// remove the smallest layers, then merge the biggest ones
foreach ($layersBySize as $sizeKey => $indexes) {
if($sizeKey !== '_'.$maxLayerSize) {
foreach ($indexes as $i) {
$layers->remove($i);
}
}
}

foreach ($image->layers() as $layer) {
$tmpFile = $this->tmpFileManager->createTemporaryFile(self::TMP_FILE_SCOPE, 'imagine-tiff-layer', pathinfo($dest, PATHINFO_EXTENSION));
$layer->save($tmpFile);
try {
$image->layers()->merge();
}
catch (\Exception $e) {
// ignore: anyway we will use only the first layer
}

$layers[] = $tmpFile;
$tmpFile = null;
foreach ($image->layers() as $i=>$layer) {
// we should have only one layer after merge, but who knows ?
$tmpFile = $this->tmpFileManager->createTemporaryFile(self::TMP_FILE_SCOPE, 'imagine-tiff-layer', $source->getFile()->getExtension());
$layer->save($tmpFile);
break; // only the first
}

unset($image);

uasort($layers, function ($layer1, $layer2) {
$size1 = filesize($layer1);
$size2 = filesize($layer2);
if ($size1 == $size2) {
return 0;
}

return ($size1 > $size2) ? -1 : 1;
});

$source = $this->container['mediavorus']->guess(array_shift($layers));
} elseif (preg_match('#(application|image)\/([a-z0-9-_\.]*)photoshop([a-z0-9-_\.]*)#i', $source->getFile()->getMimeType())) {
if($tmpFile) {
$source = $this->container['mediavorus']->guess($tmpFile);
}
}
elseif (preg_match('#(application|image)\/([a-z0-9-_\.]*)photoshop([a-z0-9-_\.]*)#i', $source->getFile()->getMimeType())) {
$image = $this->container['imagine']->open($source->getFile()->getRealPath());

foreach ($image->layers() as $layer) {
Expand All @@ -106,6 +137,7 @@ public function execute(SpecificationInterface $spec, MediaInterface $source, $d
unset($image);
}

/** @var AbstractImage $image */
$image = $this->container['imagine']->open($source->getFile()->getPathname());

if ($spec->getWidth() && $spec->getHeight()) {
Expand Down Expand Up @@ -140,23 +172,28 @@ public function execute(SpecificationInterface $spec, MediaInterface $source, $d
'resolution-units' => $spec->getResolutionUnit(),
'resolution-x' => $spec->getResolutionX(),
'resolution-y' => $spec->getResolutionY(),
'background-color' => $spec->getBackgroundColor(),
);

$image->save($dest, $options);

unset($image);

$this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
} catch (MediaVorusException $e) {
}
catch (MediaVorusException $e) {
$this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
throw new RuntimeException('Unable to transmute image to image due to Mediavorus', null, $e);
} catch (PHPExiftoolException $e) {
}
catch (PHPExiftoolException $e) {
$this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
throw new RuntimeException('Unable to transmute image to image due to PHPExiftool', null, $e);
} catch (ImagineException $e) {
}
catch (ImagineException $e) {
$this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
throw new RuntimeException('Unable to transmute image to image due to Imagine', null, $e);
} catch (RuntimeException $e) {
}
catch (RuntimeException $e) {
$this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
throw $e;
}
Expand Down

0 comments on commit 9097b7b

Please sign in to comment.