首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用GD::Barcode设置图像大小

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

Stack Overflow用户
提问于 2010-12-29 00:26:54
回答 1查看 1.8K关注 0票数 3

当然,我正在使用GD::Barcode来生成条形码,但没有找到设置图像宽度的方法。我该怎么做?下面是我在我的(Mojolicious)应用程序中做的事情:

代码语言:javascript
复制
  #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);

  }

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-21 01:30:34

解决了!

我只需要意识到

代码语言:javascript
复制
GD::Barcode::EAN8->new($barcode_id)->plot;

返回GD::Image实例。

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

这是新的解决方案:

代码语言:javascript
复制
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);

}

希望这对其他人有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4547517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档