How to set image size using GD::Barcode
I am using GD::Barcode to generate barcodes of course, but did not fi开发者_运维问答nd a way to set the image width. How do I do that? Here is what I am doing in my (Mojolicious) application:
#action that generates an image/png barcode which is embedded in the html
use GD::Barcode::EAN8;
use Time::Seconds
sub barcode {
my ($c) = @_;
my $barcode_id = $c->stash('barcode_id');
$c->app->log->debug('Generating barcode:' . $barcode_id);
my $img_data = GD::Barcode::EAN8->new($barcode_id)->plot->png;
$c->res->headers->content_type('image/png');
$c->res->headers->header(
'Cache-Control' => 'max-age=' . ONE_MONTH . ', must-revalidate, private');
$c->render_data($img_data);
}
Thanks.
Solved!!!
I just needed to realize that
GD::Barcode::EAN8->new($barcode_id)->plot;
returns a GD::Image instance.
Thanks to Sherzod B. Ruzmetov who wrote Image::Resize.
and here is the new solution:
use Time::Seconds
#...
#generate an image/png barcode which is embedded in the html
require Image::Resize ;
GD::Image->trueColor( 0 );#turn it off since Image::Resize turned it on
require GD::Barcode::EAN8;
sub barcode {
my ($c) = @_;
my $barcode_id = $c->stash('barcode_id');
$c->app->log->debug('Generating barcode:' . $barcode_id);
my $img = GD::Barcode::EAN8->new($barcode_id)->plot();
my $img_data = Image::Resize->new($img)->resize(100, 80,1)->png;
$c->res->headers->content_type('image/png');
$c->res->headers->header(
'Cache-Control' => 'max-age=' . ONE_MONTH . ', must-revalidate, private');
$c->render_data($img_data);
}
Hope this helps someone else.
精彩评论