
david at kineticode
Mar 20, 2009, 11:12 PM
Post #9 of 18
(5153 views)
Permalink
|
On Mar 18, 2009, at 11:18 PM, David E. Wheeler wrote: > Can anyone give me a good reason not to require Imager.pm and to > enable USE_THUMBNAILS by default in 2.0? > > I ask because I got tired of writing the same code over and over > again to create thumbnails for a site, so I've written a new method > in Bric::Biz::Asset::Business::Media::Image that encapsulates most > of what's in the old thumbnail.mc utility template: > > http://www.bricolage.cc/templates/media/thumbnail.mc Just to follow up on this message, folks. I checked in this change in r8511. http://marc.info/?l=bricolage-commits&m=123752394411242&q=p3 I then backported it to 1.10 and applied it to a client's server. This client is creating a new site where every image can have up to two thumbnail, one 195 x 73 and one 90 x 69. I implemented a utility template to look up and generate these thumbnails like so: my $thumb = $m->comp('/util/get_thumb.mc', for => $img, wide => 1); Passing the "wide" parameter gives me a 195 x 73 image; otherwise i get the 90 x 69. The great part, though, is that this is the entire utility template: ~~~~~~~~~~~~~~~~~~~~~ <%args> $for $wide => 0 </%args> <%perl>; my ($suffix, $prefix, $width, $height) = $wide ? ( '_wide_thumb', 'Wide Thumb for ', 195, 73 ) : ( '_thumb', 'Thumb for ', 90, 69 ); my $thumb = eval { $for->find_or_create_alternate({ title_prefix => $prefix, file_suffix => $suffix, et_key_name => 'thumbnail', relate => 0, transformer => sub { shift->scale( xpixels => $width )->crop( height => $height ); } }); }; return $thumb unless $@ && $burner->get_mode == PUBLISH_MODE; $burner->throw_error(ref $@ ? $@->error : $@); </%perl> ~~~~~~~~~~~~~~~~~~~~~ Isn't that a hell of a lot simpler than the old thumbnail.mc? If they don't already exist, the new images are created, checked in, and moved to a publish desk. And unlike with the old hacks, all the events are properly logged and permissions properly checked (pass in a user object from the template to have permissions checked for a different user than the currently logged-in user. I'm really pleased with this, because, aside from all the work of writing the new `find_or_create_alternate()` method and a ton of tests for it, writing the above template was dead easy. Now it almost feels like there is real support for dynamically generating variations on images in Bricolage. The only part that's hacky is the fact that we still have to do this in templates (hence the funky error handling above). The correct way to do this would be to have a real event- driven callback architecture, not unlike what I've described here: http://www.justatheory.com/bricolage/design/tasks_jobs_actions.html Aside from that, the only other drawback I see to this implementation is that, when the original image is updated, the thumbnails are not regenerated. The callback architecture would solve that problem, too, but I'll be curious to see what solution Krzysztof has come up with in his templates. Anyway, please check it out, those of you with thumbnail-triggering templates, and let me know if it will make maintaing those templates easier for you. Best, David
|