| 1 | =head1 NAME | ||||||||||
| 2 | |||||||||||
| 3 | App::BlurFill - A simple command line tool to create a blurred background image | ||||||||||
| 4 | |||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||
| 6 | |||||||||||
| 7 | use App::BlurFill; | ||||||||||
| 8 | |||||||||||
| 9 | my $blur_fill = App::BlurFill->new( | ||||||||||
| 10 | file => 'path/to/image.jpg', | ||||||||||
| 11 | width => 650, | ||||||||||
| 12 | height => 350, | ||||||||||
| 13 | ); | ||||||||||
| 14 | |||||||||||
| 15 | my $output = $blur_fill->process; | ||||||||||
| 16 | print "Blurred image saved to: $output\n"; | ||||||||||
| 17 | |||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||
| 19 | |||||||||||
| 20 | App::BlurFill is a simple command line tool to create a blurred background image | ||||||||||
| 21 | from a given image. It scales the image to a specified width and height, applies | ||||||||||
| 22 | a Gaussian blur, and saves the result as a new image. | ||||||||||
| 23 | |||||||||||
| 24 | =head1 METHODS | ||||||||||
| 25 | |||||||||||
| 26 | =head2 new | ||||||||||
| 27 | |||||||||||
| 28 | my $blur_fill = App::BlurFill->new( | ||||||||||
| 29 | file => 'path/to/image.jpg', | ||||||||||
| 30 | width => 650, | ||||||||||
| 31 | height => 350, | ||||||||||
| 32 | ); | ||||||||||
| 33 | |||||||||||
| 34 | Creates a new App::BlurFill object. The following parameters are accepted: | ||||||||||
| 35 | |||||||||||
| 36 | =over 4 | ||||||||||
| 37 | |||||||||||
| 38 | =item * file | ||||||||||
| 39 | |||||||||||
| 40 | The path to the input image file. This parameter is required. | ||||||||||
| 41 | =item * width | ||||||||||
| 42 | |||||||||||
| 43 | The width of the output image. Default is 650 pixels. | ||||||||||
| 44 | |||||||||||
| 45 | =item * height | ||||||||||
| 46 | |||||||||||
| 47 | The height of the output image. Default is 350 pixels. | ||||||||||
| 48 | |||||||||||
| 49 | =item * output | ||||||||||
| 50 | |||||||||||
| 51 | The path to the output image file. If not specified, a filename will be generated | ||||||||||
| 52 | based on the input file name and saved in the same directory. The output file | ||||||||||
| 53 | will have a "_blur" suffix added to the original filename. | ||||||||||
| 54 | |||||||||||
| 55 | =back | ||||||||||
| 56 | |||||||||||
| 57 | =head2 process | ||||||||||
| 58 | |||||||||||
| 59 | my $output = $blur_fill->process; | ||||||||||
| 60 | print "Blurred image saved to: $output\n"; | ||||||||||
| 61 | |||||||||||
| 62 | Processes the input image, applies a Gaussian blur, and saves the result as a | ||||||||||
| 63 | new image. Returns the path to the output image file. | ||||||||||
| 64 | |||||||||||
| 65 | =cut | ||||||||||
| 66 | |||||||||||
| 67 | package App::BlurFill; # For MetaCPAN | ||||||||||
| 68 | |||||||||||
| 69 | 3 | ▶ | use v5.40; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 70 | 3 | ▶ | use experimental 'class'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 71 | |||||||||||
| 72 | 3 | class App::BlurFill { | |||||||||
| 73 | 3 | our $VERSION = '0.1.0'; | |||||||||
| 74 | |||||||||||
| 75 | 3 | ▶ | use Imager; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 76 | 3 | ▶ | use File::Basename 'fileparse'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 77 | 3 | ▶ | use File::Temp 'tempdir'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 78 | |||||||||||
| 79 | 3 | field $file :param; | |||||||||
| 80 | 3 | field $width :param = 650; | |||||||||
| 81 | 3 | field $height :param = 350; | |||||||||
| 82 | |||||||||||
| 83 | field $imager :param = do { | ||||||||||
| 84 | Imager->new(file => $file) | ||||||||||
| 85 | or die Imager->errstr; | ||||||||||
| 86 | 3 | }; | |||||||||
| 87 | |||||||||||
| 88 | field $output :param = do { | ||||||||||
| 89 | my ($name, $path, $ext) = fileparse($file, qr/\.[^.]*$/); | ||||||||||
| 90 | $path =~ s[/$][]; | ||||||||||
| 91 | |||||||||||
| 92 | my $dir = caller eq 'App::BlurFill::CLI' ? $path : tempdir(CLEANUP => 1); | ||||||||||
| 93 | |||||||||||
| 94 | my $filename = "${name}_blur$ext"; | ||||||||||
| 95 | |||||||||||
| 96 | "$dir/$filename"; | ||||||||||
| 97 | 3 | }; | |||||||||
| 98 | |||||||||||
| 99 | 0 | ▶ | method process { | ||||||||
|
Subroutine
process: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 100 | 0 | my $background = $imager->copy; | |||||||||
| 101 | |||||||||||
| 102 | 0 | $background = $background->scale(xpixels => $width); | |||||||||
| 103 | 0 | my $bg_height = $background->getheight; | |||||||||
| 104 | 0 | $background = $background->crop( | |||||||||
| 105 | top => ($bg_height / 2) - ($height / 2), | ||||||||||
| 106 | bottom => ($bg_height / 2) + ($height / 2), | ||||||||||
| 107 | ); | ||||||||||
| 108 | 0 | $background->filter(type => 'gaussian', stddev => 15); | |||||||||
| 109 | |||||||||||
| 110 | 0 | my $img = $imager->scale(ypixels => $height); | |||||||||
| 111 | 0 | my $img_width = $img->getwidth; | |||||||||
| 112 | |||||||||||
| 113 | 0 | $background->compose(src => $img, left => ($width / 2) - ($img_width / 2)); | |||||||||
| 114 | 0 | ▶ | $background->write(file => $output, type => 'png') or die $background->errstr; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | |||||||||||
| 116 | 0 | return $output; | |||||||||
| 117 | } | ||||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | =head1 WEB INTERFACE | ||||||||||
| 121 | |||||||||||
| 122 | The web interface previously included in this distribution was moved to the | ||||||||||
| 123 | separate L<App::BlurFill::Web> distribution in version 0.0.6. Installing | ||||||||||
| 124 | App::BlurFill now provides only the Perl API and the C<blurfill> command-line | ||||||||||
| 125 | program, without requiring Dancer2, Plack, or Starman. | ||||||||||
| 126 | |||||||||||
| 127 | =head1 SEE ALSO | ||||||||||
| 128 | |||||||||||
| 129 | L<App::BlurFill::CLI>, L<App::BlurFill::Web> | ||||||||||
| 130 | |||||||||||
| 131 | =pod | ||||||||||
| 132 | |||||||||||
| 133 | =head1 AUTHOR | ||||||||||
| 134 | |||||||||||
| 135 | Dave Cross <dave@perlhacks.com> | ||||||||||
| 136 | |||||||||||
| 137 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 138 | |||||||||||
| 139 | This software is copyright (c) 2025, Magnum Solutions Ltd. All rights reserved. | ||||||||||
| 140 | |||||||||||
| 141 | This is free software; you can redistribute it and/or modify it under the | ||||||||||
| 142 | |||||||||||
| 143 | =cut | ||||||||||