perl - 如何使用 GD::Barcode 设置图像大小

标签 perl gd barcode

当然,我正在使用 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/

相关文章:

perl - 使用perl求中位数、众数、标准差?

perl - 在 Perl 中调试段错误有哪些好的方法或步骤?

php - 只处理单张图片需要imagedestroy()吗?

android - 如何在不使用其他应用程序的情况下在android应用程序中添加条形码扫描并支持最新的android版本?

python - 使用 xvkbd 读取条形码。如何禁用Enter键?

android - Zxing条码扫描器代码

任何不以 .js 结尾的字符串的正则表达式

perl - 为什么我的系统调用在我用 pp 包装的 Perl 程序中不起作用?

perl - 使用 Perl 和 GD 调整 PNG 大小时如何保持透明度

弯曲/扭曲图像的PHP函数