Adding example images in preview.

This commit is contained in:
sergiotarxz 2022-11-14 00:30:05 +01:00
parent 4ad79e94b4
commit 2e4ac26d82
2 changed files with 91 additions and 30 deletions

View File

@ -4,6 +4,7 @@
<title>Cartel de Cuentacuentos Infantil "Don Juan Tenorio".</title> <title>Cartel de Cuentacuentos Infantil "Don Juan Tenorio".</title>
<ogdesc>Cartel de Cuentacuentos Infantil "Don Juan Tenorio".</ogdesc> <ogdesc>Cartel de Cuentacuentos Infantil "Don Juan Tenorio".</ogdesc>
<category>index</category> <category>index</category>
<img src="/img/mairami-preview.jpg"/>
<slug>cuentacuentos-infantil-don-juan-tenorio</slug> <slug>cuentacuentos-infantil-don-juan-tenorio</slug>
<content> <content>
<p>Hoy os traemos información de una representación teatral que se ha producido este mismo día en Alcalá del Rio.</p> <p>Hoy os traemos información de una representación teatral que se ha producido este mismo día en Alcalá del Rio.</p>

View File

@ -15,12 +15,15 @@ use DateTime::Format::ISO8601;
use SVG; use SVG;
use Capture::Tiny qw/capture/; use Capture::Tiny qw/capture/;
const my $CURRENT_FILE => __FILE__;
const my $CURRENT_FILE => __FILE__; const my $ROOT_PROJECT => path($CURRENT_FILE)->parent->parent->parent;
const my $POSTS_DIR => const my $PUBLIC_DIR => $ROOT_PROJECT->child('public');
path($CURRENT_FILE)->parent->parent->parent->child('content/posts'); const my $POSTS_DIR => $ROOT_PROJECT->child('content/posts');
const my $BURGUILLOS_LOGO => const my $BURGUILLOS_LOGO => $PUBLIC_DIR->child('img/burguillos.png');
path($CURRENT_FILE)->parent->parent->parent->child('public/img/burguillos.png'); const my $SVG_WIDTH => 1200;
const my $SVG_HEIGHT => 627;
const my $SVG_EMBEDDED_IMAGE_MAX_WIDTH => 1000;
const my $SVG_EMBEDDED_IMAGE_MAX_HEIGHT => 200;
my $iso8601 = DateTime::Format::ISO8601->new; my $iso8601 = DateTime::Format::ISO8601->new;
@ -55,6 +58,11 @@ sub Retrieve {
or die "Missing slug at $post_file."; or die "Missing slug at $post_file.";
my $content = $dom->at(':root > content')->content my $content = $dom->at(':root > content')->content
or die "Missing content at $post_file."; or die "Missing content at $post_file.";
my $image_element = $dom->at(':root > img');
my $image;
if (defined $image_element) {
$image = $image_element->attr->{src};
}
my $post = { my $post = {
title => $title, title => $title,
@ -64,6 +72,7 @@ sub Retrieve {
category => $category, category => $category,
slug => $slug, slug => $slug,
content => $content, content => $content,
( ( defined $image ) ? ( image => $image ) : () ),
}; };
$cached_posts_by_category->{$category} //= []; $cached_posts_by_category->{$category} //= [];
my $category_posts = $cached_posts_by_category->{$category}; my $category_posts = $cached_posts_by_category->{$category};
@ -90,34 +99,78 @@ sub PostPreviewOg {
push @new_content, $line; push @new_content, $line;
next; next;
} }
my $last_word = ''; my $last_word = '';
while ( $line =~ /(.{1,${n_chars_per_line}})/g ) { while ( $line =~ /(.{1,${n_chars_per_line}})/g ) {
my $new_line = $last_word.$1; my $new_line = $last_word . $1;
$new_line =~ s/(\S*)$//; $new_line =~ s/(\S*)$//;
$last_word = $1; $last_word = $1;
push @new_content, $new_line; push @new_content, $new_line;
} }
if ($last_word) { if ($last_word) {
$new_content[$#new_content] .= $last_word; $new_content[$#new_content] .= $last_word;
} }
} }
my $svg = $self->_GenerateSVGPostPreview( $title, \@new_content ); my $svg =
my ($stdout, $stderr) = capture { $self->_GenerateSVGPostPreview( $title, \@new_content, $post->{image} );
open my $fh, '|-', qw{convert /dev/stdin png:fd:1}; my ( $stdout, $stderr ) = capture {
print $fh $svg; open my $fh, '|-', qw{convert /dev/stdin png:fd:1};
close $fh; print $fh $svg;
close $fh;
}; };
say STDERR $stderr; say STDERR $stderr;
return $stdout; return $stdout;
}
sub _AttachImageSVG {
my $self = shift;
my $svg = shift;
my $image = shift;
$image = $PUBLIC_DIR->child( './' . $image );
my ( $stdout, $stderr, $error ) = capture {
system qw/identify -format "%wx%h"/, $image;
};
if ($error) {
warn "$image not recognized by identify.";
return;
}
my ( $width, $height ) = $stdout =~ /^"(\d+)x(\d+)"$/;
if ( $height > $SVG_EMBEDDED_IMAGE_MAX_HEIGHT ) {
$width /= $height / $SVG_EMBEDDED_IMAGE_MAX_HEIGHT;
$width = int($width);
$height = $SVG_EMBEDDED_IMAGE_MAX_HEIGHT;
}
if ( $width > $SVG_EMBEDDED_IMAGE_MAX_WIDTH ) {
$height /= $width / $SVG_EMBEDDED_IMAGE_MAX_WIDTH;
$height = int($height);
$width = $SVG_EMBEDDED_IMAGE_MAX_WIDTH;
}
my $x = int(($SVG_EMBEDDED_IMAGE_MAX_WIDTH/2) - ($width / 2));
my $y = 90;
my ($output) = capture {
system qw/file --mime-type/, $image;
};
my ($format) = $output =~ /(\S+)$/;
$svg->image(
x => $x,
y => $y,
width => $width,
height => $height,
-href => "data:$format;base64,"
. encode_base64( $image->slurp )
);
return $y + $height + 50;
} }
sub _GenerateSVGPostPreview { sub _GenerateSVGPostPreview {
my $self = shift; my $self = shift;
my $title = shift; my $title = shift;
my $content = shift; my $content = shift;
my $image = shift;
my @content = @$content; my @content = @$content;
my $svg = SVG->new( width => 1200, height => 627 ); my $svg = SVG->new( width => $SVG_WIDTH, height => $SVG_HEIGHT );
$svg->rect( $svg->rect(
x => 0, x => 0,
y => 0, y => 0,
@ -142,21 +195,27 @@ sub _GenerateSVGPostPreview {
); );
$group->image( $group->image(
x => 10, x => 10,
y => 5, y => 5,
width => 40, width => 40,
height => 40, height => 40,
-href => 'data:image/png;base64,'.encode_base64($BURGUILLOS_LOGO->slurp) -href => 'data:image/png;base64,'
. encode_base64( $BURGUILLOS_LOGO->slurp )
); );
$group->text( $group->text(
x => 60, x => 60,
y => 40, y => 40,
style => { 'font-size' => 50, fill => '#f2eb8c' } style => { 'font-size' => 50, fill => '#f2eb8c' }
)->cdata('Burguillos.info'); )->cdata('Burguillos.info');
my $new_y;
if ( defined $image ) {
$new_y = $self->_AttachImageSVG( $group, $image );
}
$new_y //= 100;
$group->text( $group->text(
x => 10, x => 10,
y => 100, y => $new_y,
style => { 'font-size' => 50 } style => { 'font-size' => 50 }
)->cdata($title); )->cdata($title);
@ -164,11 +223,12 @@ sub _GenerateSVGPostPreview {
for my $line (@content) { for my $line (@content) {
$group->text( $group->text(
x => 10, x => 10,
y => 140 + ( 30 * $n ), y => $new_y + 40 + ( 30 * $n ),
style => { 'font-size' => 38 } style => { 'font-size' => 38 }
)->cdata($line); )->cdata($line);
$n++; $n++;
} }
path('a.svg')->spew($svg->xmlify);
return $svg->xmlify; return $svg->xmlify;
} }