Advertisement
kwasinski

mkt.pl

Sep 25th, 2019
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.63 KB | None | 0 0
  1. #!perl -w
  2.  
  3. ################################################################################
  4. #
  5. #  Simple Perl script to crop and mount a HTML template to be sent via email
  6. #  
  7. #  Argument description
  8. #
  9. #  --filename       Image path to be cropped
  10. #
  11. #  --clientUrl      Complete host uri where the images would be hosted
  12. #
  13. #  --redirectUrl    Url to redirect the user when clicking the mail mkt
  14. #
  15. #  --title          Mail title
  16. #
  17. # Usage:
  18. #
  19. #  perl mkt.pl --filename=image.png --clientUrl="//client.host.url" --title="Some Title" [--redirect="//some.valid.url"]
  20. #
  21.  
  22. use 5.10.0;
  23. use strict;
  24. use utf8;
  25. use POSIX;
  26.  
  27. use Getopt::Long;
  28. use File::Slurp qw/read_file write_file/;
  29. use Data::Dumper qw/Dumper/;
  30.  
  31. my %data = (
  32.     -title        => '',
  33.     -filename     => 'mkt.png',
  34.     -clientUrl    => undef,
  35.     -redirectUrl  => undef,
  36.     -templateTr   => '
  37.            <tr>
  38.                <td>
  39.                    <img src="#image#">
  40.                </td>
  41.            </tr>',
  42. );
  43.  
  44. GetOptions(
  45.     "filename=s"    => \$data{-filename},
  46.     "redirectUrl=s"         => \$data{-redirectUrl},
  47.     "clientUrl=s"   => \$data{-clientUrl},
  48.     "title=s"   => \$data{-title},
  49. );
  50.  
  51. ##
  52. ## Check for file
  53. print 'File not found: '.$data{-filename}, "\n"  and exit  if ! -e $data{-filename};
  54.  
  55.  
  56. mountHtml();
  57.  
  58.  
  59. ##
  60. ## Mount HTML
  61. sub mountHtml {
  62.     my $template = join "", <DATA>;
  63.    
  64.     splitImage($data{-filename});
  65.  
  66.     my @images = glob "mkt/*.png";
  67.  
  68.     my $contentTrs;
  69.     foreach(@images) {
  70.         my $imgTemplate = $data{-templateTr};
  71.         my $imageUrl =  $data{-clientUrl}.'/'.$_;
  72.  
  73.         $imgTemplate =~ s{\#image\#}{$imageUrl}iem;
  74.         $contentTrs .= $imgTemplate;
  75.     }
  76.  
  77.     ##
  78.     ## rows and images
  79.     $template =~ s{\#content\#}{$contentTrs}iem;
  80.  
  81.     ##
  82.     ## title
  83.     my $title = $data{-title};
  84.     $template =~ s{\#title\#}{$title}iem;
  85.  
  86.     ##
  87.     ## redirect link
  88.     my $redirectLink = 'onclick="window.open('.$data{-redirectUrl} .')"'  if defined $data{-redirectUrl};
  89.     $template =~ s{\#link\#}{$redirectLink}iem  if defined $data{-redirectUrl};
  90.     $template =~ s{\#link\#}{}  if not defined;
  91.  
  92.     write_file('mkt.html', $template);
  93. }
  94.  
  95. ##
  96. ## Split image into equal parts
  97. sub splitImage {
  98.     my ($filename) = @_;
  99.     `mkdir mkt`  if !-d 'mkt';
  100.     `convert $filename -crop 100%x25% +repage mkt/%02d.png`;
  101. }
  102.  
  103. __DATA__
  104.  
  105. <html>
  106.     <head>
  107.         <title>#TITLE#</title>
  108.     </head>
  109.     <body #link#>
  110.         <table  border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse; border: 0px;">
  111.             #CONTENT#
  112.         </table>
  113.     </body>
  114. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement