
bret at pectopah
Jul 21, 2008, 2:42 PM
Post #13 of 25
(913 views)
Permalink
|
|
Re: API Access of Updated Pages, plus templates
[In reply to]
|
|
Hi Matt. That's weird. I wonder why the element type lookup fails. There are three pieces to this image stuff that we use. The first is a template for a related-media subelement called "feature_image". The idea is that you insert a feature_image in a story, and upload a picture, and this is the template for that element. The picture uploaded into feature_image is never actually displayed anywhere. Images inside subelements of feature_image are, though. the feature_image template names the desired subelements (in essence, the desired set of large and small versions of the picture), and then calls image_resize_and_relate, which makes those subelements and the pictures to go inside them. ****** feature_image.mc <%perl> my %ratio; my %element_names_and_sizes; if (!$element->get_related_media) { $burner->throw_error('One of the picture containers in the story "' . $story->get_title . '" is empty (it does not have an image file related to it). Please delete the container, or relate an image to it, and try again.'); } %ratio = ( 'height_aspect' => 2, 'width_aspect' => 3 ); %element_names_and_sizes = ( 'article_image' => 520, 'slideshow_image' => 454, 'thumbnail_image' => 166 ); $m->comp('/util/image_resize_and_relate.mc', 'element_names_and_sizes' => \%element_names_and_sizes, 'needs_cropping' => \%ratio, 'publish_images_on_story_preview' => 'Yes', 'picture' => $element, 'target_image_element_type' => 'auto_generated_image', 'media_workflow' => Bric::Biz::Workflow->lookup({ name => 'Media' }), 'publish_desk' => Bric::Biz::Workflow::Parts::Desk->lookup({ name => 'Media Publish' }) ); my $article_image_container = $element->get_container('article_image'); if ($article_image_container->get_related_media) { </%perl> <div class="topimage" style="width: <% $article_image_container->get_related_media->get_value('width') %>px;"> <img src="<% $article_image_container->get_related_media->get_uri %>" alt="<% $element->get_value('caption') %>" width="<% $article_image_container->get_related_media->get_value('width') %>" height="<% $article_image_container->get_related_media->get_value('height') %>" /> <div class="caption"><% $element->get_value('caption') %></div> </div><!--end topimage--> % } ****** In image_resize_and_relate.mc, we actually make all the small pictures we want, along with the container subelements, and we relate the pictures to those containers ****** image_resize_and_relate.mc <%args> %element_names_and_sizes %needs_cropping $publish_images_on_story_preview $picture $target_image_element_type $media_workflow $publish_desk </%args> <%perl> use Bric::Util::Time qw(:all); use Date::Format; use DateTime; my $date = time; my $formatted_date = strfdate($date); # $m->out($formatted_date . '<br>'); # $m->out('Today is: ' . DateTime->now(time_zone => 'Canada/Eastern')->strftime("%Y/%m/%d") . '<br>'); my ($picture_is_good, $format, $scolding, $user, $new_media_document); if (!$story->get_user__id) { $user = Bric::App::Session->get_user_id; } else { $user = $story->get_user__id; } #pull out the media document inside the element and make sure it's a picture if ($picture->get_related_media) { $scolding = $m->scomp('/util/image_check.mc', pic => $picture->get_related_media ); if ($scolding) { $m->out('<strong>' . $scolding . '</strong><br>'); } else { $picture_is_good = 1; $format = $burner->notes('ext'); $burner->notes( ext => '' ); } } if ($picture_is_good) { my (%check_list, %safe_list); #check for containers foreach my $container_type(keys %element_names_and_sizes) { foreach my $existing_container($picture->get_elements) { if (($existing_container->get_key_name eq $container_type) && (!$check_list{$container_type})) { #OK, we're working with this one $check_list{$container_type} = 1; $safe_list{$existing_container->get_id} = 1; } } } #kill any duplicate containers foreach my $container_type(keys %element_names_and_sizes) { foreach my $existing_container($picture->get_elements) { if (($existing_container->get_key_name eq $container_type) && (!$safe_list{$existing_container->get_id})) { #OK, we're deleting this one my @killswitch; $killswitch[0] = $existing_container; $picture->delete_elements(\@killswitch); $picture->save; } } } #create any missing containers foreach my $container_type(keys %element_names_and_sizes) { if (!$check_list{$container_type}) { my $required_container_type = Bric::Biz::ElementType->lookup({ 'key_name' => $container_type }); $picture->add_container($required_container_type); $picture->save; } } #OK, we now have all the containers we need. #Loop through them. If they already have pictures, leave them alone. #If they don't have pictures, create them and relate them. foreach my $working_container($picture->get_containers(keys %element_names_and_sizes)) { if ($working_container->get_related_media) { my $thumbScolding = $m->scomp('/util/image_check.mc', pic => $working_container->get_related_media ); if ($thumbScolding) { #container has bad media file $m->out('<strong>' . $thumbScolding . '</strong><br>'); } else { #container has good media file, so leave it alone } } else { #container is empty, so create image and relate it to container my $target_element_type = Bric::Biz::ElementType->lookup({key_name => $target_image_element_type}); my $media_document_title = $picture->get_related_media->get_title . '-' . $working_container->get_key_name; my %target_initial_state = ( 'user__id' => $user, 'active' => 1, 'priority' => $story->get_priority, 'title' => $picture->get_related_media->get_title . '-' . $working_container->get_key_name, 'description' => '', 'workflow_id' => $media_workflow->get_id, 'element_type' => $target_element_type, 'site_id' => $story->get_site_id, 'source__id' => $picture->get_related_media->get_source__id, 'cover_date' => $formatted_date, # 'media_type_id' => $picture->get_related_media->get_media_type->get_id, 'category__id' => $picture->get_related_media->get_category__id ); my $new_file_name = $picture->get_related_media->get_file_name; my $extension = "_$element_names_and_sizes{$working_container->get_key_name}.$format"; $new_file_name =~ s/....$/$extension/; my $new_uri = $picture->get_related_media->get_primary_category->get_uri . DateTime->now(time_zone => 'Canada/Eastern')->strftime("%Y/%m/") . $new_file_name; #check if there is already an image with the same URI in the Bricolage library. #If there is, open that media document. Otherwise create a new one. # $m->out('Looking for an existing media document with this uri:<br>' . $picture->get_related_media->get_primary_category->get_uri . time2str("%Y/%m/", $date) . $new_file_name . '<br>'); my @existing_media_document = Bric::Biz::Asset::Business::Media->list({ 'uri' => $new_uri, 'active' => 1 }); if ($existing_media_document[0]) { # $m->out('<br>Found existing one. URI is ' . $existing_media_document[0]->get_primary_uri . '<br>'); $new_media_document = $existing_media_document[0]; $new_media_document->checkout({ user__id => $user }); } else { $new_media_document = Bric::Biz::Asset::Business::Media::Image->new(\%target_initial_state); $new_media_document->set_workflow_id($media_workflow->get_id); $new_media_document->save; $publish_desk->accept({ asset => $new_media_document }); $publish_desk->save; $new_media_document->checkin; } #Here we go, making the new image my $temp_file = "/tmp/$new_file_name"; my $new_image = new Image::Magick; $new_image->Read($picture->get_related_media->get_path); if ($needs_cropping{'height_aspect'}) { # $m->out('<br><br>Going to crop!'); my $target_height = int(($needs_cropping{'height_aspect'} * $picture->get_related_media->get_value('width')) / $needs_cropping{'width_aspect'}); $new_image->Crop( geometry => $picture->get_related_media->get_value('width')."x$target_height+0+0" ); # } else { # $m->out('<br><br>NOT going to crop!'); } $new_image->Thumbnail($element_names_and_sizes{$working_container->get_key_name}); $new_image->Set(colorspace=>'RGB'); $new_image->Write($temp_file); undef $new_image; #now upload our new image my $file_handler; open ($file_handler, "<$temp_file"); $new_media_document->upload_file($file_handler, $new_file_name, $picture->get_related_media->get_media_type->get_name); #no need to close filehandler because upload_file does it automatically #delete the temp image here unlink ($temp_file); #save and check in if required $new_media_document->save; if ($new_media_document->get_checked_out) { # $publish_desk->checkin($new_media_document); $new_media_document->checkin; } if ($publish_images_on_story_preview eq 'Yes') { # $m->out('Got the go-ahead to publish images on preview<br><br>'); unless ($new_media_document->get_publish_status) { # $m->out('Trying to publish "' . $new_media_document->get_title . '".<br>'); $burner->publish_another($new_media_document,$picture->get_related_media->get_cover_date,1); } } #relate the image to the container $working_container->set_related_media($new_media_document); $working_container->save; } } } </%perl> ****** The last template is just a little thing to make sure the media file is actually a picture. ****** image_check.mc <%args> $pic </%args> <%perl> my %mediatypehash = %{$pic->get_media_type}; unless ($mediatypehash{'name'} eq "image/jpeg" || $mediatypehash{'name'} eq "image/jpg" || $mediatypehash{'name'} eq "image/gif" || $mediatypehash{'name'} eq "image/png") { $m->print('I was expecting a JPG, GIF, or PNG here. Got something else.'); } if (($mediatypehash{'name'} eq "image/jpeg") || ($mediatypehash{'name'} eq "image/jpg")) { $burner->notes( ext => 'jpg' ); } if ($mediatypehash{'name'} eq "image/gif") { $burner->notes( ext => 'gif' ); } if ($mediatypehash{'name'} eq "image/png") { $burner->notes( ext => 'png' ); } </%perl> ****** Hope this helps, Bret On Mon, 2008-07-21 at 17:10 -0400, Matt Rolf wrote: > On Jul 21, 2008, at 4:39 PM, Bret Dawson wrote: > > > Hi Matt. > > > > Hey, this looks like it's based on one of ours! > > Yes, it is! > > > Are you sure that your lookup for the thumbnail_photo element type is > > succeeding? > > That's the problem - it's coming back as undefined, when in reality it > is there. > > > There's a much more fully-featured version of this template now, if > > you're interested. When I have a moment to breathe I'll put it up on > > builidngbrics, but let me know if I should just post it here too. > > We would love to see that! > > -matt > -- Bret Dawson Producer Pectopah Productions Inc. (416) 895-7635 bret[at]pectopah.com www.pectopah.com
|