Plack-App-ImageMagick

 view release on metacpan or  search on metacpan

Build.PL  view on Meta::CPAN

    'File::Temp' => '0',
    'HTTP::Request::Common' => '0',
    'Module::Build' => '0.3601',
    'Plack::Builder' => '0',
    'Plack::Test' => '0',
    'Test::More' => '0',
    'Test::NoWarnings' => '0'
  },
  'configure_requires' => {
    'ExtUtils::MakeMaker' => '6.31',
    'Image::Magick' => '6.5.0',
    'Module::Build' => '0.3601',
    'Plack' => '0.9976'
  },
  'dist_abstract' => 'Create and manipulate images with Image::Magick',
  'dist_author' => [
    'Alex J. G. Burzyński <ajgb@cpan.org>'
  ],
  'dist_name' => 'Plack-App-ImageMagick',
  'dist_version' => '1.110990',
  'license' => 'perl',
  'module_name' => 'Plack::App::ImageMagick',
  'recommends' => {},
  'recursive_test_files' => 1,
  'requires' => {
    'Digest::MD5' => '0',
    'File::Spec' => '0',
    'HTTP::Date' => '0',
    'Image::Magick' => '0',
    'JSON::XS' => '0',
    'Plack::App::File' => '0',
    'Plack::Component' => '0',
    'Plack::Request' => '0',
    'Plack::Util' => '0',
    'Plack::Util::Accessor' => '0',
    'String::Bash' => '0',
    'Try::Tiny' => '0',
    'parent' => '0'
  },

Changes  view on Meta::CPAN

1.110990    Sun Apr 10 00:13:22 2011
            [INCOMPATIBLE CHANGES]
                - switch to String::Bash for handling params

            [IMPROVEMENTS]
                - better error handling and more tests
                - validate params at object creation (was in prepare_app)


1.110920    Sat Apr  2 14:05:15 2011
            Fix test failures (Image::Magick now required at configure phase)

1.110900    Thu Mar 31 22:21:39 2011
            First version.

META.json  view on Meta::CPAN

{
   "abstract" : "Create and manipulate images with Image::Magick",
   "author" : [
      "Alex J. G. Burzy\u00c5\u0084ski <ajgb@cpan.org>"
   ],
   "dynamic_config" : 0,
   "generated_by" : "Dist::Zilla version 4.200004, CPAN::Meta::Converter version 2.102400",
   "license" : [
      "perl_5"
   ],
   "meta-spec" : {
      "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",

META.json  view on Meta::CPAN

   "name" : "Plack-App-ImageMagick",
   "prereqs" : {
      "build" : {
         "requires" : {
            "Module::Build" : "0.3601"
         }
      },
      "configure" : {
         "requires" : {
            "ExtUtils::MakeMaker" : "6.31",
            "Image::Magick" : "v6.5.0",
            "Module::Build" : "0.3601",
            "Plack" : "0.9976"
         }
      },
      "runtime" : {
         "requires" : {
            "Digest::MD5" : 0,
            "File::Spec" : 0,
            "HTTP::Date" : 0,
            "Image::Magick" : 0,
            "JSON::XS" : 0,
            "Plack::App::File" : 0,
            "Plack::Component" : 0,
            "Plack::Request" : 0,
            "Plack::Util" : 0,
            "Plack::Util::Accessor" : 0,
            "String::Bash" : 0,
            "Try::Tiny" : 0,
            "parent" : 0
         }

META.yml  view on Meta::CPAN

---
abstract: 'Create and manipulate images with Image::Magick'
author:
  - 'Alex J. G. Burzyński <ajgb@cpan.org>'
build_requires:
  File::Find: 0
  File::Temp: 0
  HTTP::Request::Common: 0
  Module::Build: 0.3601
  Plack::Builder: 0
  Plack::Test: 0
  Test::More: 0
  Test::NoWarnings: 0
configure_requires:
  ExtUtils::MakeMaker: 6.31
  Image::Magick: 6.5.0
  Module::Build: 0.3601
  Plack: 0.9976
dynamic_config: 0
generated_by: 'Dist::Zilla version 4.200004, CPAN::Meta::Converter version 2.102400'
license: perl
meta-spec:
  url: http://module-build.sourceforge.net/META-spec-v1.4.html
  version: 1.4
name: Plack-App-ImageMagick
requires:
  Digest::MD5: 0
  File::Spec: 0
  HTTP::Date: 0
  Image::Magick: 0
  JSON::XS: 0
  Plack::App::File: 0
  Plack::Component: 0
  Plack::Request: 0
  Plack::Util: 0
  Plack::Util::Accessor: 0
  String::Bash: 0
  Try::Tiny: 0
  parent: 0
resources:

Makefile.PL  view on Meta::CPAN

use strict;
use warnings;



use ExtUtils::MakeMaker 6.31;



my %WriteMakefileArgs = (
  'ABSTRACT' => 'Create and manipulate images with Image::Magick',
  'AUTHOR' => 'Alex J. G. Burzyński <ajgb@cpan.org>',
  'BUILD_REQUIRES' => {
    'File::Find' => '0',
    'File::Temp' => '0',
    'HTTP::Request::Common' => '0',
    'Module::Build' => '0.3601',
    'Plack::Builder' => '0',
    'Plack::Test' => '0',
    'Test::More' => '0',
    'Test::NoWarnings' => '0'
  },
  'CONFIGURE_REQUIRES' => {
    'ExtUtils::MakeMaker' => '6.31',
    'Image::Magick' => '6.5.0',
    'Module::Build' => '0.3601',
    'Plack' => '0.9976'
  },
  'DISTNAME' => 'Plack-App-ImageMagick',
  'EXE_FILES' => [],
  'LICENSE' => 'perl',
  'NAME' => 'Plack::App::ImageMagick',
  'PREREQ_PM' => {
    'Digest::MD5' => '0',
    'File::Spec' => '0',
    'HTTP::Date' => '0',
    'Image::Magick' => '0',
    'JSON::XS' => '0',
    'Plack::App::File' => '0',
    'Plack::Component' => '0',
    'Plack::Request' => '0',
    'Plack::Util' => '0',
    'Plack::Util::Accessor' => '0',
    'String::Bash' => '0',
    'Try::Tiny' => '0',
    'parent' => '0'
  },

README  view on Meta::CPAN

NAME
    Plack::App::ImageMagick - Create and manipulate images with
    Image::Magick

VERSION
    version 1.110990

SYNOPSIS
        # app.psgi
        use Plack::App::ImageMagick;

        my $thumbnailer_app = Plack::App::ImageMagick->new(
            root => '/path/to/images',

README  view on Meta::CPAN

        use Plack::Builder;
        builder {
            # /thumbs/photo_1.jpg?width=640&height=480
            mount "/thumbs/" => $thumbnailer_app;

            # /captcha/623b1c9b03d4033635a545b54ffc4775.png
            mount "/captcha/" => $captcha_app;
        }

DESCRIPTION
    Use Image::Magick to create and manipulate images for your web
    applications.

CONFIGURATION
    You need to supply "apply" or "handler" configuration options. All other
    parameters are optional.

  apply
        my $app = Plack::App::ImageMagick->new(
            root => '/path/to/images',
            apply => [

README  view on Meta::CPAN


    Sub reference called with following parameters:

    $app
        Reference to current Plack::App::ImageMagick object.

    $env
        Reference to current $env.

    $img
        Reference to Image::Magick object created with:

            my $img = Image::Magick->new();

    *Note:* if returned @$img object contains more then one layer
    "FlattenImage()" is called before rendering.

    *Note:* "handler" and "apply" are mutually exclusive.

  pre_process
        my $app = Plack::App::ImageMagick->new(
            pre_process => sub {
                my ($app, $env, $img) = @_;

README  view on Meta::CPAN

            },
        );

    Sub reference called after "apply" (with $img processed by its methods),
    with same parameters as "handler".

    *Note:* if the @$img object contains more then one layer
    "FlattenImage()" is called before rendering.

SEE ALSO
    *   Image::Magick

    *   Plack

    *   String::Bash

AUTHOR
    Alex J. G. Burzyński <ajgb@cpan.org>

COPYRIGHT AND LICENSE
    This software is copyright (c) 2011 by Alex J. G. Burzyński

dist.ini  view on Meta::CPAN

author  = Alex J. G. Burzyński <ajgb@cpan.org>
license = Perl_5
copyright_holder = Alex J. G. Burzyński <ajgb@cpan.org>
copyright_year   = 2011

[@Filter]
-bundle=@AJGB
-remove=PortabilityTests

[Prereqs / ConfigureRequires]
Image::Magick = 6.5.0
Plack = 0.9976

[AutoPrereqs]

lib/Plack/App/ImageMagick.pm  view on Meta::CPAN

package Plack::App::ImageMagick;
BEGIN {
  $Plack::App::ImageMagick::AUTHORITY = 'cpan:AJGB';
}
BEGIN {
  $Plack::App::ImageMagick::VERSION = '1.110990';
}
# ABSTRACT: Create and manipulate images with Image::Magick

use strict;
use warnings;

use parent qw( Plack::Component );

use Image::Magick;
use Plack::App::File;
use File::Spec ();
use JSON::XS ();
use Digest::MD5 ();
use Plack::Request;
use HTTP::Date ();
use Plack::Util ();
use String::Bash ();
use Try::Tiny;

lib/Plack/App/ImageMagick.pm  view on Meta::CPAN

            $self->cache_dir,
            Digest::MD5::md5_hex( $request_uri )
        );

        if ( -r $cached_file ) {
            return $self->_create_response_from_cache( $env, $cached_file );
        }
    }

    my $handler;
    my $img = Image::Magick->new;

    if ( my $commands = $self->apply ) {

        # expand options from query string
        if ( my $with_query = $self->with_query ) {
            my $req = Plack::Request->new($env);
            my $encoded = JSON::XS::encode_json( $commands );

            my $query_params = $req->query_parameters;
            my $params = {};

lib/Plack/App/ImageMagick.pm  view on Meta::CPAN

            } catch {
                warn "Parsing query failed: $_";
                return http_response_500();
            };
        }

        # create handler from commands
        $handler = sub {
            my ($app, $env, $img) = @_;

            unless ( ref $img eq 'Image::Magick' ) {
                warn "Invalid object $img, required Image::Magick";
                return http_response_500();
            }

            # working on existing image
            if ( my $img_root = $self->root ) {
                my $path = File::Spec->catfile( $img_root, $env->{PATH_INFO} );
                my $err = $img->Read( $path );
                if ( "$err" ) {
                    warn "Read($path) failed: $err";
                    return http_response_404();

lib/Plack/App/ImageMagick.pm  view on Meta::CPAN

        };
    } else {
        $handler = $self->handler;
    };

    if ( defined $handler ) {

        if ( my $pre_process = $self->pre_process ) {
            $img = $pre_process->($self, $env, $img);

            unless ( ref $img eq 'Image::Magick' ) {
                warn "Invalid object $img, required Image::Magick";
                return http_response_500();
            }
        }

        if ( my $out = $handler->($self, $env, $img) ) {
            if ( ref $out ne 'Image::Magick' ) {
                return $out;
            }

            if ( my $post_process = $self->post_process ) {
                $out = $post_process->($self, $env, $out);

                unless ( ref $out eq 'Image::Magick' ) {
                    warn "Invalid object $out, required Image::Magick";
                    return http_response_500();
                }

            }

            # flatten image before rendering
            if ( @$out > 1 ) {
                $out = $out->FlattenImage();
                unless ( ref $out ) {
                    warn "FlattenImage() failed: $out";

lib/Plack/App/ImageMagick.pm  view on Meta::CPAN


    # we are supposed to do something
    return http_response_500();
}

sub _create_response_from_cache {
    my ($self, $env, $file_path, $content_type) = @_;

    # discover content type from cached file
    unless ( $content_type ) {
        my $img = Image::Magick->new;
        my $format = ($img->Ping( $file_path ))[3];
        $content_type = $img->MagickToMime( $format );
    };


    my $file_app = Plack::App::File->new(
        file => $file_path,
        content_type => $content_type,
    );

lib/Plack/App/ImageMagick.pm  view on Meta::CPAN


1;

__END__
=pod

=encoding utf-8

=head1 NAME

Plack::App::ImageMagick - Create and manipulate images with Image::Magick

=head1 VERSION

version 1.110990

=head1 SYNOPSIS

    # app.psgi
    use Plack::App::ImageMagick;

lib/Plack/App/ImageMagick.pm  view on Meta::CPAN

    builder {
        # /thumbs/photo_1.jpg?width=640&height=480
        mount "/thumbs/" => $thumbnailer_app;

        # /captcha/623b1c9b03d4033635a545b54ffc4775.png
        mount "/captcha/" => $captcha_app;
    }

=head1 DESCRIPTION

Use L<Image::Magick> to create and manipulate images for your web applications.

=head1 CONFIGURATION

You need to supply L<"apply"> or L<"handler"> configuration options. All other
parameters are optional.

=head2 apply

    my $app = Plack::App::ImageMagick->new(
        root => '/path/to/images',

lib/Plack/App/ImageMagick.pm  view on Meta::CPAN

=item C<$app>

Reference to current L<Plack::App::ImageMagick> object.

=item C<$env>

Reference to current C<$env>.

=item C<$img>

Reference to L<Image::Magick> object created with:

    my $img = Image::Magick->new();

=back

I<Note:> if returned C<@$img> object contains more then one layer C<FlattenImage()> is called
before rendering.

I<Note:> L<"handler"> and L<"apply"> are mutually exclusive.

=head2 pre_process

lib/Plack/App/ImageMagick.pm  view on Meta::CPAN

=for Pod::Coverage     http_response_403
    http_response_404
    http_response_500

=head1 SEE ALSO

=over 4

=item *

L<Image::Magick>

=item *

L<Plack>

=item *

L<String::Bash>

=back

t/app.t  view on Meta::CPAN



test_psgi $app, sub {
    my $cb = shift;

    # 1
    my $res_handler_grayscale_thumb = $cb->(
        GET '/images/handler_grayscale/Camelia.png'
    );

    my $ref_handler_thumb = Image::Magick->new();
    $ref_handler_thumb->Read( "t/images/Camelia-handler-thumb.png" );

    my $out_handler_thumb = Image::Magick->new( magick => 'png' );
    $out_handler_thumb->BlobToImage( $res_handler_grayscale_thumb->content );

    ok ! $ref_handler_thumb->Difference( image => $out_handler_thumb ),
        "thumbnail via handler created";

    # 2
    my $res_apply_grayscale_thumb = $cb->(
        GET '/images/apply_grayscale/Camelia.png'
    );

    my $ref_apply_thumb = Image::Magick->new();
    $ref_apply_thumb->Read( "t/images/Camelia-apply-thumb.png" );

    my $out_apply_thumb = Image::Magick->new( magick => 'png' );
    $out_apply_thumb->BlobToImage( $res_apply_grayscale_thumb->content );

    ok ! $ref_apply_thumb->Difference( image => $out_apply_thumb ),
        "thumbnail via apply created";

    # 3
    my $res_with_query = $cb->(
        GET '/images/with_query/Camelia.png?method=Scale&opacity=50&color=red'
    );

    my $ref_with_query = Image::Magick->new();
    $ref_with_query->Read( "t/images/Camelia-with_query.png" );

    my $out_with_query = Image::Magick->new( magick => 'png' );
    $out_with_query->BlobToImage( $res_with_query->content );

    ok ! $ref_with_query->Difference( image => $out_with_query ),
        "thumbnail via with_query created";

    # 4
    my $res_colors = $cb->(
        GET '/images/colors/Camelia.png'
    );

    my $ref_colors = Image::Magick->new();
    $ref_colors->Read( "t/images/Camelia-colors.png" );

    my $out_colors = Image::Magick->new( magick => 'png' );
    $out_colors->BlobToImage( $res_colors->content );

    ok ! $ref_colors->Difference( image => $out_colors ),
        "colors changed via apply";

    # 5
    my $res_fx = $cb->(
        GET '/images/fx/photo.jpg'
    );

    my $ref_fx = Image::Magick->new();
    $ref_fx->Read( "t/images/photo-fx.jpg" );

    my $out_fx = Image::Magick->new( magick => 'jpg' );
    $out_fx->BlobToImage( $res_fx->content );

    ok ! $ref_fx->Difference( image => $out_fx ),
        "Fx via apply";

    unlink("t/images/cache/48c184fdd2999a7ae824071bb128f7c8");

    # 6
    my $res_pix = $cb->(
        GET '/images/pix/1x1.png?color=red&foo='
    );

    my $ref_pix = Image::Magick->new();
    $ref_pix->Read( "t/images/1x1-red.png" );

    my $out_pix = Image::Magick->new( magick => 'png' );
    $out_pix->BlobToImage( $res_pix->content );

    ok ! $ref_pix->Difference( image => $out_pix ),
        "image via with_query created";

    unlink("t/images/cache/f9a434bb29320e1703a62a14ba378d12");

    # 7
    my $res_text = $cb->(
        GET '/images/text/message.png?text=Hi there'
    );

    my $ref_text = Image::Magick->new();
    $ref_text->Read( "t/images/text.png" );

    my $out_text = Image::Magick->new( magick => 'png' );
    $out_text->BlobToImage( $res_text->content );

    ok ! $ref_text->Difference( image => $out_text ),
        "annotation via with_query created";

    # 8
    my $res_prepost = $cb->(
        GET '/images/prepost/mirror.png'
    );

    my $ref_prepost = Image::Magick->new();
    $ref_prepost->Read( "t/images/prepost.png" );

    my $out_prepost = Image::Magick->new( magick => 'png' );
    $out_prepost->BlobToImage( $res_prepost->content );

    ok ! $ref_prepost->Difference( image => $out_prepost ),
        "pre/post processing executed";

    # 9
    {
        local $SIG{__WARN__} = sub {
            like shift @_, qr/^Undefined method at index: 0/,
                "expected warning thrown";



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