1 package CXGN
::Insitu
::Image
;
5 Image.pm - a class to deal with insitu images.
9 This class provides database access and store functions as well as image upload and certain image manipulation functions, such as image file type conversion and image resizing; and functions to associate tags with the image.
13 Lukas Mueller (lam87@cornell.edu)
15 =head1 MEMBER FUNCTIONS
17 The following functions are provided in this class:
25 use CXGN
::Insitu
::Tag
;
28 use base qw
/ CXGN::Insitu::DB / ;
46 my $self = $class ->SUPER::new
($dbh);
48 $self->set_configuration_object(SGN
::Context
->new());
51 $self->set_image_id($id);
59 my $query = "SELECT experiment_id,
66 WHERE insitu.image.image_id=?";
68 my $sth = $self->get_dbh()->prepare($query);
69 $sth->execute($self->get_image_id()) ;
71 my ($experiment_id, $name, $description, $filename, $file_ext, $user_id) =
72 $sth->fetchrow_array();
74 #print STDERR "fetched image: image_id ".($self->get_image_id())." experiment_id = $experiment_id.\n";
76 $self->set_experiment_id($experiment_id);
77 $self->set_name($name);
78 $self->set_description($description);
79 $self->set_filename($filename);
80 $self->set_file_ext($file_ext);
81 $self->set_user_id($user_id);
87 if ($self->get_image_id()) {
91 my $query = "UPDATE insitu.image SET
98 WHERE insitu.image.image_id=?";
100 my $sth = $self->get_dbh()->prepare($query);
103 $self->get_experiment_id(),
105 $self->get_description(),
106 $self->get_filename(),
107 $self->get_file_ext(),
108 $self->get_user_id(),
109 $self->get_image_id()
111 return $self->get_image_id();
117 my $query = "INSERT INTO insitu.image (
124 VALUES (?, ?, ?, ?, ?, ?)";
125 my $sth = $self->get_dbh()->prepare($query);
127 $self->get_experiment_id(),
129 $self->get_description(),
130 $self->get_filename(),
131 $self->get_file_ext(),
135 $self->set_image_id($self->get_dbh()->last_insert_id("image"));
136 return $self->get_image_id();
153 if ($self->get_image_id()) {
154 my $query = "UPDATE insitu.image set obsolete='t' WHERE image_id=?";
155 my $sth = $self->get_dbh()->prepare($query);
156 $sth->execute($self->get_image_id());
159 print STDERR
"Image.pm: Trying to delete an image from the db that has not yet been stored.";
180 return $self->{image_id
};
197 $self->{image_id
}=shift;
200 =head2 get_experiment_id
211 sub get_experiment_id
{
213 return $self->{experiment_id
};
217 =head2 set_experiment_id
228 sub set_experiment_id
{
230 $self->{experiment_id
}=shift;
246 return $self->{name
};
266 =head2 get_description
277 sub get_description
{
279 return $self->{description
};
283 =head2 set_description
294 sub set_description
{
296 $self->{description
}=shift;
313 return $self->{user_id
};
330 $self->{user_id
}=shift;
348 return $self->{filename
};
364 $self->{filename
}=shift;
380 return $self->{file_ext
};
397 $self->{file_ext
}=shift;
403 Desc: adds a tag to the image
405 Args: a tag object (CXGN::Insitu::Tag).
406 Side Effects: the tag is immediately store in the database.
407 there is no need to call store() on the image object.
416 my $query = "INSERT INTO insitu.image_tag (tag_id, image_id) values (?, ?)";
418 my $sth = $self->get_dbh()->prepare($query);
419 $sth->execute($tag->get_tag_id(), $self->get_image_id());
425 Usage: my @tags = $image->get_tags();
426 Desc: gets all the tags associated with this image object
429 Side Effects: the tags are being fetched from the database. The image
430 object does not 'buffer' tag associations (see also add_tag()).
438 my $query = "SELECT tag_id FROM insitu.image_tag WHERE image_id=?";
439 my $sth = $self->get_dbh()->prepare($query);
440 $sth->execute($self->get_image_id());
442 while (my ($tag_id) = $sth->fetchrow_array()) {
443 push @tags, CXGN
::Insitu
::Tag
->new($self->get_dbh(), $tag_id);
454 Side Effects: the association to the tag object will be removed
455 directly accessing the database backstore. There is no
456 need to call store() after remove_tag(). The tag itself
465 my $query = "DELETE FROM insitu.image_tag WHERE tag_id=? and image_id=?";
466 my $sth = $self->get_dbh()->prepare($query);
467 $sth->execute($tag->get_tag_id(), $self->get_image_id());
470 =head2 get_large_suffix
481 sub get_large_suffix
{
486 =head2 get_thumb_suffix
497 sub get_thumb_suffix
{
504 =head2 get_large_size
520 =head2 get_thumb_size
538 =head2 get_fullsize_dir
549 sub get_fullsize_dir
{
551 my $fullsize_dir = $self->get_configuration_object()->get_conf("insitu_fullsize_dir");
552 $fullsize_dir =~ s
|/+$||;
553 return $fullsize_dir."/".$self->get_experiment_id();
557 =head2 get_display_dir
565 To do: should get info through conf object
569 sub get_display_dir
{
571 # directory this script will move shrunken images to
572 my $display_dir = SGN
::Context
->new->get_conf("insitu_display_dir");
573 $display_dir =~ s
|/+$||;
574 return $display_dir."/".$self->get_experiment_id();
585 To do: should get info through conf object
590 my $input_dir = SGN
::Context
->new->get_conf("insitu_input_dir");
591 $input_dir =~ s
|/+$||;
596 =head2 get_thumbnail_url
607 sub get_thumbnail_url
{
609 return $self->get_configuration_object()->get_conf("insitu_display_url")."/".$self->get_experiment_id()."/".$self->get_filename()."_".$self->get_large_suffix().".jpg";
613 =head2 get_fullsize_url
624 sub get_fullsize_url
{
626 return $self->get_configuration_object()->get_conf("insitu_fullsize_url")."/".$self->get_experiment_id()."/".$self->get_filename().$self->get_file_ext();
630 =head2 get_configuration_object
641 sub get_configuration_object
{
643 return $self->{configuration_object
};
646 =head2 set_configuration_object
657 sub set_configuration_object
{
659 $self->{configuration_object
}=shift;
664 =head2 get_img_src_tag
675 sub get_img_src_tag
{
677 return "<img src=\"".$self->get_thumbnail_url()."\" border=\"0\" width=".$self->get_thumb_size()." /> ";
680 =head2 get_temp_filename
691 sub get_temp_filename
{
693 return $self->{temp_filename
};
697 =head2 set_temp_filename
708 sub set_temp_filename
{
710 $self->{temp_filename
}=shift;
715 my $experiment = shift;
716 my $uploaded_filename=shift;
717 my $uploaded_filehandle = shift;
721 # if an image file has been uploaded, copy it to a temporary
723 # get remote file name, make it safe, keep it sane
725 $uploaded_filename =~ s/.*[\/\\](.*)/$1/;
727 # generate local file name, including IP and time, to make sure
728 # multiple uploads don't clobber each other
730 #my $date = `strftime "\%Y-\%m-\%d", gmtime`;
731 my $create_time = time();
732 $uploaded_filename = $self->get_input_dir()."/" . $ENV{REMOTE_ADDR
} . "_${create_time}_${uploaded_filename}";
733 warn "Uploaded_filename=$uploaded_filename\n";
735 $self->set_temp_filename($uploaded_filename);
737 #my $uploaded_filehandle = $query->upload('e_file');
739 # only copy file if it doesn't already exist
741 if (!-e
$uploaded_filename) {
743 # open a filehandle for the uploaded file
745 if (!$uploaded_filehandle) {
749 # copy said file to destination, line by line
750 warn "Now uploading file...\n";
751 open UPLOADFILE
, ">$uploaded_filename" or die "Could not write to ${uploaded_filename}: $!\n";
752 warn "could open filename...\n";
754 while (<$uploaded_filehandle>) {
755 #warn "Read another chunk...\n";
759 warn "Done uploading...\n";
763 print STDERR
"$uploaded_filename exists, not overwriting...\n";
780 my $file_name = shift;
781 my $experiment = shift;
783 my $experiment_id=$experiment->get_experiment_id();
784 if (!$experiment_id) { die "Need an experiment id!\n"; }
786 # create subdirectories for these images to live in
788 my $fullsize_dir = $self->get_fullsize_dir();
789 my $display_dir = $self->get_display_dir();
791 # these commands shouldn't do any harm if these directories already exist
793 if (! -d
'$fullsize_dir' ) {
794 system("mkdir '$fullsize_dir'" );
795 system("chmod 775 '$fullsize_dir'");
798 if (! -d
'$display_dir') {
799 system("mkdir '$display_dir'");
800 system("chmod 775 '$display_dir'");
806 my ($safe_file, $safe_ext, $unix_file);
807 $safe_file = $file_name;
808 $safe_file =~ m/(.*)(\.[a-zA-Z0-9]{3,4})$/i;
811 $unix_file = $safe_file;
812 $unix_file =~ s/\s/_/g;
814 my $input_dir = $self->get_input_dir();
815 my $large_suffix = $self->get_large_suffix();
816 my $thumb_suffix = $self->get_thumb_suffix();
817 my $large_size = $self->get_large_size();
818 my $thumb_size = $self->get_thumb_size();
819 # copy unmodified image to be fullsize image
821 my $temp_filename = $self->get_temp_filename();
822 my $mv = "mv '$temp_filename' '${fullsize_dir}/${unix_file}${safe_ext}'";
823 print STDERR
"MOVING FILE $mv\n";
825 my $chmod = "chmod 664 '${fullsize_dir}/${unix_file}${safe_ext}'";
826 print STDERR
"CHMODing FILE: $chmod\n";
829 # convert to jpg if format is different
830 if ($safe_ext !~ /jpg|jpeg/i) {
831 system("convert ${fullsize_dir}/${unix_file}${safe_ext} ${fullsize_dir}/${unix_file}.jpg");
835 # create small thumbnail for each image
836 $self->copy_image_resize("${fullsize_dir}/${unix_file}${safe_ext}", "${display_dir}/${unix_file}_${thumb_suffix}.jpg", "$thumb_size");
838 # create midsize image for each image
839 $self->copy_image_resize("${fullsize_dir}/${unix_file}${safe_ext}", "${display_dir}/${unix_file}_${large_suffix}.jpg", "$large_size");
841 # enter preliminary image data into database
842 #$tag_table->insert_image($experiment_id, $unix_file, ${safe_ext});
843 $self -> set_filename
($unix_file);
844 $self -> set_file_ext
($safe_ext);
849 sub copy_image_resize
{
851 my ($original_image, $new_image, $width) = @_;
853 #$debug and warn "\tCopying $original_image to $new_image and resizing it to $width px wide\n";
855 # first copy the file
856 my $copy = "cp '$original_image' '$new_image'";
857 print STDERR
"COPYING: $copy\n";
859 my $chmod = "chmod 664 '$new_image'";
860 print STDERR
"CHMODing: $chmod\n";
863 # now resize the new file, and ensure it is a jpeg
864 my $resize = `mogrify -geometry $width '$new_image'`;
865 my $jpeg = `mogrify -format jpg '$new_image'`;
867 if ($resize || $jpeg) {
880 # my $image_id = $self->get_image_id();
881 # my $experiment_id = $self->get_experiment_id();
882 # my $filename = $self->get_filename();
883 # my $description = $self->get_description();
884 # my $name = $self->get_name();
885 # my $file_ext= $self->get_file_ext();
886 # my $large_suffix = $self->get_large_suffix();
887 # my $large_size = $self->get_large_size();
888 # my @tags = $self->get_tags();
889 # my $categories = "CATEGORIES GO HERE";
892 # my $thumbnail = $self->get_display_dir()."/thumbnail_images/$experiment_id/$filename.jpg";
893 # my $fullsize = $self->get_fullsize_dir()."/fullsize_images/$experiment_id/$filename$file_ext";
898 # <a href="/thumbnail_images/$experiment_id/$filename.jpg" onclick="javascript: window.open('/fullsize_images/$experiment_id/$filename$file_ext', 'blank', 'toolbar=no'); return false;"><img src="/thumbnail_images/$experiment_id/$filename\_$large_suffix.jpg" border="0" width="$large_size" alt="image id: $image_id" /></a><br /><em>$filename</em></center>
905 # $output .= "<hr noshade=\"noshade\" />\n\n";
907 # # generate table showing additional information for this image
908 # if ($name || $description || @tags>0) {
909 # $output .= <<IMAGE_INFO;
910 # <center><table border="0" cellpadding="0" cellspacing="0" width="90%">
912 # <th class="fielddef" style="text-align:center" colspan="2">Image Info</td>
916 # $output .= <<IMAGE_NAME;
918 # <td class="fielddef">Name</td>
919 # <td class="fieldinput">$name</td>
923 # if ($description) {
924 # $output .= <<IMAGE_DESC;
926 # <td class="fielddef">Description</td>
927 # <td class="fieldinput">$description</td>
932 # first make sure to kill any redundancy with the experiment tags
933 # my %expr_tags = $tag_table->return_relevant_tags("ex", $image{experiment_id});
934 # my %new_image_tags = ();
935 # foreach my $img_tag (keys %{$image{tags}}) {
936 # if (!$expr_tags{$img_tag}) {
937 # ($debug>1) and warn "setting tag $img_tag for image\n";
938 # $new_image_tags{$img_tag} = $image{tags}{$img_tag};
941 # ($debug>1) and warn "tag $img_tag is set for both experiment and image!\n";
944 # my $categories = get_tag_links(\%new_image_tags);
945 # (keys(%new_image_tags)>0) and $output .= <<IMAGE_TAGS;
949 # <td class="fielddef">Categories</td>
950 # <td class="fieldinput">$categories</td>
954 # $output .= "</table></center>\n";
955 # $output .= "<hr noshade=\"noshade\" />\n\n";
958 # # generate table showing information about this experiment
959 # #$output .= get_experiment_string($image{experiment_id});