Changeset 11915


Ignore:
Timestamp:
2006-06-09T14:12:49+12:00 (18 years ago)
Author:
mdewsnip
Message:

Much improved code for generating and maintaining the place image variants. Place images are no longer scaled larger, and it no longer rewrites all the .inf files every time the collection is imported!

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/cic-hcap/perllib/plugins/CICPlug.pm

    r11913 r11915  
    190190    my $dbh = DBI->connect('dbi:ODBC:CIC-HCAP');
    191191
    192     # $self->process_institutions($dbh);
    193     # $self->process_places($dbh);
     192    $self->process_institutions($dbh);
     193    $self->process_places($dbh);
    194194    $self->process_designers($dbh);
    195195
     
    289289        }
    290290
    291         $self->new_place_image($institution_doc_obj, $institution_best_place_image_location, "medium");
    292         $self->new_place_image($institution_doc_obj, $institution_best_place_image_location, "large");
     291        $self->generate_place_image_variant($institution_doc_obj, $institution_best_place_image_location, "medium");
     292        $self->generate_place_image_variant($institution_doc_obj, $institution_best_place_image_location, "large");
    293293
    294294        my $institution_best_place_image_large_file_name = $institution_best_place_image_name . "-large.jpg";
     
    329329        }
    330330        else {
    331         $self->new_place_image($institution_doc_obj, $institution_campus_plan_image_location, "large");
     331        $self->generate_place_image_variant($institution_doc_obj, $institution_campus_plan_image_location, "large");
    332332
    333333        my $institution_campus_plan_image_large_file_name = $institution_campus_plan_name . "-large.jpg";
     
    590590    while (my $place_images_match_hashref = $place_images_sql_handle->fetchrow_hashref) {
    591591        my $place_image_location = $place_images_match_hashref->{"Location"};
    592         $self->new_place_image($place_doc_obj, $place_image_location, "small");
    593         $self->new_place_image($place_doc_obj, $place_image_location, "large");
     592        $self->generate_place_image_variant($place_doc_obj, $place_image_location, "small");
     593        $self->generate_place_image_variant($place_doc_obj, $place_image_location, "large");
    594594
    595595        my $place_image_name = $place_images_match_hashref->{"FileName"};
     
    695695        if (defined($designer_place_image_match_hashref)) {
    696696        my $designer_place_image_location = $designer_place_image_match_hashref->{"Location"};
    697         $self->new_place_image($designer_doc_obj, $designer_place_image_location, "small");
     697        $self->generate_place_image_variant($designer_doc_obj, $designer_place_image_location, "small");
    698698
    699699        my $designer_place_image_name = $designer_place_image_match_hashref->{"FileName"};
     
    771771
    772772
    773 sub new_place_image
     773sub get_place_image_dimensions
    774774{
    775775    my $self = shift(@_);
    776     my ($doc_obj, $place_image_location, $place_image_size) = (@_);
     776    my $place_image_file_path = shift(@_);
    777777    my $fail_log_handle = $self->{'failhandle'};
    778778
    779     # Don't bother with empty values
    780     return if ($place_image_location eq "");
    781 
    782     # Convert the server location of the file into the local location of the file
    783     my $place_image_file_path = $place_image_location;
    784     $place_image_file_path =~ s/^[A-Z]:/$self->{'images_directory'}/;
    785 
    786     # Create a new section for each image of the place
    787     my $place_image_section = $doc_obj->insert_section($doc_obj->get_end_child($doc_obj->get_top_section()));
    788     $doc_obj->add_utf8_text($place_image_section, "Some dummy text.");
    789     $doc_obj->add_utf8_metadata($place_image_section, "Title", "1");
    790 
     779    # Make sure the place image file actually exists
    791780    if (!-f $place_image_file_path) {
    792781    print STDERR "<ProcessingError n='$place_image_file_path' p='CICPlug' r='Does not exist'>\n" if ($self->{'gli'});
     
    795784    return;
    796785    }
    797 
    798     my $place_image_file_name = $place_image_file_path;
    799     $place_image_file_name =~ s/^.*?([^\\]+)$/$1/;
    800     my ($image_file_root) = ($place_image_file_name =~ /^(.+)\./);
    801 
    802     # Scale the original image to make the place image
    803     my $image_width = $self->{$place_image_size . '_image_width'};
    804     my $image_options = "-scale $image_width " . $self->{$place_image_size . '_image_options'};
    805     my $image_file_name = $image_file_root . "-$place_image_size." . $self->{$place_image_size . '_image_type'};
    806 
    807     my $original_image_file_path = $place_image_file_path;
    808     my $image_type = $place_image_size;
    809 
    810     my $original_image_file_date = (stat($original_image_file_path))[9];
    811     my $image_file_path = &util::filename_cat($self->{'cache_directory'}, $image_file_name);
    812     my $image_command = "convert -verbose $image_options \"$original_image_file_path\" \"$image_file_path\"";
    813     my $image_height;
    814 
    815     # Check if this image has already been generated by looking for a ".inf" file in the tmp directory
    816     $image_file_name =~ /^(.*?)\.[^\.]+$/;
    817     my $image_info_file_name = $1 . ".inf";
    818     my $image_info_file_path = &util::filename_cat($self->{'cache_directory'}, $image_info_file_name);
    819     if (-e $image_info_file_path) {
    820     open(IMAGE_INFO_FILE, "<$image_info_file_path");
    821     my @image_info = <IMAGE_INFO_FILE>;
    822     close(IMAGE_INFO_FILE);
    823 
    824     # Read the original image file date and image command from the file and check they match
    825     my $old_original_image_file_date = $image_info[0];
    826     $old_original_image_file_date =~ s/\n$//;
    827     my $old_image_command = $image_info[1];
    828     $old_image_command =~ s/\n$//;
    829     if ($old_original_image_file_date eq $original_image_file_date && lc($old_image_command) eq lc($image_command)) {
    830         # print STDERR "Already generated $image_file_name...\n";
    831         $image_width = $image_info[2];
    832         $image_width =~ s/\n$//;
    833         $image_height = $image_info[3];
    834         $image_height =~ s/\n$//;
    835     }
    836     }
    837 
    838     # Generate the image if it doesn't already exist
    839     if (!defined($image_width) || !defined($image_height)) {
    840     print STDERR "Generating place image file: $image_file_name...\n";
    841     my $outhandle = $self->{'outhandle'};
    842     print $outhandle "$image_command\n" if ($self->{'verbosity'} > 2);
    843     my $image_result = `$image_command` || "";
    844     print $outhandle "RESULT = $image_result\n" if ($self->{'verbosity'} > 2);
    845 
    846     # Check that the converted image exists
    847     if (!-f $image_file_path) {
    848         print STDERR "<ProcessingError n='$image_file_path' p='CICPlug' r='Could not be created'>\n" if ($self->{'gli'});
    849         print STDERR "Error: Image $image_file_path could not be created.\n";
    850         print $fail_log_handle "Error: Image $image_file_path could not be created.\n";
    851         return;
    852     }
    853 
    854     # Get image dimensions
    855     if ($image_command !~ /\-scale / && $image_result =~ m/([0-9]+)x([0-9]+)/) {
    856         $image_width = $1;
    857         $image_height = $2;
    858     }
    859     elsif ($image_result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
    860         $image_width = $1;
    861         $image_height = $2;
    862     }
    863     elsif ($image_result =~ m/Image Width: (\d+) Image Length: (\d+)/) {
    864         $image_width = $1;
    865         $image_height = $2;
    866     }
    867         elsif ($image_result =~ m/JPEG (\d+)x(\d+)/) {
    868         $image_width = $1;
    869         $image_height = $2;
    870         }
    871     else {
    872         print STDERR "Error: Could not parse image width and height for $original_image_file_path\n";
    873         return;
    874     }
    875     }
    876 
    877     # Associate the image file
    878     $doc_obj->associate_file($image_file_path, $image_file_name, undef, $self->{'section'});
    879 
    880     # Add various bits of metadata for the image
    881     my $image_metadata_value = $image_file_name;
    882     $image_metadata_value =~ s/ /%20/g;
    883     $doc_obj->add_utf8_metadata($self->{'section'}, $image_type . "Image", $image_metadata_value);
    884     $doc_obj->add_metadata($self->{'section'}, $image_type . "ImageWidth", $image_width);
    885     $doc_obj->add_metadata($self->{'section'}, $image_type . "ImageHeight", $image_height);
    886     $doc_obj->add_metadata($self->{'section'}, $image_type . "ImagePath", "_httpcollection_/index/assoc/[parent(Top):assocfilepath]/[${image_type}Image]");
    887 
    888     # Write the image info file so we don't have to regenerate this exact image again in the future
    889     open(IMAGE_INFO_FILE, ">$image_info_file_path");
    890     print IMAGE_INFO_FILE "$original_image_file_date\n";
    891     print IMAGE_INFO_FILE "$image_command\n";
    892     print IMAGE_INFO_FILE "$image_width\n";
    893     print IMAGE_INFO_FILE "$image_height\n";
    894     close(IMAGE_INFO_FILE);
     786    my $place_image_file_date = (stat($place_image_file_path))[9];
     787
     788    # Check if this place image has already been identified by looking for a ".info" file in the same directory
     789    my $place_image_info_file_path = $place_image_file_path . ".info";
     790    if (-f $place_image_info_file_path) {
     791    # A ".info" file exists, so read the cached place image information from it
     792    open(PLACE_IMAGE_INFO_FILE, "<$place_image_info_file_path");
     793    my @place_image_info = <PLACE_IMAGE_INFO_FILE>;
     794    close(PLACE_IMAGE_INFO_FILE);
     795
     796    # Read the cached place image file date and check that it matches
     797    my $cached_place_image_file_date = $place_image_info[0];
     798    $cached_place_image_file_date =~ s/\n$//;
     799    if ($cached_place_image_file_date == $place_image_file_date) {
     800        # It does match, so use the cached information from the ".info" file instead of re-identifying the file
     801        my $place_image_width = $place_image_info[1];
     802        $place_image_width =~ s/\n$//;
     803        my $place_image_height = $place_image_info[2];
     804        $place_image_height =~ s/\n$//;
     805        return ($place_image_width, $place_image_height);
     806    }
     807    }
     808
     809    # We haven't already identified the place image, so do it now
     810    print STDERR "Identifying place image $place_image_file_path...\n";
     811    my $identify_command = "identify -format \"%w %h\" \"$place_image_file_path\"";
     812    my $identify_result = `$identify_command`;
     813    print "Identify result: $identify_result\n" if ($self->{'verbosity'} > 2);
     814
     815    # Check that the output is what we're expecting
     816    if ($identify_result !~ /(\d+) (\d+)/) {
     817    print STDERR "<ProcessingError n='$place_image_file_path' p='CICPlug' r='Could not identify'>\n" if ($self->{'gli'});
     818    print STDERR "Error: Place image $place_image_file_path could not be identified.\n";
     819    print $fail_log_handle "Error: Place image $place_image_file_path could not be identified.\n";
     820    return;
     821    }
     822
     823    # Parse the place image width and height from the output
     824    my $place_image_width = $1;
     825    my $place_image_height = $2;
     826
     827    # Write the place image info file so we don't have to identify this exact image again in the future
     828    open(PLACE_IMAGE_INFO_FILE, ">$place_image_info_file_path");
     829    print PLACE_IMAGE_INFO_FILE "$place_image_file_date\n";
     830    print PLACE_IMAGE_INFO_FILE "$place_image_width\n";
     831    print PLACE_IMAGE_INFO_FILE "$place_image_height\n";
     832    close(PLACE_IMAGE_INFO_FILE);
     833    return ($place_image_width, $place_image_height);
     834}
     835
     836
     837sub generate_place_image_variant
     838{
     839    my $self = shift(@_);
     840    my ($doc_obj, $place_image_location, $place_image_variant_size) = (@_);
     841    my $fail_log_handle = $self->{'failhandle'};
     842
     843    # Convert the server location of the file into the local location of the file
     844    my $place_image_file_path = $place_image_location;
     845    $place_image_file_path =~ s/^[A-Z]:/$self->{'images_directory'}/;
     846
     847    # Get the width and height of the place image
     848    my ($place_image_width, $place_image_height) = $self->get_place_image_dimensions($place_image_file_path);
     849    if (!defined($place_image_width) || !defined($place_image_height)) {
     850    # An error has occurred (error message generated by get_place_image_dimensions())
     851    return;
     852    }
     853    my $place_image_file_date = (stat($place_image_file_path))[9];
     854
     855    # Generate the path of the place image variant (in the cache directory)
     856    my $place_image_variant_file_suffix = "-$place_image_variant_size." . $self->{$place_image_variant_size . '_image_type'};
     857    my $place_image_variant_file_path = $place_image_location;
     858    $place_image_variant_file_path =~ s/^[A-Z]:/$self->{'cache_directory'}/;
     859    $place_image_variant_file_path =~ s/^(.+)(\..*)/$1$place_image_variant_file_suffix/;
     860    my ($place_image_variant_file_name) = ($place_image_variant_file_path =~ /^.+\\(.+)$/);
     861
     862    # Only scale down the place image if it is bigger than the desired width
     863    my $place_image_variant_desired_width = $self->{$place_image_variant_size . '_image_width'};
     864    if ($place_image_width > $place_image_variant_desired_width) {
     865    # Only generate the place image variant if it doesn't already exist, or if the place image is newer
     866    if (!-f $place_image_variant_file_path || $place_image_file_date > (stat($place_image_variant_file_path))[9]) {
     867        print STDERR "Generating place image variant $place_image_variant_file_path...\n";
     868        my ($place_image_variant_directory) = ($place_image_variant_file_path =~ /^(.+)\\.+$/);
     869        &util::mk_all_dir($place_image_variant_directory);
     870        my $place_image_variant_options = "-scale $place_image_variant_desired_width " . $self->{$place_image_variant_size . '_image_options'};
     871        my $convert_command = "convert $place_image_variant_options \"$place_image_file_path\" \"$place_image_variant_file_path\"";
     872        my $convert_result = `$convert_command`;
     873    }
     874    }
     875    else {
     876    # The desired width is bigger than the place image, so we just use the original
     877    $place_image_variant_file_path = $place_image_file_path;
     878    }
     879
     880    my ($place_image_variant_width, $place_image_variant_height) = $self->get_place_image_dimensions($place_image_variant_file_path);
     881    if (!defined($place_image_variant_width) || !defined($place_image_variant_height)) {
     882    # An error has occurred (error message generated by get_place_image_dimensions())
     883    return;
     884    }
     885
     886    # Create a new section for each place image variant (!! TO DO: Check if this is really necessary)
     887    my $place_image_variant_section = $doc_obj->insert_section($doc_obj->get_end_child($doc_obj->get_top_section()));
     888    $doc_obj->add_utf8_text($place_image_variant_section, "Some dummy text.");
     889    $doc_obj->add_utf8_metadata($place_image_variant_section, "Title", "1");
     890
     891    # Associate the place image variant file
     892    $doc_obj->associate_file($place_image_variant_file_path, $place_image_variant_file_name, undef, $self->{'section'});
     893
     894    # Add various bits of metadata for the place image variant
     895    my $place_image_variant_metadata_value = $place_image_variant_file_name;
     896    $place_image_variant_metadata_value =~ s/ /%20/g;
     897    $doc_obj->add_utf8_metadata($self->{'section'}, $place_image_variant_size . "Image", $place_image_variant_metadata_value);
     898    $doc_obj->add_metadata($self->{'section'}, $place_image_variant_size . "ImageWidth", $place_image_variant_width);
     899    $doc_obj->add_metadata($self->{'section'}, $place_image_variant_size . "ImageHeight", $place_image_variant_height);
     900    $doc_obj->add_metadata($self->{'section'}, $place_image_variant_size . "ImagePath", "_httpcollection_/index/assoc/[parent(Top):assocfilepath]/[${place_image_variant_size}Image]");
    895901}
    896902
Note: See TracChangeset for help on using the changeset viewer.