Revision 47

Date:
2010/08/23 15:45:57
Author:
ahitrov@rambler.ru
Revision Log:
Возможности обрезки и сминания изображения
Files:

Legend:

 
Added
 
Removed
 
Modified
  • utf8/core/comps/contenido/components/set_image.msn

     
    21 21
    22 22 if ($SETS->{$field}) {
    23 23
    24 my $preview = $prop->{'preview'} || $keeper->{'preview'};
    25 my @preview = ref $preview && ref $preview eq 'ARRAY' ? @{$preview} : ref $preview ? ($keeper->{'preview'}) : ($preview);
    24 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 warn Dumper(\@crops);
    26 28
    27 if (ref $IMAGE eq 'HASH') {
    28 foreach my $suffix (@preview) {
    29 Contenido::File::remove(${$IMAGE->{mini}}{$suffix}->{'filename'}) if ${$IMAGE->{mini}}{$suffix} and ${$IMAGE->{mini}}{$suffix}->{'filename'};
    30 }
    29 if (ref $IMAGE eq 'HASH' && exists $IMAGE->{mini} && ref $IMAGE->{mini} eq 'HASH') {
    30 foreach my $val ( values %{$IMAGE->{mini}} ) {
    31 if ( ref $val && exists $val->{filename} && $val->{filename} ) {
    32 Contenido::File::remove($val->{'filename'});
    33 }
    34 }
    31 35 }
    32 36
    33 37 my $filename = '/images/'.$object->get_file_name() || return;
     
    39 43 my $fh = $upload->fh();
    40 44 my $size = (stat $fh)[7];
    41 45
    42 my $fh_tmp = IO::File->new('>'.$filename_tmp.'.'.$extension) || die 'Can\'t open temporary file';
    46 my $fh_tmp = IO::File->new('>'.$filename_tmp.'.'.$extension) || die 'Can\'t open temporary file';
    43 47
    44 my $buffer;
    48 my $buffer;
    45 49
    46 sysread $fh, $buffer, $size;
    47 syswrite $fh_tmp, $buffer, $size;
    50 sysread $fh, $buffer, $size;
    51 syswrite $fh_tmp, $buffer, $size;
    48 52
    49 undef $fh_tmp;
    53 undef $fh_tmp;
    50 54
    51 if (Contenido::File::store($filename.'.'.$extension, $filename_tmp.'.'.$extension)) {
    55 if (Contenido::File::store($filename.'.'.$extension, $filename_tmp.'.'.$extension)) {
    52 56 if (ref $IMAGE eq 'HASH' and $IMAGE->{'filename'}) {
    53 Contenido::File::remove($IMAGE->{'filename'});
    57 Contenido::File::remove($IMAGE->{'filename'});
    54 58 }
    55 59
    56 60 # hashref slice assigning - жжесть
    57 61 @{$IMAGE}{'filename', 'width', 'height'} = (
    58 $filename.'.'.$extension,
    59 Image::Size::imgsize($filename_tmp.'.'.$extension),
    62 $filename.'.'.$extension,
    63 Image::Size::imgsize($filename_tmp.'.'.$extension),
    60 64 );
    61 65 foreach my $suffix (@preview) {
    62 66
     
    67 71 warn 'Contenido Error: При вызове "'.$c_line.'" произошла ошибка "'.$result.'" ('.$@.")\n";
    68 72 return undef;
    69 73 }
    70 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
    71 $filename.'.'.$suffix.'.'.$extension,
    72 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$extension),
    73 );
    74 Contenido::File::store($filename.'.'.$suffix.'.'.$extension, $filename_tmp.'.'.$suffix.'.'.$extension);
    74 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
    75 $filename.'.'.$suffix.'.'.$extension,
    76 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$extension),
    77 );
    78 %{$IMAGE->{'resize'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
    79 Contenido::File::store($filename.'.'.$suffix.'.'.$extension, $filename_tmp.'.'.$suffix.'.'.$extension);
    80 unlink $filename_tmp.'.'.$suffix.'.'.$extension if -e $filename_tmp.'.'.$suffix.'.'.$extension;
    75 81 }
    76 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$preview[0]}}{'filename', 'width', 'height'};
    82 if ( @preview ) {
    83 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$preview[0]}}{'filename', 'width', 'height'};
    84 @{$IMAGE->{'resize'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$preview[0]}}{'filename', 'width', 'height'};
    85 }
    86
    87 ########## CROPS
    88 foreach my $suffix (@crops) {
    89
    90 my $shave_string;
    91 my ($nwidth, $nheight) = $suffix =~ /(\d+)x(\d+)/i ? ($1, $2) : (0, 0);
    92 if ( ($IMAGE->{width} / $IMAGE->{height}) > ($nwidth / $nheight) ) {
    93 my $shave_pixels = (($IMAGE->{width} / $IMAGE->{height}) - ($nwidth / $nheight)) * $IMAGE->{height};
    94 $shave_string = ' -shave '.int($shave_pixels / 2).'x0';
    95 } elsif ( ($IMAGE->{height} / $IMAGE->{width}) > ($nheight / $nwidth) ) {
    96 my $shave_pixels = (($IMAGE->{height} / $IMAGE->{width}) - ($nheight / $nwidth)) * $IMAGE->{width};
    97 $shave_string = ' -shave 0x'.int($shave_pixels / 2);
    98 }
    99 if ( $shave_string ) {
    100 my $c_line = $state->{"convert_binary"}." $shave_string $filename_tmp.$extension $filename_tmp.shaved.$extension";
    101 warn "CROP!!! [$c_line]\n\n";
    102 my $result = `$c_line`;
    103 if (length $result > 0) {
    104 print "Contenido Error: При вызове '$c_line' произошла ошибка '$result' ($@)\n";
    105 } else {
    106 unlink $filename_tmp.".".$suffix;
    107 $filename_tmp .= ".shaved";
    108 }
    109 }
    110
    111 my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality 80 '.$filename_tmp.'.'.$extension.' '.$filename_tmp.'.'.$suffix.'.'.$extension;
    112 my $result = `$c_line`;
    113
    114 if (length $result > 0) {
    115 warn 'Contenido Error: При вызове "'.$c_line.'" произошла ошибка "'.$result.'" ('.$@.")\n";
    116 return undef;
    117 }
    118 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
    119 $filename.'.'.$suffix.'.'.$extension,
    120 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$extension),
    121 );
    122 %{$IMAGE->{'crop'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
    123 Contenido::File::store($filename.'.'.$suffix.'.'.$extension, $filename_tmp.'.'.$suffix.'.'.$extension);
    124 unlink $filename_tmp.'.'.$suffix.'.'.$extension if -e $filename_tmp.'.'.$suffix.'.'.$extension;
    125 }
    126 if ( @crops ) {
    127 if ( !exists $IMAGE->{'mini'}{'filename'} ) {
    128 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$crops[0]}}{'filename', 'width', 'height'};
    129 }
    130 @{$IMAGE->{'crop'}}{'filename', 'width', 'height'} = @{$IMAGE->{'crop'}{$crops[0]}}{'filename', 'width', 'height'};
    131 }
    132
    133 ########## SHRINKS
    134 foreach my $suffix (@shrinks) {
    135
    136 my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality 80 '.$filename_tmp.'.'.$extension.' '.$filename_tmp.'.'.$suffix.'.'.$extension;
    137 my $result = `$c_line`;
    138
    139 if (length $result > 0) {
    140 warn 'Contenido Error: При вызове "'.$c_line.'" произошла ошибка "'.$result.'" ('.$@.")\n";
    141 return undef;
    142 }
    143 @{$IMAGE->{'mini'}{$suffix}}{'filename', 'width', 'height'} = (
    144 $filename.'.'.$suffix.'.'.$extension,
    145 Image::Size::imgsize($filename_tmp.'.'.$suffix.'.'.$extension),
    146 );
    147 %{$IMAGE->{'shrink'}{$suffix}} = %{$IMAGE->{'mini'}{$suffix}};
    148 Contenido::File::store($filename.'.'.$suffix.'.'.$extension, $filename_tmp.'.'.$suffix.'.'.$extension);
    149 unlink $filename_tmp.'.'.$suffix.'.'.$extension if -e $filename_tmp.'.'.$suffix.'.'.$extension;
    150 }
    151 if ( @shrinks && !exists $IMAGE->{'mini'}{'filename'} ) {
    152 if ( !exists $IMAGE->{'mini'}{'filename'} ) {
    153 @{$IMAGE->{'mini'}}{'filename', 'width', 'height'} = @{$IMAGE->{'mini'}{$shrinks[0]}}{'filename', 'width', 'height'};
    154 }
    155 @{$IMAGE->{'shrink'}}{'filename', 'width', 'height'} = @{$IMAGE->{'shrink'}{$shrinks[0]}}{'filename', 'width', 'height'};
    156 }
    157
    158
    77 159 }
    78 160
    79 unlink $filename_tmp.'.'.$extension if -e $filename_tmp.'.'.$extension;
    80 foreach my $suffix (@preview) {
    81 unlink $filename_tmp.'.'.$suffix.'.'.$extension if -e $filename_tmp.'.'.$suffix.'.'.$extension;
    82 }
    161 unlink $filename_tmp.'.'.$extension if -e $filename_tmp.'.'.$extension;
    83 162 } else {
    84 163 warn 'ERROR in incoming POST form: "'.$field.' is not file upload (value: "'.$SETS->{$field}.'")';
    85 164 }