1 |
8 |
ahitrov@rambler.ru |
<%args> |
2 |
|
|
|
3 |
|
|
$SETS => undef |
4 |
|
|
$field => undef |
5 |
|
|
$default => undef |
6 |
|
|
$object => undef |
7 |
|
|
$prop => undef |
8 |
|
|
|
9 |
|
|
</%args> |
10 |
|
|
|
11 |
|
|
<%init> |
12 |
|
|
use IO::File; |
13 |
|
|
use Data::Dumper; |
14 |
|
|
|
15 |
|
|
return undef unless defined $SETS and defined $field; |
16 |
|
|
|
17 |
|
|
# -------------------------------------------------------------------------------------- |
18 |
|
|
# Добавление одной картинки |
19 |
|
|
|
20 |
|
|
my $IMAGE = $default; |
21 |
|
|
|
22 |
|
|
if ($SETS->{$field}) { |
23 |
|
|
|
24 |
47 |
ahitrov@rambler.ru |
my @preview = exists $prop->{'preview'} && ref $prop->{'preview'} eq 'ARRAY' ? @{$prop->{'preview'}} : exists $prop->{'preview'} && $prop->{'preview'} ? ($prop->{'preview'}) : (); |
25 |
|
|
my @crops = exists $prop->{'crop'} && ref $prop->{'crop'} eq 'ARRAY' ? @{$prop->{'crop'}} : exists $prop->{'crop'} && $prop->{'crop'} ? ($prop->{'crop'}) : (); |
26 |
|
|
my @shrinks = exists $prop->{'shrink'} && ref $prop->{'shrink'} eq 'ARRAY' ? @{$prop->{'shrink'}} : exists $prop->{'shrink'} && $prop->{'shrink'} ? ($prop->{'shrink'}) : (); |
27 |
8 |
ahitrov@rambler.ru |
|
28 |
47 |
ahitrov@rambler.ru |
if (ref $IMAGE eq 'HASH' && exists $IMAGE->{mini} && ref $IMAGE->{mini} eq 'HASH') { |
29 |
|
|
foreach my $val ( values %{$IMAGE->{mini}} ) { |
30 |
|
|
if ( ref $val && exists $val->{filename} && $val->{filename} ) { |
31 |
|
|
Contenido::File::remove($val->{'filename'}); |
32 |
|
|
} |
33 |
|
|
} |
34 |
8 |
ahitrov@rambler.ru |
} |
35 |
|
|
|
36 |
|
|
my $filename = '/images/'.$object->get_file_name() || return; |
37 |
|
|
my $filename_tmp = $state->{'tmp_dir'}.'/'.join('_', split('/', $filename)); |
38 |
|
|
|
39 |
|
|
my $upload = $r->upload($field); |
40 |
|
|
if ($upload) { |
41 |
|
|
my $extension = $upload->filename() =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin'; |
42 |
|
|
my $fh = $upload->fh(); |
43 |
|
|
my $size = (stat $fh)[7]; |
44 |
|
|
|
45 |
47 |
ahitrov@rambler.ru |
my $fh_tmp = IO::File->new('>'.$filename_tmp.'.'.$extension) || die 'Can\'t open temporary file'; |
46 |
8 |
ahitrov@rambler.ru |
|
47 |
47 |
ahitrov@rambler.ru |
my $buffer; |
48 |
8 |
ahitrov@rambler.ru |
|
49 |
47 |
ahitrov@rambler.ru |
sysread $fh, $buffer, $size; |
50 |
|
|
syswrite $fh_tmp, $buffer, $size; |
51 |
8 |
ahitrov@rambler.ru |
|
52 |
47 |
ahitrov@rambler.ru |
undef $fh_tmp; |
53 |
8 |
ahitrov@rambler.ru |
|
54 |
47 |
ahitrov@rambler.ru |
if (Contenido::File::store($filename.'.'.$extension, $filename_tmp.'.'.$extension)) { |
55 |
8 |
ahitrov@rambler.ru |
if (ref $IMAGE eq 'HASH' and $IMAGE->{'filename'}) { |
56 |
47 |
ahitrov@rambler.ru |
Contenido::File::remove($IMAGE->{'filename'}); |
57 |
8 |
ahitrov@rambler.ru |
} |
58 |
|
|
|
59 |
|
|
# hashref slice assigning - жжесть |
60 |
|
|
@{$IMAGE}{'filename', 'width', 'height'} = ( |
61 |
47 |
ahitrov@rambler.ru |
$filename.'.'.$extension, |
62 |
|
|
Image::Size::imgsize($filename_tmp.'.'.$extension), |
63 |
8 |
ahitrov@rambler.ru |
); |
64 |
|
|
foreach my $suffix (@preview) { |
65 |
|
|
|
66 |
|
|
my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'\' -quality 80 '.$filename_tmp.'.'.$extension.' '.$filename_tmp.'.'.$suffix.'.'.$extension; |
67 |
|
|
my $result = `$c_line`; |
68 |
|
|
|
69 |
|
|
if (length $result > 0) { |
70 |
|
|
warn 'Contenido Error: При вызове "'.$c_line.'" произошла ошибка "'.$result.'" ('.$@.")\n"; |
71 |
|
|
return undef; |
72 |
|
|
} |
73 |
47 |
ahitrov@rambler.ru |
@{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = ( |
74 |
|
|
$filename.'.'.$suffix.'.'.$extension, |
75 |
|
|
Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$extension), |
76 |
|
|
); |
77 |
|
|
%{$IMAGE->{'resize'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}}; |
78 |
|
|
Contenido::File::store($filename.'.'.$suffix.'.'.$extension, $filename_tmp.'.'.$suffix.'.'.$extension); |
79 |
|
|
unlink $filename_tmp.'.'.$suffix.'.'.$extension if -e $filename_tmp.'.'.$suffix.'.'.$extension; |
80 |
8 |
ahitrov@rambler.ru |
} |
81 |
47 |
ahitrov@rambler.ru |
if ( @preview ) { |
82 |
|
|
@{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$preview[0]}}{'filename', 'width', 'height'}; |
83 |
|
|
@{$IMAGE->{'resize'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$preview[0]}}{'filename', 'width', 'height'}; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
########## CROPS |
87 |
|
|
foreach my $suffix (@crops) { |
88 |
|
|
|
89 |
|
|
my $shave_string; |
90 |
|
|
my ($nwidth, $nheight) = $suffix =~ /(\d+)x(\d+)/i ? ($1, $2) : (0, 0); |
91 |
|
|
if ( ($IMAGE->{width} / $IMAGE->{height}) > ($nwidth / $nheight) ) { |
92 |
|
|
my $shave_pixels = (($IMAGE->{width} / $IMAGE->{height}) - ($nwidth / $nheight)) * $IMAGE->{height}; |
93 |
|
|
$shave_string = ' -shave '.int($shave_pixels / 2).'x0'; |
94 |
|
|
} elsif ( ($IMAGE->{height} / $IMAGE->{width}) > ($nheight / $nwidth) ) { |
95 |
|
|
my $shave_pixels = (($IMAGE->{height} / $IMAGE->{width}) - ($nheight / $nwidth)) * $IMAGE->{width}; |
96 |
|
|
$shave_string = ' -shave 0x'.int($shave_pixels / 2); |
97 |
|
|
} |
98 |
|
|
if ( $shave_string ) { |
99 |
|
|
my $c_line = $state->{"convert_binary"}." $shave_string $filename_tmp.$extension $filename_tmp.shaved.$extension"; |
100 |
|
|
my $result = `$c_line`; |
101 |
|
|
if (length $result > 0) { |
102 |
|
|
print "Contenido Error: При вызове '$c_line' произошла ошибка '$result' ($@)\n"; |
103 |
|
|
} |
104 |
|
|
} |
105 |
|
|
|
106 |
52 |
ahitrov@rambler.ru |
my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality 80 '.$filename_tmp.'.shaved.'.$extension.' '.$filename_tmp.'.'.$suffix.'.'.$extension; |
107 |
47 |
ahitrov@rambler.ru |
my $result = `$c_line`; |
108 |
|
|
|
109 |
|
|
if (length $result > 0) { |
110 |
|
|
warn 'Contenido Error: При вызове "'.$c_line.'" произошла ошибка "'.$result.'" ('.$@.")\n"; |
111 |
|
|
return undef; |
112 |
|
|
} |
113 |
|
|
@{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = ( |
114 |
|
|
$filename.'.'.$suffix.'.'.$extension, |
115 |
|
|
Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$extension), |
116 |
|
|
); |
117 |
|
|
%{$IMAGE->{'crop'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}}; |
118 |
|
|
Contenido::File::store($filename.'.'.$suffix.'.'.$extension, $filename_tmp.'.'.$suffix.'.'.$extension); |
119 |
52 |
ahitrov@rambler.ru |
unlink $filename_tmp.'.shaved.'.$extension if -e $filename_tmp.'.shaved.'.$extension; |
120 |
|
|
unlink $filename_tmp.'.'.$suffix.'.'.$extension if -e $filename_tmp.'.'.$suffix.'.'.$extension; |
121 |
47 |
ahitrov@rambler.ru |
} |
122 |
|
|
if ( @crops ) { |
123 |
|
|
if ( !exists $IMAGE->{'mini'}{'filename'} ) { |
124 |
|
|
@{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$crops[0]}}{'filename', 'width', 'height'}; |
125 |
|
|
} |
126 |
|
|
@{$IMAGE->{'crop'}}{'filename', 'width', 'height'} = @{$IMAGE->{'crop'}{$crops[0]}}{'filename', 'width', 'height'}; |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
########## SHRINKS |
130 |
|
|
foreach my $suffix (@shrinks) { |
131 |
|
|
|
132 |
|
|
my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality 80 '.$filename_tmp.'.'.$extension.' '.$filename_tmp.'.'.$suffix.'.'.$extension; |
133 |
|
|
my $result = `$c_line`; |
134 |
|
|
|
135 |
|
|
if (length $result > 0) { |
136 |
|
|
warn 'Contenido Error: При вызове "'.$c_line.'" произошла ошибка "'.$result.'" ('.$@.")\n"; |
137 |
|
|
return undef; |
138 |
|
|
} |
139 |
|
|
@{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = ( |
140 |
|
|
$filename.'.'.$suffix.'.'.$extension, |
141 |
|
|
Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$extension), |
142 |
|
|
); |
143 |
|
|
%{$IMAGE->{'shrink'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}}; |
144 |
|
|
Contenido::File::store($filename.'.'.$suffix.'.'.$extension, $filename_tmp.'.'.$suffix.'.'.$extension); |
145 |
|
|
unlink $filename_tmp.'.'.$suffix.'.'.$extension if -e $filename_tmp.'.'.$suffix.'.'.$extension; |
146 |
|
|
} |
147 |
|
|
if ( @shrinks && !exists $IMAGE->{'mini'}{'filename'} ) { |
148 |
|
|
if ( !exists $IMAGE->{'mini'}{'filename'} ) { |
149 |
|
|
@{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$shrinks[0]}}{'filename', 'width', 'height'}; |
150 |
|
|
} |
151 |
|
|
@{$IMAGE->{'shrink'}}{'filename', 'width', 'height'} = @{$IMAGE->{'shrink'}{$shrinks[0]}}{'filename', 'width', 'height'}; |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
|
155 |
8 |
ahitrov@rambler.ru |
} |
156 |
|
|
|
157 |
47 |
ahitrov@rambler.ru |
unlink $filename_tmp.'.'.$extension if -e $filename_tmp.'.'.$extension; |
158 |
8 |
ahitrov@rambler.ru |
} else { |
159 |
|
|
warn 'ERROR in incoming POST form: "'.$field.' is not file upload (value: "'.$SETS->{$field}.'")'; |
160 |
|
|
} |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
# Дополнительные поля - берем все, что найдем... |
164 |
|
|
|
165 |
|
|
my $pattern = "^".$field.q|\.(.*)$|; |
166 |
|
|
|
167 |
|
|
foreach my $localfield (keys(%{ $SETS })) { |
168 |
|
|
if ($localfield =~ /$pattern/) { |
169 |
|
|
my $imagefield = $1; |
170 |
|
|
$IMAGE->{$imagefield} = $m->comp("/contenido/components/filter.msn", str => $SETS->{$localfield}); |
171 |
|
|
} |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
return $IMAGE; |
175 |
|
|
</%init> |