Image-ButtonMaker

 view release on metacpan or  search on metacpan

META.yml  view on Meta::CPAN

# http://module-build.sourceforge.net/META-spec.html
#XXXXXXX This is a prototype!!!  It will change in the future!!! XXXXX#
name:         Image-ButtonMaker
version:      0.1.4
version_from: lib/Image/ButtonMaker.pm
installdirs:  site
requires:
    Image::Magick:                 5.5

distribution_type: module
generated_by: ExtUtils::MakeMaker version 6.17

Makefile.PL  view on Meta::CPAN

use 5.008;
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
    NAME              => 'Image::ButtonMaker',
    VERSION_FROM      => 'lib/Image/ButtonMaker.pm', # finds $VERSION
    PREREQ_PM         => {Image::Magick => 5.5}, # e.g., Module::Name => 1.1
    ABSTRACT_FROM     => 'lib/Image/ButtonMaker.pm', # retrieve abstract from module
    AUTHOR            => 'Piotr Czarny <picz@sifira.dk>',
);

README  view on Meta::CPAN


   perl Makefile.PL
   make
   make test
   make install

DEPENDENCIES

This module requires these other modules and libraries:

  Image::Magick (somewhere around version 5)

COPYRIGHT AND LICENCE

Copyright (C) 2004 by Piotr Czarny

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.2 or,
at your option, any later version of Perl 5 you may have available.


lib/Image/ButtonMaker.pm  view on Meta::CPAN


Image::ButtonMaker is a helper module for people who need to generate
vast amounts of button images. The module supports dividing your
buttons into classes, who inherit appearance from each other and
overriding needed parameters on class level or on single button level.

Image::ButtonMaker was developed as a part of a large scale web
application with multiple language support, themes and products.


Image::ButtonMaker B<requires> Image::Magick with TrueType Font
support to run.

=head1 MAIN PRINCIPLES

Each button has a set of different attributes, which determine the
appearance of the button. The button can belong to a B<class> which
acts as a template for the button. The class can be a member of a
B<class tree>, where attributes are inherited from parent class to child
class.

lib/Image/ButtonMaker/Button.pm  view on Meta::CPAN

use strict;
use utf8;
use locale;
use Image::ButtonMaker::ButtonClass;
use Image::ButtonMaker::ClassContainer;
use Image::ButtonMaker::TextContainer;
use Image::ButtonMaker::TemplateCanvas;
use Image::ButtonMaker::ColorCanvas;


use Image::Magick;

our @property_names = qw(
                         FileType

                         WidthMin
                         WidthMax
                         HeightMin
                         HeightMax

                         ArtWorkType

lib/Image/ButtonMaker/Button.pm  view on Meta::CPAN

### Private'ish methods #########################################################
sub __prepare_pix_canvas {
    my $self = shift;
    reset_error();

    my $imageName = $self->lookup_property('CanvasTemplateImg');
    return
        set_error(3000, "Could not find file for canvas: $imageName")
        unless(-f $imageName);

    my $i         = Image::Magick->new();
    my $res       = $i->Read($imageName);

    return
        set_error(3000, "Could not read image file $imageName")
        if($res);

    my $cut_left   = $self->lookup_property('CanvasCutLeft')    || 0;
    my $cut_right  = $self->lookup_property('CanvasCutRight')   || 0;
    my $cut_top    = $self->lookup_property('CanvasCutTop')     || 0;
    my $cut_bottom = $self->lookup_property('CanvasCutBottom')  || 0;

lib/Image/ButtonMaker/Button.pm  view on Meta::CPAN

sub __add_icon_to_container {
    my $self = shift;
    my $container = shift;

    my $iconFile = $self->lookup_property('IconName');
    return set_error(3000, "No Icon name specified")
        unless($iconFile);
    return set_error(3000, "Could not find icon file : $iconFile")
        unless(-f $iconFile);

    my $icon = Image::Magick->new();
    my $res = $icon->Read($iconFile);
    return set_error(3000, "Could not load icon file: $iconFile")
        if($res);

    my $vertfix = $self->lookup_property('IconVerticalAdjust');

    $res = $container->add_cell( 
                                 type  => 'icon', 
                                 image => $icon,
                                 vertfix => $vertfix,

lib/Image/ButtonMaker/Button.pm  view on Meta::CPAN

                            AfterResizeY => 30,
                            }

           ) or die "$Image::ButtonMaker::Button::errorstr";

  my $img = $but->render || die "$Image::ButtonMaker::Button::errorstr";

=head1 DESCRIPTION

This module is used to create single button objects and is capable of
rendering image objects. The module uses B<Image::Magick> as a backend
and some methods return and deal with B<Image::Magick> objects.

=head1 METHODS

=over 4

=item B<new>

This is a class method and a constuctor for Image::ButtonMaker::Button.
When a Image::ButtonMaker::Button object is created, it also creates some
lower-level objects for internal use.

lib/Image/ButtonMaker/Button.pm  view on Meta::CPAN

=item * properties

Hash reference. It is a hash of legal button-properties. Following properties are
allowed:

=over 4

=item * FileType

File type of the file generated by the B<write> method. Could be 'png' or 'gif'
or something more exotic, as long as Image::Magick supports it.

=item * WidthMin

Minimum width of the generated button.

=item * WidthMax 

Maximum width of the generated button.

=item * HeightMin

lib/Image/ButtonMaker/Button.pm  view on Meta::CPAN

=item * CanvasType

Canvas is the template of the button. At this point two types of canvas are supported
and they are 'pixmap' and 'color'. 'pixmap' type uses a template which is sliced up and
stretched for each button, while 'color' is just for plain rectangular buttons with plain
background and a maybe a border.

=item * CanvasTemplateImg

When CanvasType is set to 'pixmap' this is the path tho the template image. The 
image must be readable by Image::Magick to be usefull.

=item * CanvasCutRight

When CanvasType is set to 'pixmap' this is the number of pixels, that will be cut
off and placed in the right stretch area.

=item * CanvasCutLeft

When CanvasType is set to 'pixmap' this is the number of pixels, that will be cut
off and placed in the left stretch area.

lib/Image/ButtonMaker/Button.pm  view on Meta::CPAN

This is the text to be rendered inside a button.

=item * TextFont

A path to a .ttf (True Type Font) file containing your favorite font to be used 
inside the button.

=item * TextColor

A color of the text. Can be an RGB value like '#ff00aa' or some symbolic name
understood by Image::Magick.

=item * TextSize

Text size. F.ex '9' or maybe even '11'

=item * TextAntiAlias

Make the text nice and soft in the edges. Options are 'yes' or 'no'.

=item * TextScale

lib/Image/ButtonMaker/ColorCanvas.pm  view on Meta::CPAN

package Image::ButtonMaker::ColorCanvas;

use strict;
use utf8;
use Image::Magick;

my @default_publ = (


                    background_color => '#000000',
                    border_color     => '#000000',

                    border_width     => 0,
                    );

lib/Image/ButtonMaker/ColorCanvas.pm  view on Meta::CPAN



sub render($$) {
    my $self = shift;
    my($tot_w, $tot_h) = @_;

    my $bgcolor  = $self->{background_color};
    my $fgcolor  = $self->{border_color};
    my $stroke_w = $self->{border_width};

    my $res = Image::Magick->new(size       => $tot_w.'x'.$tot_h,
                                 matte      => 1,
                                 mattecolor => $bgcolor
                                );
    $res->Read("xc:$bgcolor");

    if($stroke_w) {
        $res->Draw(fill        => $bgcolor,
                   stroke      => $fgcolor,
                   primitive   => 'rectangle',
                   points      => '0,0 '.($tot_w-1).','.($tot_h-1),

lib/Image/ButtonMaker/TemplateCanvas.pm  view on Meta::CPAN

package Image::ButtonMaker::TemplateCanvas;

use strict;
use utf8;
use Image::Magick;

my @default_publ = (

                    template   => undef,

                    ### Cut parameters
                    cut_left   => 4,
                    cut_right  => 4,
                    cut_top    => 3,
                    cut_bottom => 3,

lib/Image/ButtonMaker/TemplateCanvas.pm  view on Meta::CPAN


    my ($left, $right, $top, $bottom) = ($self->{cut_left}, $self->{cut_right},
                                         $self->{cut_top},  $self->{cut_bottom});

    ## Calculate width & height for center tile
    my $c_width  = $tot_w - $left - $right;
    my $c_height = $tot_h - $top  - $bottom;

    ## Create the button image
    my $matte_color = $self->{matte_color};
    my $res = Image::Magick->new(size => $tot_w.'x'.$tot_h, matte => 1, mattecolor => $matte_color);
    $res->Read("xc:$matte_color");

    ## Top left corner
    $cur = $slc->[0]->Clone;
    $cur->Resize(width=> $left, height => $top);
    $res->Composite(image => $cur, compose => 'Over', geometry=>fromtop(0,0));

    ## Top middle
    $cur = $slc->[1]->Clone;
    $cur->Resize(width=> $c_width, height => $top);

lib/Image/ButtonMaker/TemplateCanvas.pm  view on Meta::CPAN


    return $res;
}


#### Privatish methods ################################
##


#### Class Method: slice_it_up($image, $left, $right, $top, $bottom)
##   return value: Image::Magick object containing nine slices
sub slice_it_up($$$$$$) {
    my $image = shift;

    my ($left, $right, $top, $bottom, $mattecolor) = @_;

    my($img_h, $img_w) = $image->Get('rows', 'columns');

    my $middle_h = $img_h - $top - $bottom;
    my $middle_w = $img_w - $left - $right;

    return 
        set_error(1000, "Canvas Cut leaves no space for artwork") 
        if($middle_h < 1 || $middle_w < 1);

    my $result = Image::Magick->new(mattecolor => $mattecolor, matte => 'True');
    my $cur;

    #### Top left slice
    $cur = $image->Clone;
    $cur->Crop(width => $left, height => $top, x => 0, y => 0);
    $result->[0] = $cur;

    #### Top middle
    $cur = $image->Clone;
    $cur->Crop(width => $middle_w, height => $top, x => $left, y => 0);

lib/Image/ButtonMaker/TextContainer.pm  view on Meta::CPAN

package Image::ButtonMaker::TextContainer;

use Image::Magick;
use strict;
use utf8;


#### Create a dummy image for QueryFontMetrics calls
my $idummy = Image::Magick->new();
$idummy->Read('xc:black');



#### Prototype for TextContainer objects
my @defaults = (
                layout      => 'horizontal',
                align       => 'baseline',
                cells       => [],
                );

lib/Image/ButtonMaker/TextContainer.pm  view on Meta::CPAN

        return undef;
    }

    my $align = $self->{align};
    my $cells = $self->{cells};

    my ($img_width, $img_height, $max_asc, $min_desc) = $self->compute_size;

    #### If no target image is passed, then generate a target
    if(!$image) {
        $image = Image::Magick->new(size  => $img_width .'x'.$img_height,
                                       matte => 1,
                                       );
        $image->Read('xc:rgba(0,0,0,0)');
    }

    my $leftpoint = $x_offset;
    my $toppoint;

    foreach my $cell (@$cells) {
        my $type = $cell->{type};



( run in 1.071 second using v1.01-cache-2.11-cpan-beeb90c9504 )