Cropping the title if too long with the maximum number of possible

chars.
This commit is contained in:
Sergiotarxz 2023-09-17 06:43:45 +02:00
parent 4a10dcb0e5
commit 75fb55ca54
2 changed files with 47 additions and 37 deletions

View File

@ -14,8 +14,7 @@
<h2>Información de contacto para preparación de pedidos.</h2> <h2>Información de contacto para preparación de pedidos.</h2>
<p>El Bar-Restaurante Durán - Centro de día, <p>El Bar-Restaurante Durán - Centro de día, ubicado en avenida de Andalucía número 4 ofrece una propuesta única con sus platos de calidad excepcional.</p>
ubicado en avenida de Andalucía número 4 ofrece una propuesta única con sus platos de calidad excepcional.</p>
<p>Os recomendamos probar los crepes de pollo, desde Burguillos.info los hemos probado y hemos quedado encantados por lo deliciosos que están.</p> <p>Os recomendamos probar los crepes de pollo, desde Burguillos.info los hemos probado y hemos quedado encantados por lo deliciosos que están.</p>

View File

@ -23,21 +23,28 @@ const my $SVG_HEIGHT => 627;
const my $SVG_EMBEDDED_IMAGE_MAX_WIDTH => 1200; const my $SVG_EMBEDDED_IMAGE_MAX_WIDTH => 1200;
const my $SVG_EMBEDDED_IMAGE_MAX_HEIGHT => 400; const my $SVG_EMBEDDED_IMAGE_MAX_HEIGHT => 400;
sub Generate($self, $title, $content, $image_file = undef, $image_bottom_preview = undef) { sub Generate (
my $dom = Mojo::DOM->new($content); $self, $title, $content,
$image_file = undef,
$image_bottom_preview = undef
)
{
my $dom = Mojo::DOM->new($content);
$content = $dom->all_text; $content = $dom->all_text;
my $svg = $self->_GenerateSVGPreview(
my $svg = $self->_DivideTextContentInLines($title, 62)->[0],
$self->_GenerateSVGPreview( $title, $self->_DivideTextContentInLines($content), $image_file, $image_bottom_preview ); $self->_DivideTextContentInLines($content),
$image_file, $image_bottom_preview
);
return $self->_SVGToPNG($svg); return $self->_SVGToPNG($svg);
} }
sub _ToPng($self, $image) { sub _ToPng ( $self, $image ) {
if ($image =~ /\.\w+$/) { if ( $image =~ /\.\w+$/ ) {
my $new_image = $image =~ s/\.\w+$/.generated.png/r; my $new_image = $image =~ s/\.\w+$/.generated.png/r;
say $new_image; say $new_image;
if (!-e $new_image) { if ( !-e $new_image ) {
system 'convert', '-background', 'none', "$image", "$new_image"; system 'convert', '-background', 'none', "$image", "$new_image";
} }
$image = $new_image; $image = $new_image;
@ -45,7 +52,7 @@ sub _ToPng($self, $image) {
return path($image); return path($image);
} }
sub _GenerateSVGPreviewHeaderBar($self, $svg, $group) { sub _GenerateSVGPreviewHeaderBar ( $self, $svg, $group ) {
$group->rect( $group->rect(
x => 0, x => 0,
y => 0, y => 0,
@ -61,10 +68,9 @@ sub _GenerateSVGPreviewHeaderBar($self, $svg, $group) {
style => { fill => '#F8F8FF' } style => { fill => '#F8F8FF' }
); );
my $burguillos_logo_png = path( $self->_ToPng($BURGUILLOS_LOGO) );
my $burguillos_logo_png = path($self->_ToPng($BURGUILLOS_LOGO));
say $burguillos_logo_png; say $burguillos_logo_png;
say ''.$burguillos_logo_png; say '' . $burguillos_logo_png;
$group->image( $group->image(
x => 10, x => 10,
y => 5, y => 5,
@ -80,7 +86,9 @@ sub _GenerateSVGPreviewHeaderBar($self, $svg, $group) {
)->cdata('Burguillos.info'); )->cdata('Burguillos.info');
} }
sub _GenerateSVGPreview($self, $title, $content, $image_file, $image_bottom_preview) { sub _GenerateSVGPreview ( $self, $title, $content, $image_file,
$image_bottom_preview )
{
my @content = @$content; my @content = @$content;
my $svg = SVG->new( width => $SVG_WIDTH, height => $SVG_HEIGHT ); my $svg = SVG->new( width => $SVG_WIDTH, height => $SVG_HEIGHT );
@ -92,12 +100,13 @@ sub _GenerateSVGPreview($self, $title, $content, $image_file, $image_bottom_prev
} }
); );
$self->_GenerateSVGPreviewHeaderBar($svg, $group); $self->_GenerateSVGPreviewHeaderBar( $svg, $group );
my $new_y; my $new_y;
if ( defined $image_file ) { if ( defined $image_file ) {
$new_y = $self->_AttachImageSVG( $svg, $group, $image_file, $image_bottom_preview ); $new_y = $self->_AttachImageSVG( $svg, $group, $image_file,
$image_bottom_preview );
} }
$new_y //= 100; $new_y //= 100;
@ -109,19 +118,19 @@ sub _GenerateSVGPreview($self, $title, $content, $image_file, $image_bottom_prev
my $n = 0; my $n = 0;
for my $line (@content) { for my $line (@content) {
next if $line =~ /^\s*$/; next if $line =~ /^\s*$/;
$group->text( $group->text(
x => 10, x => 10,
y => $new_y + 40 + ( 30 * $n ), y => $new_y + 40 + ( 30 * $n ),
style => { 'font-size' => 32 } style => { 'font-size' => 32 }
)->cdata($line); )->cdata($line);
$n++; $n++;
last if $n > 2; last if $n > 2;
} }
return $svg->xmlify; return $svg->xmlify;
} }
sub _SVGToPNG($self, $svg) { sub _SVGToPNG ( $self, $svg ) {
path('a.svg')->spew_utf8($svg); path('a.svg')->spew_utf8($svg);
my ( $stdout, $stderr ) = capture { my ( $stdout, $stderr ) = capture {
open my $fh, '|-', qw{convert /dev/stdin png:fd:1}; open my $fh, '|-', qw{convert /dev/stdin png:fd:1};
@ -133,11 +142,10 @@ sub _SVGToPNG($self, $svg) {
return $stdout; return $stdout;
} }
sub _DivideTextContentInLines($self, $content) { sub _DivideTextContentInLines ( $self, $content, $n_chars_per_line = 79 ) {
$content =~ s/(\s)\s+/$1/g; $content =~ s/(\s)\s+/$1/g;
my @content_divided_in_lines = split /\n/, $content; my @content_divided_in_lines = split /\n/, $content;
my @new_content; my @new_content;
my $n_chars_per_line = 79;
for my $line (@content_divided_in_lines) { for my $line (@content_divided_in_lines) {
if ( length($line) <= $n_chars_per_line ) { if ( length($line) <= $n_chars_per_line ) {
@ -158,9 +166,10 @@ sub _DivideTextContentInLines($self, $content) {
return \@new_content; return \@new_content;
} }
sub _AttachImageSVG($self, $svg, $group, $image_file, $image_bottom_preview) { sub _AttachImageSVG ( $self, $svg, $group, $image_file, $image_bottom_preview )
{
$image_file = $PUBLIC_DIR->child( './' . $image_file ); $image_file = $PUBLIC_DIR->child( './' . $image_file );
$image_file = path($self->_ToPng($image_file)); $image_file = path( $self->_ToPng($image_file) );
my ( $stdout, $stderr, $error ) = capture { my ( $stdout, $stderr, $error ) = capture {
system qw/identify -format "%wx%h"/, $image_file; system qw/identify -format "%wx%h"/, $image_file;
}; };
@ -169,9 +178,9 @@ sub _AttachImageSVG($self, $svg, $group, $image_file, $image_bottom_preview) {
return; return;
} }
my ( $width, $height ) = $stdout =~ /^"(\d+)x(\d+)"$/; my ( $width, $height ) = $stdout =~ /^"(\d+)x(\d+)"$/;
$height = int($height * 1200 / $width); $height = int( $height * 1200 / $width );
$width = 1200; $width = 1200;
my $height_complete_image = (1200 / $width) * $height; my $height_complete_image = ( 1200 / $width ) * $height;
if ( $height > $SVG_EMBEDDED_IMAGE_MAX_HEIGHT ) { if ( $height > $SVG_EMBEDDED_IMAGE_MAX_HEIGHT ) {
$width /= $height / $SVG_EMBEDDED_IMAGE_MAX_HEIGHT; $width /= $height / $SVG_EMBEDDED_IMAGE_MAX_HEIGHT;
@ -185,14 +194,16 @@ sub _AttachImageSVG($self, $svg, $group, $image_file, $image_bottom_preview) {
$width = $SVG_EMBEDDED_IMAGE_MAX_WIDTH; $width = $SVG_EMBEDDED_IMAGE_MAX_WIDTH;
} }
my $defs = $svg->defs(); my $defs = $svg->defs();
my $clip_path = $defs->clipPath(id => 'cut-top'); my $clip_path = $defs->clipPath( id => 'cut-top' );
$clip_path->rect(x => 0, y => 50, width => 1200, height => $height); $clip_path->rect( x => 0, y => 50, width => 1200, height => $height );
my $x = 0; my $x = 0;
my $y_image = 50 - $height_complete_image + $height; my $y_image = 50 - $height_complete_image + $height;
if (defined $image_bottom_preview && $height_complete_image > $SVG_EMBEDDED_IMAGE_MAX_HEIGHT) { if ( defined $image_bottom_preview
$y_image += $height_complete_image - $image_bottom_preview; && $height_complete_image > $SVG_EMBEDDED_IMAGE_MAX_HEIGHT )
{
$y_image += $height_complete_image - $image_bottom_preview;
} }
my $y = 50; my $y = 50;
my ($output) = capture { my ($output) = capture {
@ -205,14 +216,14 @@ sub _AttachImageSVG($self, $svg, $group, $image_file, $image_bottom_preview) {
width => $SVG_WIDTH, width => $SVG_WIDTH,
height => $height_complete_image, height => $height_complete_image,
-href => "data:$format;base64," . encode_base64( $image_file->slurp ), -href => "data:$format;base64," . encode_base64( $image_file->slurp ),
'clip-path' => 'url(#cut-top)', 'clip-path' => 'url(#cut-top)',
); );
$group->rect( $group->rect(
x => 0, x => 0,
y => $y+$height, y => $y + $height,
width => $SVG_WIDTH, width => $SVG_WIDTH,
height => $SVG_HEIGHT, height => $SVG_HEIGHT,
style => { fill => 'azure' }, style => { fill => 'azure' },
); );
return $y + $height + 50; return $y + $height + 50;
} }