Revision 668

Date:
2017/11/18 13:17:06
Author:
ahitrov
Revision Log:
Original image is transformed only if it's dimentions differ from target size.
New image|images parameter: quality (int). Default is 95.

Files:

Legend:

 
Added
 
Removed
 
Modified
  • utf8/core/lib/Contenido/File.pm

     
    192 192 my @preview = exists $prop->{'preview'} && ref $prop->{'preview'} eq 'ARRAY' ? @{$prop->{'preview'}} : exists $prop->{'preview'} && $prop->{'preview'} ? ($prop->{'preview'}) : ();
    193 193 my @crops = exists $prop->{'crop'} && ref $prop->{'crop'} eq 'ARRAY' ? @{$prop->{'crop'}} : exists $prop->{'crop'} && $prop->{'crop'} ? ($prop->{'crop'}) : ();
    194 194 my @shrinks = exists $prop->{'shrink'} && ref $prop->{'shrink'} eq 'ARRAY' ? @{$prop->{'shrink'}} : exists $prop->{'shrink'} && $prop->{'shrink'} ? ($prop->{'shrink'}) : ();
    195 my $quality = exists $prop->{quality} && $prop->{quality} =~ /^\d+$/ ? $prop->{quality} : 95;
    195 196
    196 197 my $filename = '/images/'.$object->get_file_name() || return;
    197 198 if ( $no_rename ) {
     
    269 270 if ( exists $prop->{transform} && ref $prop->{transform} eq 'ARRAY' && scalar @{$prop->{transform}} == 2 && $prop->{transform}[0] =~ /(crop|resize|shrink)/ ) {
    270 271 warn "Need transform\n" if $DEBUG;
    271 272 my $c_line;
    272 if ( $prop->{transform}[0] eq 'resize' ) {
    273 $c_line = $state->{'convert_binary'}.' -adaptive-resize \''.$prop->{transform}[1].'>\' -quality 100 '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.transformed.'.$ext;
    274 } elsif ( $prop->{transform}[0] eq 'crop' ) {
    275 my $shave_string;
    276 my ($nwidth, $nheight) = $prop->{transform}[1] =~ /(\d+)x(\d+)/i ? ($1, $2) : (0, 0);
    277 if ( ($image_info->{width} / $image_info->{height}) > ($nwidth / $nheight) ) {
    278 my $shave_pixels = (($image_info->{width} / $image_info->{height}) - ($nwidth / $nheight)) * $image_info->{height};
    279 $shave_string = ' -shave '.int($shave_pixels / 2).'x0';
    280 } elsif ( ($image_info->{height} / $image_info->{width}) > ($nheight / $nwidth) ) {
    281 my $shave_pixels = (($image_info->{height} / $image_info->{width}) - ($nheight / $nwidth)) * $image_info->{width};
    282 $shave_string = ' -shave 0x'.int($shave_pixels / 2);
    283 }
    284 if ( $shave_string ) {
    285 my $c_line = $state->{"convert_binary"}." $shave_string $filename_tmp.$ext $filename_tmp.shaved.$ext";
    286 my $result = `$c_line`;
    287 if (length $result > 0) {
    288 print "Contenido Error: При вызове '$c_line' произошла ошибка '$result' ($@)\n";
    289 return undef;
    273 my ($nwidth, $nheight) = $prop->{transform}[1] =~ /(\d+)x(\d+)/i ? ($1, $2) : (0, 0);
    274 unless ( $nwidth && $nheight && $image_info->{width} == $nwidth && $image_info->{height} == $nheight ) {
    275 if ( $prop->{transform}[0] eq 'resize' ) {
    276 $c_line = $state->{'convert_binary'}.' -adaptive-resize \''.$prop->{transform}[1].'>\' -quality '.$quality.' '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.transformed.'.$ext;
    277 } elsif ( $prop->{transform}[0] eq 'crop' ) {
    278 my $shave_string;
    279 if ( ($image_info->{width} / $image_info->{height}) > ($nwidth / $nheight) ) {
    280 my $shave_pixels = (($image_info->{width} / $image_info->{height}) - ($nwidth / $nheight)) * $image_info->{height};
    281 $shave_string = ' -shave '.int($shave_pixels / 2).'x0';
    282 } elsif ( ($image_info->{height} / $image_info->{width}) > ($nheight / $nwidth) ) {
    283 my $shave_pixels = (($image_info->{height} / $image_info->{width}) - ($nheight / $nwidth)) * $image_info->{width};
    284 $shave_string = ' -shave 0x'.int($shave_pixels / 2);
    290 285 }
    291 } else {
    292 my $c_line = "cp $filename_tmp.$ext $filename_tmp.shaved.$ext";
    293 my $result = `$c_line`;
    294 if (length $result > 0) {
    295 print "Contenido Error: При вызове '$c_line' произошла ошибка '$result' ($@)\n";
    296 return undef;
    286 if ( $shave_string ) {
    287 my $c_line = $state->{"convert_binary"}." $shave_string $filename_tmp.$ext $filename_tmp.shaved.$ext";
    288 my $result = `$c_line`;
    289 if (length $result > 0) {
    290 print "Contenido Error: При вызове '$c_line' произошла ошибка '$result' ($@)\n";
    291 return undef;
    292 }
    293 } else {
    294 my $c_line = "cp $filename_tmp.$ext $filename_tmp.shaved.$ext";
    295 my $result = `$c_line`;
    296 if (length $result > 0) {
    297 print "Contenido Error: При вызове '$c_line' произошла ошибка '$result' ($@)\n";
    298 return undef;
    299 }
    297 300 }
    301 $c_line = $state->{'convert_binary'}.' -adaptive-resize \''.$prop->{transform}[1].'!\' -quality '.$quality.' '.$filename_tmp.'.shaved.'.$ext.' '.$filename_tmp.'.transformed.'.$ext;
    302 } elsif ( $prop->{transform}[0] eq 'shrink' ) {
    303 $c_line = $state->{'convert_binary'}.' -adaptive-resize \''.$prop->{transform}[1].'!\' -quality '.$quality.' '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.transformed.'.$ext;
    298 304 }
    299 $c_line = $state->{'convert_binary'}.' -adaptive-resize \''.$prop->{transform}[1].'!\' -quality 100 '.$filename_tmp.'.shaved.'.$ext.' '.$filename_tmp.'.transformed.'.$ext;
    300 } elsif ( $prop->{transform}[0] eq 'shrink' ) {
    301 $c_line = $state->{'convert_binary'}.' -adaptive-resize \''.$prop->{transform}[1].'!\' -quality 100 '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.transformed.'.$ext;
    305 my $result = `$c_line`;
    306 $transformed = 1;
    307 warn "Transformed\n" if $DEBUG;
    308 unlink $filename_tmp.'.shaved.'.$ext if -e $filename_tmp.'.shaved.'.$ext;
    309 $size = -s $filename_tmp.'.transformed.'.$ext;
    302 310 }
    303 my $result = `$c_line`;
    304 $transformed = 1;
    305 warn "Transformed\n" if $DEBUG;
    306 unlink $filename_tmp.'.shaved.'.$ext if -e $filename_tmp.'.shaved.'.$ext;
    307 $size = -s $filename_tmp.'.transformed.'.$ext;
    308 311 }
    309 312
    310 313 if ( exists $opts{watermark} && $opts{watermark} ) {
     
    343 346
    344 347 warn "Thumbnail generator (preview)\n" if $DEBUG;
    345 348 foreach my $suffix (@preview) {
    346 my $c_line = $state->{'convert_binary'}.' -resize \''.$suffix.'>\' -quality 90 '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
    349 my $c_line = $state->{'convert_binary'}.' -resize \''.$suffix.'>\' -quality '.$quality.' '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
    347 350 my $result = `$c_line`;
    348 351
    349 352 if (length $result > 0) {
     
    391 394 }
    392 395 }
    393 396
    394 my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality 90 '.$filename_tmp.'.shaved.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
    397 my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality '.$quality.' '.$filename_tmp.'.shaved.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
    395 398 my $result = `$c_line`;
    396 399
    397 400 if (length $result > 0) {
     
    420 423 warn "Thumbnail generator (shrink)\n" if $DEBUG;
    421 424 foreach my $suffix (@shrinks) {
    422 425
    423 my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality 90 '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
    426 my $c_line = $state->{'convert_binary'}.' -geometry \''.$suffix.'!\' -quality '.$quality.' '.$filename_tmp.'.'.$ext.' '.$filename_tmp.'.'.$suffix.'.'.$ext;
    424 427 my $result = `$c_line`;
    425 428
    426 429 if (length $result > 0) {