(none) imager-devel
/ help / lists / applications / search /

Re: creating a bubble badge with Imager.

From: Tony Cook (
Date: Sun 30 Oct 2011 - 23:10:21 GMT

  • Next message: Tony Cook: "Imager 0.86 released"

    On Sun, Oct 30, 2011 at 03:52:31PM +0100, mark hardwick wrote:
    > Hi
    > I'm trying to transform a normal image into a round thumbnail that looks like a bubble.
    > An example of what I'm trying to do (but using Image Magick) can be found at
    > http://www.imagemagick.org/Usage/thumbnails/#badge_mask_paint
    > (I don't care if it follows those steps exactly, I just want a result that looks something like that)
    > I'd prefer to use Imager if possible because the code that I'm working with already uses Imager and
    > changing it to use Image Magick would be a considerable job.
    > I've been trying different combinations of things with Imager for the past couple of days.
    > Most fail with an error message of "Something went wrong"
    > I wonder if anyone can save me further pain and let me know what I need to do?

    Included below.

      perl makebadge.pl thumbnail.gif badge_mask.png badge_shading.png out.png

    Requires Imager 0.63 or later.

    Instead of using rubthrough() to add the shading you could use
    compose() and adjust opacity to make the shading effect stronger or


    use strict;
    use Imager;

    my $in_name = shift;
    my $mask_name = shift;
    my $shading_name = shift;
    my $out_name = shift
      or die "Usage: $0 source mask shading output\n";

    my $in = Imager->new(file => $in_name)
      or die "Cannot load $in_name; ", Imager->errstr, "\n";

    my $mask = Imager->new(file => $mask_name)
      or die "Cannot load $mask_name; ", Imager->errstr, "\n";

    my $shading = Imager->new(file => $shading_name)
      or die "Cannot load $shading_name; ", Imager->errstr, "\n";

    my $badge = make_badge($in, $mask, $shading);

    $badge->write(file => $out_name)
      or die "Cannot save $out_name: ", $badge->errstr, "\n";

    sub make_badge {
      my ($in, $mask, $shading) = @_;

      my $mask_alpha = $mask->convert(preset => "alpha");
      my $out = Imager->new(channels => 4, xsize => $mask->getwidth,
                            ysize => $mask->getheight);
      $out->compose(src => $in,
                    mask => $mask_alpha,
                    src_left => ($in->getwidth - $mask->getwidth) / 2,
                    src_top => ($in->getheight - $mask->getheight) / 2);
      $out->rubthrough(src => $shading);

      return $out;