Merge pull request #3859 from alchemy-fr/PHRAS-3523_some-images-not-stamped

PHRAS-3523 merge some-images-not-stamped
This commit is contained in:
Nicolas Maillat
2021-09-09 18:36:17 +02:00
committed by GitHub

View File

@@ -22,12 +22,12 @@ use MediaVorus\Media\MediaInterface;
class recordutils_image class recordutils_image
{ {
/** /**
* @param Application $app * @param Application $app
* @param \media_subdef $subdef * @param media_subdef $subdef
* *
* @return string|null The path to the stamped file, or null if stamp is not required * @return string|null The path to the stamped file, or null if stamp is not required
*/ */
public static function stamp(Application $app, \media_subdef $subdef) public static function stamp(Application $app, media_subdef $subdef)
{ {
static $palette; static $palette;
@@ -50,19 +50,21 @@ class recordutils_image
try { try {
$alpha = 100; $alpha = 100;
$attr = explode(',', $attr); $attr = explode(',', $attr);
if(count($attr) == 4) { if (count($attr) == 4) {
// 0..127 -> 100..0 // 0..127 -> 100..0
$alpha = (int)((127 - (int)array_pop($attr)) / 1.27); $alpha = (int)((127 - (int)array_pop($attr)) / 1.27);
} }
return $palette->color($attr, $alpha); return $palette->color($attr, $alpha);
} catch (ImagineException $e) { }
catch (ImagineException $e) {
return $palette->color($ret); return $palette->color($ret);
} }
}; };
$base_id = $subdef->get_record()->getBaseId(); $base_id = $subdef->get_record()->getBaseId();
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE) { if ($subdef->get_type() !== media_subdef::TYPE_IMAGE) {
return null; return null;
} }
@@ -70,13 +72,14 @@ class recordutils_image
return null; return null;
} }
$rotation = null; $rotation = "?";
try { try {
$image = $app->getMediaFromUri($subdef->getRealPath()); $image = $app->getMediaFromUri($subdef->getRealPath());
if (MediaInterface::TYPE_IMAGE === $image->getType()) { if (MediaInterface::TYPE_IMAGE === $image->getType()) {
$rotation = $image->getOrientation(); $rotation = $image->getOrientation();
} }
} catch (\Exception $e) { }
catch (Exception $e) {
// getting orientation failed but we don't care the reason // getting orientation failed but we don't care the reason
} }
@@ -103,25 +106,38 @@ class recordutils_image
return $pathOut; return $pathOut;
} }
/** @var Imagine\Imagick\Imagine $imageine */
$imagine = $app['imagine'];
// open the document // open the document
$image_in = $app['imagine']->open($pathIn); try {
$image_in = $imagine->open($pathIn);
}
catch (Exception $e) {
return $pathIn;
}
$image_size = $image_in->getSize(); $image_size = $image_in->getSize();
$back_rotation = 0;
switch ($rotation) { switch ($rotation) {
case Image::ORIENTATION_90: case Image::ORIENTATION_90:
$image_width = $image_size->getHeight(); $image_width = $image_size->getHeight();
$image_height = $image_size->getWidth(); $image_height = $image_size->getWidth();
$image_in->rotate(90); $image_in->rotate(90);
$rotation = '90'; $back_rotation = 270;
break; break;
case Image::ORIENTATION_270: case Image::ORIENTATION_270:
$image_width = $image_size->getHeight(); $image_width = $image_size->getHeight();
$image_height = $image_size->getWidth(); $image_height = $image_size->getWidth();
$image_in->rotate(270); $image_in->rotate(270);
$back_rotation = 90;
break; break;
case Image::ORIENTATION_180: case Image::ORIENTATION_180:
$image_width = $image_size->getWidth(); $image_width = $image_size->getWidth();
$image_height = $image_size->getHeight(); $image_height = $image_size->getHeight();
$image_in->rotate(180); $image_in->rotate(180);
$back_rotation = 180;
break; break;
default: default:
$image_width = $image_size->getWidth(); $image_width = $image_size->getWidth();
@@ -132,20 +148,22 @@ class recordutils_image
// open the logo // open the logo
$logo_phywidth = $logo_phyheight = 0; // physical size $logo_phywidth = $logo_phyheight = 0; // physical size
$logo_file = $app['root.path'] . '/config/stamp/' . $base_id; $logo_file = $app['root.path'] . '/config/stamp/' . $base_id;
$logo_obj = null;
try { try {
$logo_obj = $app['imagine']->open($logo_file); $logo_obj = $imagine->open($logo_file);
$logo_size = $logo_obj->getSize(); $logo_size = $logo_obj->getSize();
$logo_phywidth = $logo_size->getWidth(); $logo_phywidth = $logo_size->getWidth();
$logo_phyheight = $logo_size->getHeight(); $logo_phyheight = $logo_size->getHeight();
} catch (ImagineException $e) { }
catch (ImagineException $e) {
// missing logo : ignore
} }
$tables = [ $tables = [
'TOP' => ['h' => 0, 'rows' => []], 'TOP' => ['h' => 0, 'rows' => []],
'TOP-OVER' => ['h' => 0, 'rows' => []], 'TOP-OVER' => ['h' => 0, 'rows' => []],
'BOTTOM' => ['h' => 0, 'rows' => []], 'BOTTOM' => ['h' => 0, 'rows' => []],
'BOTTOM-OVER' => ['h' => 0, 'rows' => []] 'BOTTOM-OVER' => ['h' => 0, 'rows' => []]
]; ];
for ($istamp = 0; $istamp < $stampNodes->length; $istamp++) { for ($istamp = 0; $istamp < $stampNodes->length; $istamp++) {
@@ -188,10 +206,11 @@ class recordutils_image
$x = $sxxml->description->{$fieldname}; $x = $sxxml->description->{$fieldname};
if (is_array($x)) { if (is_array($x)) {
foreach ($x as $v) { foreach ($x as $v) {
$fldval .= ( $fldval ? '; ' : '') . (string) $v; $fldval .= ($fldval ? '; ' : '') . (string)$v;
} }
} else { }
$fldval .= ( $fldval ? '; ' : '') . (string) $x; else {
$fldval .= ($fldval ? '; ' : '') . (string)$x;
} }
$n->parentNode->replaceChild($domprefs->createTextNode($fldval), $n); $n->parentNode->replaceChild($domprefs->createTextNode($fldval), $n);
} }
@@ -205,6 +224,7 @@ class recordutils_image
// compute logo position / size // compute logo position / size
$logo_reswidth = 0; $logo_reswidth = 0;
$logo_resheight = 0; $logo_resheight = 0;
$logo_xpos = null;
if ($logo_phywidth > 0 && $logo_phyheight > 0) { if ($logo_phywidth > 0 && $logo_phyheight > 0) {
$v = $xpprefs->query('logo', $stamp); $v = $xpprefs->query('logo', $stamp);
@@ -214,23 +234,25 @@ class recordutils_image
$logopos = @strtoupper($v->item(0)->getAttribute('position')); $logopos = @strtoupper($v->item(0)->getAttribute('position'));
if (($logowidth = trim($v->item(0)->getAttribute('width'))) != '') { if (($logowidth = trim($v->item(0)->getAttribute('width'))) != '') {
if (substr($logowidth, -1) == '%') { if (substr($logowidth, -1) == '%') {
$logo_reswidth = (int) ($logowidth * $image_width / 100); $logo_reswidth = (int)($logowidth * $image_width / 100);
} else {
$logo_reswidth = (int) $logowidth;
} }
$logo_resheight = (int) ($logo_phyheight * ($logo_reswidth / $logo_phywidth)); else {
$logo_reswidth = (int)$logowidth;
}
$logo_resheight = (int)($logo_phyheight * ($logo_reswidth / $logo_phywidth));
} }
if ($logopos == 'LEFT' || $logopos == 'RIGHT') { if ($logopos == 'LEFT' || $logopos == 'RIGHT') {
if ($logo_reswidth > $image_width / 2) { if ($logo_reswidth > $image_width / 2) {
// logo too large, resize please // logo too large, resize please
$logo_reswidth = (int) ($image_width / 2); $logo_reswidth = (int)($image_width / 2);
$logo_resheight = (int) ($logo_phyheight * ($logo_reswidth / $logo_phywidth)); $logo_resheight = (int)($logo_phyheight * ($logo_reswidth / $logo_phywidth));
} }
$text_width -= $logo_reswidth; $text_width -= $logo_reswidth;
if ($logopos == 'LEFT') { if ($logopos == 'LEFT') {
$logo_xpos = 0; $logo_xpos = 0;
} else { }
else {
$logo_xpos = ($image_width - $logo_reswidth); $logo_xpos = ($image_width - $logo_reswidth);
} }
} }
@@ -245,28 +267,30 @@ class recordutils_image
for ($i = 0; $i < $texts->length; $i++) { for ($i = 0; $i < $texts->length; $i++) {
if (($tmpfontsize = trim($texts->item($i)->getAttribute('size'))) != '') { if (($tmpfontsize = trim($texts->item($i)->getAttribute('size'))) != '') {
if (substr($tmpfontsize, -1) == '%') { if (substr($tmpfontsize, -1) == '%') {
$tmpfontsize = (int) ($tmpfontsize * $image_width / 4000); $tmpfontsize = (int)($tmpfontsize * $image_width / 4000);
} else { }
$tmpfontsize = (int) $tmpfontsize; else {
$tmpfontsize = (int)$tmpfontsize;
} }
$fontsize = $tmpfontsize; $fontsize = $tmpfontsize;
} }
if ($fontsize < 2) { if ($fontsize < 2) {
$fontsize = 2; $fontsize = 2;
} elseif ($fontsize > 300) { }
elseif ($fontsize > 300) {
$fontsize = 300; $fontsize = 300;
} }
$txtline = $texts->item($i)->nodeValue; $txtline = $texts->item($i)->nodeValue;
if ($txtline != '') { if ($txtline != '') {
$wrap = static::wrap($app['imagine'], $fontsize, 0, __DIR__ . '/arial.ttf', $txtline, $text_width); $wrap = static::wrap($imagine, $fontsize, 0, __DIR__ . '/arial.ttf', $txtline, $text_width);
$txtblock[] = [ $txtblock[] = [
'fontsize' => $fontsize, 'fontsize' => $fontsize,
'fontcolor' => $xmlToColor($texts->item($i)->getAttribute('color'), [0, 0, 0]), 'fontcolor' => $xmlToColor($texts->item($i)->getAttribute('color'), [0, 0, 0]),
'h' => $wrap['toth'], 'h' => $wrap['toth'],
'lines' => $wrap['l'] 'lines' => $wrap['l']
]; ];
$txth += $wrap['toth']; $txth += $wrap['toth'];
} }
@@ -284,7 +308,7 @@ class recordutils_image
} }
// create the block // create the block
$imfg = $app['imagine']->create(new Box($image_width, $stampheight), $stamp_background); $imfg = $imagine->create(new Box($image_width, $stampheight), $stamp_background);
// copy the logo // copy the logo
if ($logo_reswidth > 0 && $logo_resheight > 0) { if ($logo_reswidth > 0 && $logo_resheight > 0) {
@@ -293,7 +317,8 @@ class recordutils_image
$logo_obj->copy()->resize(new Box($logo_reswidth, $logo_resheight)), $logo_obj->copy()->resize(new Box($logo_reswidth, $logo_resheight)),
new Point($logo_xpos, 0) new Point($logo_xpos, 0)
); );
} else { }
else {
$imfg->paste($logo_obj, new Point($logo_xpos, 0)); $imfg->paste($logo_obj, new Point($logo_xpos, 0));
} }
} }
@@ -302,7 +327,7 @@ class recordutils_image
$draw = $imfg->draw(); $draw = $imfg->draw();
$txt_ypos = 0; $txt_ypos = 0;
foreach ($txtblock as $block) { foreach ($txtblock as $block) {
$font = $app['imagine']->font(__DIR__ . '/arial.ttf', $block['fontsize'], $block['fontcolor']); $font = $imagine->font(__DIR__ . '/arial.ttf', $block['fontsize'], $block['fontcolor']);
foreach ($block['lines'] as $line) { foreach ($block['lines'] as $line) {
if ($line['t'] != '') { if ($line['t'] != '') {
$draw->text($line['t'], $font, new Point($logo_reswidth, $txt_ypos), 0); $draw->text($line['t'], $font, new Point($logo_reswidth, $txt_ypos), 0);
@@ -323,14 +348,19 @@ class recordutils_image
$tables[$stamp_position]['h'] += $stampheight; $tables[$stamp_position]['h'] += $stampheight;
} }
unset($logo_obj);
unset($domprefs);
$newh = $tables['TOP']['h'] + $image_height + $tables['BOTTOM']['h']; $newh = $tables['TOP']['h'] + $image_height + $tables['BOTTOM']['h'];
// create the output image // create the output image
$image_out = $app['imagine']->create(new Box($image_width, $newh), $palette->color("FFFFFF", 64)); $image_out = $imagine->create(new Box($image_width, $newh), $palette->color("FFFFFF", 64));
// paste the input image into // paste the input image into
$image_out->paste($image_in, new Point(0, $tables['TOP']['h'])); $image_out->paste($image_in, new Point(0, $tables['TOP']['h']));
unset($image_in);
// fix the coordinates // fix the coordinates
foreach ($tables['TOP-OVER']['rows'] as $k => $row) { foreach ($tables['TOP-OVER']['rows'] as $k => $row) {
$tables['TOP-OVER']['rows'][$k]['y0'] += $tables['TOP']['h']; $tables['TOP-OVER']['rows'][$k]['y0'] += $tables['TOP']['h'];
@@ -347,19 +377,30 @@ class recordutils_image
foreach ($tables[$ta]['rows'] as $row) { foreach ($tables[$ta]['rows'] as $row) {
if ($row['h'] > 0) { if ($row['h'] > 0) {
$image_out->paste($row['img'], new Point($row['x0'], $row['y0'])); $image_out->paste($row['img'], new Point($row['x0'], $row['y0']));
unset($row['img']);
} }
} }
} }
// save the output // save the output
if($back_rotation != 0) {
$image_out->rotate($back_rotation);
}
$image_out->save($pathOut); $image_out->save($pathOut);
unset($image_out);
gc_collect_cycles();
if (is_file($pathOut)) { if (is_file($pathOut)) {
$writer = $app['exiftool.writer']; $writer = $app['exiftool.writer'];
// copy metadata to the stamped file if we can // copy metadata to the stamped file if we can
if(method_exists($writer, "copy")) { if (method_exists($writer, "copy")) {
$writer->copy($subdef->getRealPath(), $pathOut); $writer->copy($subdef->getRealPath(), $pathOut);
} }
return $pathOut; return $pathOut;
} }
@@ -368,12 +409,12 @@ class recordutils_image
/** /**
* *
* @param Application $app * @param Application $app
* @param \media_subdef $subdef * @param media_subdef $subdef
* *
* @return boolean|string * @return boolean|string
*/ */
public static function watermark(Application $app, \media_subdef $subdef) public static function watermark(Application $app, media_subdef $subdef)
{ {
static $palette; static $palette;
@@ -387,7 +428,7 @@ class recordutils_image
return $subdef->getRealPath(); return $subdef->getRealPath();
} }
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE) { if ($subdef->get_type() !== media_subdef::TYPE_IMAGE) {
return $subdef->getRealPath(); return $subdef->getRealPath();
} }
@@ -408,27 +449,32 @@ class recordutils_image
return $pathOut; return $pathOut;
} }
$in_image = $app['imagine']->open($pathIn); /** @var Imagine\Imagick\Imagine $imagine */
$imagine = $app['imagine'];
$in_image = $imagine->open($pathIn);
$in_size = $in_image->getSize(); $in_size = $in_image->getSize();
$in_w = $in_size->getWidth(); $in_w = $in_size->getWidth();
$in_h = $in_size->getHeight(); $in_h = $in_size->getHeight();
$wm_file = $app['root.path'] . '/config/wm/' . $base_id; $wm_file = $app['root.path'] . '/config/wm/' . $base_id;
if (file_exists($wm_file)) { if (file_exists($wm_file)) {
$wm_image = $app['imagine']->open($wm_file); $wm_image = $imagine->open($wm_file);
$wm_size = $wm_image->getSize(); $wm_size = $wm_image->getSize();
$wm_w = $wm_size->getWidth(); $wm_w = $wm_size->getWidth();
$wm_h = $wm_size->getHeight(); $wm_h = $wm_size->getHeight();
if (($wm_w / $wm_h) > ($in_w / $in_h)) { if (($wm_w / $wm_h) > ($in_w / $in_h)) {
$wm_size = $wm_size->widen($in_w); $wm_size = $wm_size->widen($in_w);
} else { }
else {
$wm_size = $wm_size->heighten($in_h); $wm_size = $wm_size->heighten($in_h);
} }
$wm_image->resize($wm_size); $wm_image->resize($wm_size);
$in_image->paste($wm_image, new Point(($in_w - $wm_size->getWidth()) >> 1, ($in_h - $wm_size->getHeight()) >> 1))->save($pathOut); $in_image->paste($wm_image, new Point(($in_w - $wm_size->getWidth()) >> 1, ($in_h - $wm_size->getHeight()) >> 1))->save($pathOut);
} else { }
else {
$collname = $subdef->get_record()->getCollection()->get_name(); $collname = $subdef->get_record()->getCollection()->get_name();
$draw = $in_image->draw(); $draw = $in_image->draw();
$black = $palette->color("000000"); $black = $palette->color("000000");
@@ -438,10 +484,10 @@ class recordutils_image
$draw->line(new Point(0, $in_h - 2), new Point($in_w - 2, 0), $black); $draw->line(new Point(0, $in_h - 2), new Point($in_w - 2, 0), $black);
$draw->line(new Point(1, $in_h - 1), new Point($in_w - 1, 1), $white); $draw->line(new Point(1, $in_h - 1), new Point($in_w - 1, 1), $white);
$fsize = max(8, (int) (max($in_w, $in_h) / 30)); $fsize = max(8, (int)(max($in_w, $in_h) / 30));
$fonts = [ $fonts = [
$app['imagine']->font(__DIR__ . '/arial.ttf', $fsize, $black), $imagine->font(__DIR__ . '/arial.ttf', $fsize, $black),
$app['imagine']->font(__DIR__ . '/arial.ttf', $fsize, $white) $imagine->font(__DIR__ . '/arial.ttf', $fsize, $white)
]; ];
$testbox = $fonts[0]->box($collname, 0); $testbox = $fonts[0]->box($collname, 0);
$tx_w = min($in_w, $testbox->getWidth()); $tx_w = min($in_w, $testbox->getWidth());
@@ -451,14 +497,14 @@ class recordutils_image
$y0 = max(1, ($in_h - $tx_h) >> 1); $y0 = max(1, ($in_h - $tx_h) >> 1);
for ($i = 0; $i <= 1; $i++) { for ($i = 0; $i <= 1; $i++) {
$x = max(1, ($in_w >> 2) - ($tx_w >> 1)); $x = max(1, ($in_w >> 2) - ($tx_w >> 1));
$draw->text($collname, $fonts[$i], new Point($x - $i, $y0 - $i), 0); $draw->text($collname, $fonts[$i], new Point($x - $i, $y0 - $i));
$x = max(1, $in_w - $x - $tx_w); $x = max(1, $in_w - $x - $tx_w);
$draw->text($collname, $fonts[$i], new Point($x - $i, $y0 - $i), 0); $draw->text($collname, $fonts[$i], new Point($x - $i, $y0 - $i));
$y = max(1, ($in_h >> 2) - ($tx_h >> 1)); $y = max(1, ($in_h >> 2) - ($tx_h >> 1));
$draw->text($collname, $fonts[$i], new Point($x0 - $i, $y - $i), 0); $draw->text($collname, $fonts[$i], new Point($x0 - $i, $y - $i));
$y = max(1, $in_h - $y - $tx_h); $y = max(1, $in_h - $y - $tx_h);
$draw->text($collname, $fonts[$i], new Point($x0 - $i, $y - $i), 0); $draw->text($collname, $fonts[$i], new Point($x0 - $i, $y - $i));
} }
} }
@@ -470,13 +516,14 @@ class recordutils_image
return false; return false;
} }
/** /**
* *
* @param int $fontSize * @param int $fontSize
* @param int $angle * @param int $angle
* @param string $fontFace * @param string $fontFace
* @param string $string * @param string $string
* @param int $width * @param int $width
* @return array * @return array
*/ */
protected static function wrap(ImagineInterface $imagine, $fontSize, $angle, $fontFace, $string, $width) protected static function wrap(ImagineInterface $imagine, $fontSize, $angle, $fontFace, $string, $width)
@@ -500,15 +547,17 @@ class recordutils_image
if ($lig == '') { if ($lig == '') {
$ret[] = ['w' => 0, 'h' => $dy, 't' => '']; $ret[] = ['w' => 0, 'h' => $dy, 't' => ''];
$toth += $dy; $toth += $dy;
} else { }
else {
$twords = []; $twords = [];
$iword = -1; $iword = -1;
$lastc = ''; $lastc = '';
$length = strlen($lig); $length = strlen($lig);
$part = 0;
for ($i = 0; $i < $length; $i++) { for ($i = 0; $i < $length; $i++) {
$c = $lig[$i]; $c = $lig[$i];
if ($iword == -1 || (ctype_space($c) && !ctype_space($lastc))) { if ($iword == -1 || (ctype_space($c) && !ctype_space($lastc))) {
$twords[++$iword] = [($part = 0) => '', 1 => '']; $twords[++$iword] = [($part = 0) => '', 1 => ''];
} }
if (!ctype_space($c) && $part == 0) { if (!ctype_space($c) && $part == 0) {
$part++; $part++;
@@ -524,10 +573,11 @@ class recordutils_image
$w = $testbox->getWidth(); $w = $testbox->getWidth();
$h = $testbox->getHeight(); $h = $testbox->getHeight();
if ($i > 0 && $testbox->getWidth() > $width) { if ($i > 0 && $testbox->getWidth() > $width) {
$ret[] = ['w' => $lastw, 'h' => $lasth, 't' => $buff]; $ret[] = ['w' => $lastw, 'h' => $lasth, 't' => $buff];
$toth += $lasth; $toth += $lasth;
$buff = $wrd[1]; $buff = $wrd[1];
} else { }
else {
$buff = $test; $buff = $test;
} }
$lastw = $w; $lastw = $w;
@@ -541,6 +591,6 @@ class recordutils_image
} }
} }
return ['toth' => $toth, 'l' => $ret, 'h' => $height, 'dy' => $dy]; return ['toth' => $toth, 'l' => $ret, 'h' => $height, 'dy' => $dy];
} }
} }