or download this
use Imager;
use Imager::Filter::Autocrop;
...
$img->read(type=>'scalar', file=>$imgdata) or die "read: ".$img->errst
+r();
$img->autocrop(fuzz=>20) or die "autocrop: ".$img->errstr();
$img->write(file=>'out.jpg') or die "writing: ".$img->errstr();