Changeset 226

Show
Ignore:
Timestamp:
03/17/07 20:18:06 (2 years ago)
Author:
matt
Message:

Just use Image::Magick now

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plugins/demo/gallery

    r221 r226  
    2323     
    2424use File::MMagic; 
    25 use Image::Imlib2; 
     25use Image::Magick; 
     26use Image::Size qw(imgsize); 
    2627use Data::Page; 
    2728use XML::LibXML::XPathContext; 
     
    135136    my $out; 
    136137     
    137     $self->resize_image($size, $file, $type, \$out); 
     138    if ($client->param('size') eq 'thumb') { 
     139        $self->resize_thumb($size, $file, $type, \$out); 
     140    } 
     141    else { 
     142        $self->resize_image($size, $file, $type, \$out); 
     143    } 
    138144     
    139145    $cache->set("$file+$size", $out); 
     
    146152} 
    147153 
     154sub resize_thumb { 
     155    my ($self, $size, $file, $type, $out) = @_; 
     156     
     157    my ($w, $h) = imgsize($file); 
     158    my $biggest = ($w > $h) ? $w : $h; 
     159    my $ratio = $size / $biggest; 
     160    my $neww = int($w * $ratio); 
     161    my $newh = int($h * $ratio); 
     162    my $p = Image::Magick->new($neww."x$newh"); 
     163    $p->Read($file); 
     164    $p->Resize(width=>$neww, height=>$newh, filter=>'Lanczos'); 
     165    $p->UnsharpMask( 
     166        radius => 1.5, 
     167        sigma => 1, 
     168        amount => 1.5, 
     169        threshold => 0.003 
     170    ); 
     171    $p->Set(quality => 75); 
     172 
     173    ($$out) = $p->ImageToBlob(); 
     174    return; 
     175} 
     176 
    148177sub resize_image { 
    149178    my ($self, $size, $file, $type, $out) = @_; 
    150179     
    151     my $image = Image::Imlib2->load($file) 
    152         or die "Image load ($file) failed: $!"; 
    153  
    154     my ($w, $h) = ($image->width(), $image->height()); 
    155      
    156     $self->log(LOGINFO, "Original width x height: $w x $h"); 
    157      
    158     my $quality = $self->config('GalleryThumbQuality') || 'preview'; 
    159     $quality = 'normal' if $quality ne 'preview'; 
    160     $quality = 'normal' if $self->client->param('size') ne 'thumb'; 
    161      
    162     $self->log(LOGINFO, "Scaling to $size with quality: $quality"); 
    163      
    164     my $thumb = $image->create_scaled_image( 
    165                                  $w > $h ? 
    166                                    ($size, 0) 
    167                                  : (0, $size) 
    168                              ); 
    169      
    170     $self->log(LOGINFO, "sharpening $file size $size"); 
    171     #$thumb->sharpen(1) if $quality eq 'normal'; 
    172      
    173     my ($fh, $filename) = tempfile("galleryXXXXXXXX", SUFFIX => ".jpg"); 
    174     $thumb->save($filename); 
    175     seek($fh, 0, 0); 
    176     local $/; 
    177     $$out = <$fh>; 
    178     unlink($filename); 
     180    my ($w, $h) = imgsize($file); 
     181    my $biggest = ($w > $h) ? $w : $h; 
     182    my $ratio = $size / $biggest; 
     183    my $neww = int($w * $ratio); 
     184    my $newh = int($h * $ratio); 
     185    my $p = Image::Magick->new($neww."x$newh"); 
     186    $p->Read($file); 
     187    $p->Resize(width=>$neww, height=>$newh, filter=>'Lanczos'); 
     188    $p->UnsharpMask( 
     189        radius => 1.5, 
     190        sigma => 1, 
     191        amount => 1.0, 
     192        threshold => 0.03 
     193    ); 
     194    $p->Set(quality => 75); 
     195 
     196    ($$out) = $p->ImageToBlob(); 
    179197    return; 
    180198}