Line # Revision Author
1 3 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 48 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 3 ahitrov@rambler.ru
28 48 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 3 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 48 ahitrov@rambler.ru my $fh_tmp = IO::File->new('>'.$filename_tmp.'.'.$extension) || die 'Can\'t open temporary file';
46 3 ahitrov@rambler.ru
47 48 ahitrov@rambler.ru my $buffer;
48 3 ahitrov@rambler.ru
49 48 ahitrov@rambler.ru sysread $fh, $buffer, $size;
50 syswrite $fh_tmp, $buffer, $size;
51 3 ahitrov@rambler.ru
52 48 ahitrov@rambler.ru undef $fh_tmp;
53 3 ahitrov@rambler.ru
54 48 ahitrov@rambler.ru if (Contenido::File::store($filename.'.'.$extension, $filename_tmp.'.'.$extension)) {
55 3 ahitrov@rambler.ru if (ref $IMAGE eq 'HASH' and $IMAGE->{'filename'}) {
56 48 ahitrov@rambler.ru Contenido::File::remove($IMAGE->{'filename'});
57 3 ahitrov@rambler.ru }
58
59 # hashref slice assigning - ������
60 @{$IMAGE}{'filename', 'width', 'height'} = (
61 48 ahitrov@rambler.ru $filename.'.'.$extension,
62 Image::Size::imgsize($filename_tmp.'.'.$extension),
63 3 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 48 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 3 ahitrov@rambler.ru }
81 48 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 56 ahitrov@rambler.ru } else {
105 my $c_line = "cp $filename_tmp.$extension $filename_tmp.shaved.$extension";
106 my $result = `$c_line`;
107 if (length $result > 0) {
108 print "Contenido Error: ��� ������ '$c_line' ��������� ������ '$result' ($@)\n";
109 }
110 48 ahitrov@rambler.ru }
111
112 56 ahitrov@rambler.ru my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality 80 '.$filename_tmp.'.shaved.'.$extension.' '.$filename_tmp.'.'.$suffix.'.'.$extension;
113 my $result = `$c_line`;
114 48 ahitrov@rambler.ru
115 56 ahitrov@rambler.ru if (length $result > 0) {
116 warn 'Contenido Error: ��� ������ "'.$c_line.'" ��������� ������ "'.$result.'" ('.$@.")\n";
117 return undef;
118 }
119 48 ahitrov@rambler.ru @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
120 $filename.'.'.$suffix.'.'.$extension,
121 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$extension),
122 );
123 %{$IMAGE->{'crop'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
124 Contenido::File::store($filename.'.'.$suffix.'.'.$extension, $filename_tmp.'.'.$suffix.'.'.$extension);
125 51 ahitrov@rambler.ru unlink $filename_tmp.'.shaved.'.$extension if -e $filename_tmp.'.shaved.'.$extension;
126 48 ahitrov@rambler.ru unlink $filename_tmp.'.'.$suffix.'.'.$extension if -e $filename_tmp.'.'.$suffix.'.'.$extension;
127 }
128 if ( @crops ) {
129 if ( !exists $IMAGE->{'mini'}{'filename'} ) {
130 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$crops[0]}}{'filename', 'width', 'height'};
131 }
132 @{$IMAGE->{'crop'}}{'filename', 'width', 'height'} = @{$IMAGE->{'crop'}{$crops[0]}}{'filename', 'width', 'height'};
133 }
134
135 ########## SHRINKS
136 foreach my $suffix (@shrinks) {
137
138 my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality 80 '.$filename_tmp.'.'.$extension.' '.$filename_tmp.'.'.$suffix.'.'.$extension;
139 my $result = `$c_line`;
140
141 if (length $result > 0) {
142 warn 'Contenido Error: ��� ������ "'.$c_line.'" ��������� ������ "'.$result.'" ('.$@.")\n";
143 return undef;
144 }
145 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
146 $filename.'.'.$suffix.'.'.$extension,
147 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$extension),
148 );
149 %{$IMAGE->{'shrink'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
150 Contenido::File::store($filename.'.'.$suffix.'.'.$extension, $filename_tmp.'.'.$suffix.'.'.$extension);
151 unlink $filename_tmp.'.'.$suffix.'.'.$extension if -e $filename_tmp.'.'.$suffix.'.'.$extension;
152 }
153 if ( @shrinks && !exists $IMAGE->{'mini'}{'filename'} ) {
154 if ( !exists $IMAGE->{'mini'}{'filename'} ) {
155 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$shrinks[0]}}{'filename', 'width', 'height'};
156 }
157 @{$IMAGE->{'shrink'}}{'filename', 'width', 'height'} = @{$IMAGE->{'shrink'}{$shrinks[0]}}{'filename', 'width', 'height'};
158 }
159
160
161 3 ahitrov@rambler.ru }
162
163 48 ahitrov@rambler.ru unlink $filename_tmp.'.'.$extension if -e $filename_tmp.'.'.$extension;
164 3 ahitrov@rambler.ru } else {
165 warn 'ERROR in incoming POST form: "'.$field.' is not file upload (value: "'.$SETS->{$field}.'")';
166 }
167 }
168
169 # �������������� ���� - ����� ���, ��� ������...
170
171 my $pattern = "^".$field.q|\.(.*)$|;
172
173 foreach my $localfield (keys(%{ $SETS })) {
174 if ($localfield =~ /$pattern/) {
175 my $imagefield = $1;
176 $IMAGE->{$imagefield} = $m->comp("/contenido/components/filter.msn", str => $SETS->{$localfield});
177 }
178 }
179
180 return $IMAGE;
181 </%init>