How to customise thumbnails
Based on EPrints-3.3.10.
To customise the size and type of thumbnails generated by eprints you can create archive-based configuration. The steps below show how to add a new thumbnail size, and render it the way *I* want it.
The default thumbnails are generated by: ~/perl_lib/EPrints/Plugin/Convert/Thumbnails.pm
I wanted to add a new size 160x90px, called 'homepage' (to fit in with the institution's branding).
To make your own set of thumbnail sizes, edit ~/archives/ARCHIVEID/cfg/cfg.d/plugins.pl
.
I added the following to the file:
$c->{plugins}->{'Convert::Thumbnails'}->{params}->{sizes} = {(
small => [66,50],
homepage => [160, 90], # new size - others are copied from Thumbnails.pm
medium => [200,150],
preview => [400,300],
lightbox => [640,480],
)};
$c->{thumbnail_types} = sub {
my( $list, $repo, $doc ) = @_;
push @$list, qw( homepage ); # add new size to list of files to be generated
};
By default this will produce a thumbnail that will fit into a box 160x90px - a proportionally scaled down version of the original. The 'small' thumbnail size produces a cropped version of the original. This is a better option for my needs, but it has a grey 1px border around it - that I don't want.
To alter the thumbnails I need to add the following to ~/archives/ARCHIVEID/cfg/cfg.d/plugins.pl
- which is a slightly modified version of the sub call_convert
copied from Thumbnails.pl
$c->{plugins}->{'Convert::Thumbnails'}->{params}->{call_convert} = sub {
my( $self, $dir, $doc, $src, $geom, $size ) = @_;
my $convert = $self->{'convert'};
my $version = $self->convert_version;
if (!defined($geom)) {
EPrints::abort("NO GEOM");
}
my $fn = $size . ".jpg";
my $dst = "$dir/$fn";
$geom = "$geom->[0]x$geom->[1]";
# JPEG
# Read this: http://www.imagemagick.org/script/command-line-processing.php#geometry
# for details of the convert geom options.
#
if( $size eq "small" )
{
# attempt to create a thumbnail that fits within the given dimensions
# geom^ requires 6.3.8
if( $version > 6.3 )
{
$self->_system($convert, "-strip", "-colorspace", "RGB", "-background", "white", "-thumbnail","$geom^", "-gravity", "center", "-extent", $geom, "-bordercolor", "gray", "-border", "1x1", $src."[0]", "JPEG:$dst");
}
else
{
$self->_system($convert, "-strip", "-colorspace", "RGB", "-background", "white", "-thumbnail","$geom>", "-extract", $geom, "-bordercolor", "gray", "-border", "1x1", $src."[0]", "JPEG:$dst");
}
}
elsif( $size eq "homepage" ) # This is the new section to deal with the new 'homepage' size
{
# attempt to create a thumbnail that fits within the given dimensions
# geom^ requires 6.3.8
if( $version > 6.3 )
{
# homepage thumbnails don't want a border on them!
$self->_system($convert, "-strip", "-colorspace", "RGB", "-background", "white", "-thumbnail","$geom^", "-gravity", "center", "-extent", $geom, "-bordercolor", "white", "-border", "0x0", $src."[0]", "JPEG:$dst");
}
else
{
# homepage thumbnails don't want a border on them!
$self->_system($convert, "-strip", "-colorspace", "RGB", "-background", "white", "-thumbnail","$geom>", "-extract", $geom, "-bordercolor", "white", "-border", "0x0", $src."[0]", "JPEG:$dst");
}
}
elsif( $size eq "medium" )
{
$self->_system($convert, "-strip", "-colorspace", "RGB", "-trim", "+repage", "-size", "$geom", "-thumbnail","$geom>", "-background", "white", "-gravity", "center", "-extent", $geom, "-bordercolor", "white", "-border", "0x0", $src."[0]", "JPEG:$dst");
}
else
{
$self->_system($convert, "-strip", "-colorspace", "RGB", "-background", "white", "-thumbnail","$geom>", "-extract", $geom, "-bordercolor", "white", "-border", "0x0", $src."[0]", "JPEG:$dst");
}
if( -s $dst )
{
return ($fn);
}
return ();
};
To test the new thumbnail generation, find an eprint with some good images attached to it. In the example below I'll use '1234', and assume the original image is called 'file.jpg' and is the first document in the eprint (this only matters for the URL to view the image in the browser with).
Run ~/bin/epadmin redo_thumbnails ARCHIVEID 1234 --verbose
You should see the convert command being run - and the new size filename being created e.g.
Starting EPrints Repository.
Connecting to DB ... done.
Redoing thumbnails for document 1234
/usr/bin/convert -strip -colorspace RGB -background white -thumbnail 160x90^ -gravity center -extent 160x90 -bordercolor white -border 0x0 /opt/eprints3/archives/digitallibrary/documents/disk0/00/00/12/34/01/file.jpg[0] JPEG:/tmp/_NyUoqmsuZ/homepage.jpg
/usr/bin/convert ... JPEG:/tmp/XR51ieKBCj/lightbox.jpg
/usr/bin/convert ... JPEG:/tmp/fCOP2BPoa2/preview.jpg
/usr/bin/convert ... JPEG:/tmp/zLEip1ewUq/medium.jpg
/usr/bin/convert ... JPEG:/tmp/weRJwQPgNT/small.jpg
NB The lines for lightbox, preview, medium and small.jpg have been truncated for clarity.
You should now be able to view the new thumbnails in a browser:
http://your.repo.url/1234/1.hashomepageThumbnailVersion/file.jpg