Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2009/04/01

Confronting Big Avatars with Perl and Magick

I do twitter. I run Linux. I use Twitux, not because I like it the most but because I dislike it the least of everything I can run without installing AIR.

It has a problem, however. Avatars are 48x48, but if you upload a way-too-freakin'-big avatar, like the huge pics you can get off your phone these days, it doesn't complain. It just screws up your display. And I finally got sick of it.


#!/usr/bin/perl

use Modern::Perl ;
use Image::Magick ;
use subs qw{ check_image fix_image } ;

# probably an easy change to make this more portable.
# oh well
my $dir = '/home/jacoby/.gnome2/twitux/avatars' ;

#go through all avatars
if ( opendir my $DH , $dir ) {
while ( defined( my $file = readdir( $DH ) ) ) {
next if $file =~ m{^\.}mx ;
my $full_filename = join '/' , $dir , $file ;
check_image $full_filename ;
}
}
else {
say 'Fail' ;
}

# are they 48x48?
sub check_image {
my $file = shift ;
my $image = new Image::Magick ;
$image->Read($file) ;
my $width = $image->Get('width') || 0 ;
my $height = $image->Get('height') || 0 ;
return if $height == 48 && $width == 48 ;
return fix_image $file ;
}

#make them 48x48.
sub fix_image {
my $file = shift ;
my $image = new Image::Magick ;
$image->Read($file) ;
$image->Resize(width=>48,height=>48) ;
$image->Write($file) ;
return ;
}


You need Perl. You need Image::Magick. You need crontab to run it ever so often.