source: trunk/gsdl/perllib/plugins/ImagePlug.pm@ 1733

Last change on this file since 1733 was 1733, checked in by say1, 23 years ago

new plugin for images

  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1###########################################################################
2#
3# ImagePlug.pm -- simple text plugin
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package ImagePlug;
27
28use BasPlug;
29
30sub BEGIN {
31 @ISA = ('BasPlug');
32}
33
34sub new {
35 my ($class) = @_;
36 my $self = new BasPlug ("ImagePlug", @_);
37
38 return bless $self, $class;
39}
40
41sub get_default_process_exp {
42 my $self = shift (@_);
43
44 return q^(?i)(\.jpe?g|\.gif|\.png|\.tiff?)$^;
45}
46
47sub run_convert {
48 my $self = shift (@_);
49 my $filename = shift (@_);
50 my $file = shift (@_);
51 my $doc_obj = shift (@_);
52
53 my $type = "unknown";
54
55 if (-e "$filename.thumbnail.gif") {
56 print "ImagePlug: both \"$filename\" and \"$filename.thumbnail.gif\" exist.\n";
57 return undef;
58 }
59
60 my $result = "";
61 $result = `convert -verbose -geometry 100x100 $filename $filename.thumbnail.gif` ;
62 my $section = $doc_obj->get_top_section();
63
64 if ($result =~ m/=>.* ([0-9]+)x([0-9]+)=>([0-9]+)x([0-9]+)/) {
65 $doc_obj->add_metadata ($section, "ImageHeight", $1);
66 $doc_obj->add_metadata ($section, "ImageWidth", $2);
67 $doc_obj->add_metadata ($section, "ThumbHeight", $3);
68 $doc_obj->add_metadata ($section, "ThumbWidth", $4);
69 }
70 if ($result =~ m/^[^\n]*GIF/) {
71 $type = "gif";
72 }
73 if ($result =~ m/^[^\n]*PNG/) {
74 $type = "png";
75 }
76 if ($result =~ m/^[^\n]*TIFF?/) {
77 $type = "tiff";
78 }
79
80 if ($type ne "unknown") {
81 $doc_obj->add_metadata ($section, "ImageType", $type);
82 }
83 $doc_obj->add_metadata ($section, "Image", "$file");
84 $doc_obj->add_metadata ($section, "Thumb", "$file.thumbnail.gif");
85
86 print $result;
87 return $type;
88}
89
90
91# The ImagePlug read() function. This function does all the right things
92# to make general options work for a given plugin. It calls the process()
93# function which does all the work specific to a plugin (like the old
94# read functions used to do). Most plugins should define their own
95# process() function and let this read() function keep control.
96#
97# ImagePlug overrides read() because there is no need to read the actual
98# text of the file in, because the contents of the file is not text...
99#
100# Return number of files processed, undef if can't process
101# Note that $base_dir might be "" and that $file might
102# include directories
103
104sub read {
105 my $self = shift (@_);
106 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
107
108 my $filename = &util::filename_cat($base_dir, $file);
109 return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
110 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
111 return undef;
112 }
113 my $plugin_name = ref ($self);
114 $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
115
116 # create a new document
117 my $doc_obj = new doc ($filename, "indexed_doc");
118
119 #run convert to get the thumbnail and extract size and type info
120 my $mimetype = run_convert($self, $filename, $file, $doc_obj);
121
122 #add the image as an associated file ...
123 if (-e "$filename.thumbnail.gif") {
124 $doc_obj->associate_file("$filename.thumbnail.gif","$file.thumbnail.gif","image/gif",$section);
125 } else {
126 print "ImagePlug: couldn't find \"$filename.thumbnail.gif\"\n";
127 }
128
129 if (defined $mimetype) {
130 $doc_obj->associate_file($filename,$file,"image/$mimetype",$section);
131 }
132
133 #create an empty text string so we don't break downstream plugins
134 my $text = "";
135
136 # include any metadata passed in from previous plugins
137 # note that this metadata is associated with the top level section
138 $self->extra_metadata ($doc_obj, $section, $metadata);
139
140 # do plugin specific processing of doc_obj
141 return undef unless defined ($self->process (\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj));
142
143 # do any automatic metadata extraction
144 $self->auto_extract_metadata ($doc_obj);
145
146 # add an OID
147 $doc_obj->set_OID();
148
149# $doc_obj->add_text($section, "<pre>\n$text\n</pre>");
150
151 # process the document
152 $processor->process($doc_obj);
153
154 if (-e "$filename.thumbnail.gif") {
155 util::rm("$filename.thumbnail.gif");
156 }
157
158 return 1; # processed the file
159}
160
161# do plugin specific processing of doc_obj
162sub process {
163 my $self = shift (@_);
164 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
165 my $outhandle = $self->{'outhandle'};
166
167 print $outhandle "ImagePlug: processing $file\n"
168 if $self->{'verbosity'} > 1;
169
170 my $cursection = $doc_obj->get_top_section();
171
172 # get title metadata
173 # (don't need to get title if it has been passed
174 # in from another plugin)
175 if (!defined $metadata->{'Title'}) {
176 my ($title) = $$textref =~ /^([^\n]*)/;
177 if (length($title) > 100) {
178 $title = substr ($title, 0, 100);
179 }
180 $doc_obj->add_utf8_metadata ($cursection, "Title", $title);
181 }
182
183 # insert preformat tags and add text to document object
184 $doc_obj->add_utf8_text($cursection, "\n$$textref\n");
185
186 return 1;
187}
188
1891;
190
191
192
193
194
195
196
197
198
199
200
Note: See TracBrowser for help on using the repository browser.