forked from bricoleurs/templates
-
Notifications
You must be signed in to change notification settings - Fork 1
/
thumbnail.mc
218 lines (153 loc) · 5.96 KB
/
thumbnail.mc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<%doc>
=head1 Name
thumbnail.mc - Create a thumbnail document for an image document
=head1 SYNOPSIS
my $thumbnail = $m->comp(
'/util/thumbnail.mc',
image => $image,
);
=head1 Description
So you have lots of image documents, but you need thumbnails for them. You
could write a template that uses L<Imager|Imager> or some other module to
create a thumbnail version, but since Bricolage creates thumbnails for you,
why bother? Why not just take advantage of Bricolage's thumbnail images?
This utility template does just that. It creates image documents for Bricolage
thumbnail image files and associate them with the image document for which
they were created. Simply pass in an image document and it will create a
thumbnail document, relate it to the image document, and return it.
To get set up to use it, just follow these steps:
Here's some extra information (just playing with svn)
=over
=item *
Enable the C<USE_THUMBNAILS> F<bricolage.conf> directive and restart
Bricolage. If you have lots of images already, you'll need to view them all in
the "Find Media" interface in order to trigger the creation of thumbnail
images for them all.
=item *
Enable the C<PUBLISH_RELATED_ASSETS> F<bricolage.conf> directive. This will
force the thumbnail image to always be published at the same time as the
original image.
=item *
Check the "Related Media" checkbox for all image element types for documents
of which you plan to associate thumbnail images.
=item *
Create a utility template for this template code. I like to put utility
templates in the F</util> category, so this might be called
F</util/thumbnail.mc>.
=item *
From another template, call this utility template, passing in an image media
document to get back a thumbnail. Something like this:
my $image = $element->get_related_media;
$burner->throw_error($image->get_uri . ' is not an image document')
unless $image->isa('Bric::Biz::Asset::Business::Media::Image');
my $thumb = $m->comp('/util/thumbnail.mc', image => $image);
$m->print(
'<a href="', $image->get_uri, '" title="', $image->get_title, '">',
'<img src="', $thumb->get_uri, '" alt="', $thumb->get_title,
qq{" /></a>\n},
);
That will create an C<img> tag for the thumbnail image that links to the
original image.
=back
=head1 Parameters
=over
=item C<image>
Required. A Bric::Biz::Asset::Business::Media::Image object for which you wish
to create a thumbnail.
=item C<key_name>
The key name of the media element type to use when creating the thumbnail
image document. Defaults to the same element type as is used by the C<$image>
document.
=item C<file_prefix>
The prefix to prepend to the image file name to create the thumbnail file
name. Defaults to "thumb-".
=item C<file_suffix>
The suffix to append to the image file name to create the thumbnail file
name. Defaults to the empty string ('').
=item C<title_prefix>
The prefix to prepend to the image title to create the thumbnail title.
Defaults to "Thumbnail for ".
=item C<title_suffix>
The suffix to append to the image title to create the thumbnail title.
Defaults to the empty string ('').
=back
=head1 Prerequisites
=over
=item Bricolage 1.8
=item C<USE_THUMBNAILS> F<bricolage.conf> directive enabled.
=item C<PUBLISH_RELATED_ASSETS> F<bricolage.conf> directive enabled.
=back
=head1 Caveats
If an image is modified, Bricolage will create a new thumbnail for it, but
this template will still relate to the old thumbnail. Perhaps a future version
of this template will address this issue.
=head1 Authors
Rod Taylor <pg@rbt.ca>
David Wheeler <david@kineticode.com>
=head1 Copyright and License
Copyright 2004-2006 by Rod Taylor and David Wheeler.
This template is free software; you can redistribute it and/or modify it under
the same terms as Bricolage itself.
=cut
</%doc>
<%args>
$image
$key_name => $image->get_element_key_name
$file_prefix => 'thumb-'
$file_suffix => ''
$title_prefix => 'Thumbnail for '
$title_suffix => ''
</%args>
<%once>;
my $ET_CLASS = $Bric::Biz::ElementType::VERSION
? 'Bric::Biz::ElementType'
: 'Bric::Biz::AssetType';
</%once>
<%perl>;
my $melem = $image->get_element;
# Try to find an existing thumbnail image.
if (my $thumb = $melem->get_related_media) {
return $thumb;
}
# We don't need the URI but calling it before _thumb_file will ensure that a
# thumbnail is created if it doesn't already exist.
$image->thumb_uri;
# XXX Yeah yeah, I know I shouldn't call a private method...
my $path = $image->_thumb_file;
my $image_fn = $image->get_file_name;
my $thumb_fn = "$file_prefix$image_fn$file_suffix";
(my $uri = URI::Escape::uri_unescape($image->get_uri))
=~ s{\Q$image_fn\E$}{$thumb_fn};
# Make sure that the thumbnail file exists.
$burner->throw_error(
qq{Thumbnail file "$path" does not exist; is USE_THUMBNAILS enabled?}
) unless -f $path;
# Look up the thumbnail element type.
my $et = $ET_CLASS->lookup({ key_name => $key_name })
or $burner->throw_error(qq{Could not find the "$key_name" element type});
# Create the thumbnail media document.
my $thumb = Bric::Biz::Asset::Business::Media->new({
priority => $image->get_priority,
title => $title_prefix . $image->get_title . $title_suffix,
description => $title_prefix . $image->get_description . $title_suffix,
site_id => $image->get_site_id,
source__id => $image->get_source__id,
media_type_id => $image->get_media_type->get_id,
category__id => $image->get_category__id,
element_type => $et,
user__id => Bric::App::Session::get_user_id,
});
$thumb->set_cover_date($image->get_cover_date(Bric::Config::ISO_8601_FORMAT));
$thumb->save;
# Add the thumbnail image file to the media document.
open my $thumb_fh, '<', $path or die "Cannot open '$path': $!\n";
$thumb->upload_file($thumb_fh => $thumb_fn);
close $thumb_fh;
# Check in the thumbnail document.
$thumb->checkin;
$thumb->save;
# Add the thumbnail to the media document and return it.
$melem->set_related_media($thumb);
$melem->save;
return $thumb;
</%perl>