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

发布于 2024-10-09 07:44:50 字数 730 浏览 0 评论 0原文

我当然使用 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);

  }

谢谢。

I am using GD::Barcode to generate barcodes of course, but did not find 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2024-10-16 07:44:50

解决了!!!

我只需要意识到

GD::Barcode::EAN8->new($barcode_id)->plot;

返回一个 GD::Image 实例。

感谢 Sherzod B. Ruzmetov 编写了 Image::Resize。

这是新的解决方案:


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);

}

希望这对其他人有帮助。


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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文