当然,我正在使用 GD::Barcode 生成条形码,但没有找到设置图像宽度的方法。 我怎么做? 这是我在我的 (Mojolicious) 应用程序中所做的:
#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);
}
谢谢。
最佳答案
解决!!!
我只需要意识到这一点
GD::Barcode::EAN8->new($barcode_id)->plot;
返回一个 GD::Image 实例。
感谢编写 Image::Resize 的 Sherzod B. Ruzmetov。
这是新的解决方案:
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);
}
希望这对其他人有帮助。
关于perl - 如何使用 GD::Barcode 设置图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4547517/