| 1 | package App::ZofCMS::Plugin::ImageResize; | ||
| 2 | |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = '1.001003'; # VERSION | ||
| 7 | |||
| 8 | 1 | ▶ | use Image::Size; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Image::Resize; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use File::Spec; | ||
| 11 | use File::Copy; | ||
| 12 | |||
| 13 | use base 'App::ZofCMS::Plugin::Base'; | ||
| 14 | |||
| 15 | sub _key { 'plug_image_resize' } | ||
| 16 | sub _defaults { | ||
| 17 | return ( | ||
| 18 | inplace => 1, | ||
| 19 | only_down => 1, | ||
| 20 | cell => 'd', | ||
| 21 | key => 'plug_image_resize', | ||
| 22 | path => 'thumbs', | ||
| 23 | # images | ||
| 24 | ); | ||
| 25 | } | ||
| 26 | sub _do { | ||
| 27 | my ( $self, $conf, $t, $q, $config ) = @_; | ||
| 28 | |||
| 29 | if ( ref $conf->{images} eq 'CODE' ) { | ||
| 30 | $conf->{images} = $conf->{images}->( $t, $q, $config ); | ||
| 31 | } | ||
| 32 | |||
| 33 | return | ||
| 34 | unless defined $conf->{images}; | ||
| 35 | |||
| 36 | my ( $cell, $key, $images ) = @$conf{qw/cell key images/}; | ||
| 37 | |||
| 38 | if ( ref $images eq 'ARRAY' and @$images and ref $images->[0] eq 'ARRAY' ) { | ||
| 39 | $t->{$cell}{$key} = []; | ||
| 40 | |||
| 41 | for ( @$images ) { | ||
| 42 | push @{ $t->{$cell}{$key} }, _resize_image($_, $t, $conf); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | elsif ( ref $images eq 'HASH' ) { | ||
| 46 | $t->{$cell}{$key} = {}; | ||
| 47 | |||
| 48 | for ( keys %$images ) { | ||
| 49 | $t->{$cell}{$key}{ $_ } = _resize_image( $images->{$_}, $t, $conf ); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | else { | ||
| 53 | $t->{$cell}{$key} = _resize_image( $images, $t, $conf ); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | sub _resize_image { | ||
| 58 | my ( $img, $t, $conf ) = @_; | ||
| 59 | |||
| 60 | my %img; | ||
| 61 | if ( ref $img eq 'ARRAY' ) { | ||
| 62 | @img{ qw/x y image inplace only_down path/ } = @$img; | ||
| 63 | } | ||
| 64 | else { | ||
| 65 | %img = %$img; | ||
| 66 | } | ||
| 67 | |||
| 68 | $img{inplace} = $conf->{inplace} | ||
| 69 | unless defined $img{inplace}; | ||
| 70 | |||
| 71 | $img{path} = $conf->{path} | ||
| 72 | unless defined $img{path}; | ||
| 73 | |||
| 74 | $img{only_down} = $conf->{only_down} | ||
| 75 | unless defined $img{only_down}; | ||
| 76 | |||
| 77 | set_error($t, "Could copy the image $!") and return | ||
| 78 | unless defined $img{image} | ||
| 79 | and length $img{image} | ||
| 80 | and -e $img{image}; | ||
| 81 | |||
| 82 | if ( $img{only_down} ) { | ||
| 83 | my ( $x, $y ) = imgsize($img{image}); | ||
| 84 | |||
| 85 | if ( $x > $img{x} or $y > $img{y} ) { | ||
| 86 | calc_new_dimensions( $x, $y, \%img ); | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | @img{ qw/x y/ } = ( $x, $y ); | ||
| 90 | $img{no_resize} = 1; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | unless ( $img{inplace} ) { | ||
| 95 | my $file = ( File::Spec->splitpath($img{image}) )[-1]; | ||
| 96 | my $resize_file = File::Spec->catfile( $conf->{path}, $file ); | ||
| 97 | copy $img{image}, $resize_file | ||
| 98 | or set_error($t, "Could copy the image $!") | ||
| 99 | and return; | ||
| 100 | |||
| 101 | $img{image} = $resize_file; | ||
| 102 | } | ||
| 103 | |||
| 104 | unless ( $img{no_resize} ) { | ||
| 105 | my $gd = Image::Resize->new( $img{image} )->resize( @img{ qw/x y/ } ); | ||
| 106 | |||
| 107 | open my $fh, '>', $img{image} | ||
| 108 | or set_error($t, "Could not open image for writing $!") | ||
| 109 | and return; | ||
| 110 | |||
| 111 | print $fh $gd->jpeg; | ||
| 112 | close $fh; | ||
| 113 | |||
| 114 | @img{ qw/x y/ } = imgsize( $img{image} ); | ||
| 115 | } | ||
| 116 | |||
| 117 | return \%img; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub calc_new_dimensions { | ||
| 121 | my ( $x, $y, $img ) = @_; | ||
| 122 | |||
| 123 | if ( $x > $img->{x} and $y > $img->{y} ) { | ||
| 124 | if ( $x - $img->{x} > $y - $img->{y} ) { | ||
| 125 | $img->{y} = int($y * $img->{x} / $x); | ||
| 126 | } | ||
| 127 | else { | ||
| 128 | $img->{x} = int($x * $img->{y} / $y); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | elsif ( $x > $img->{x} ) { | ||
| 132 | $img->{y} = int($y * $img->{x} / $x); | ||
| 133 | } | ||
| 134 | else { | ||
| 135 | $img->{x} = int($x * $img->{y} / $y); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | sub set_error { | ||
| 140 | my ( $t, $err ) = @_; | ||
| 141 | $t->{t}{plug_image_resize_error} = $err; | ||
| 142 | return 1; | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | 1; | ||
| 147 | __END__ | ||