#!/usr/local/bin/perl5 # import images based on imput arg[0] # # # #use strict; use DBI; use Image::Size; use ImageHoard; my $file; my $dsn = 'NULL'; my $db_user_name = 'NULL'; my $db_password = 'NULL'; my $dbh = DBI->connect($dsn, $db_user_name, $db_password) || die $dbh->errstr; my $image = ImageHoard->new; $image->set_database_handle( $dbh ); $image->set_directory( "./" ); open INF, $ARGV[0] or die "\nCan't open $srcfile for reading: $!\n"; binmode INF; $file = ; my ( $imgX , $imgY ) = imgsize( $ARGV[0] ); $image->create_record( $file,"1",$ARGV[1],$ARGV[2],$imgX,$imgY,"72" ); print "done\n"; #### package ImageHoard; use DBI; use POSIX; # Manages meta data and directory of images. Images are stored on the local # file system. Someday it might store the data on a remote filesystem but # im not that advanced yet. Meta data is stored in the MYSQL database. # # METHODS # # new # creates a new object # # set database handle # sets the database handle sent to the object to it # can work with the tables it needs to. # # create_record # takes the binary data for the image as well as the associated # meta data # # delete_record # # # find_matching_records # takes in the metadata field and the value to match # returns a list of image id's that match # # keyword_search # takes in the keyword and the value to match # returns a list of image id's that match # # METADATA # ID The unique name of the image for the file system # Type The type of the image so that it can be displayed # ReferenceID the user who added it to the system # Name The name of the Image # ImgX # ImgY # ImgRes # sub new { my $self = {}; $self->{'dbh'} = undef; #error reporting and update vars $self->{error_type} = undef; $self->{error_html_string} = undef; bless ($self); return $self; } sub get_ID { my $a; my @results; my $self = shift; my $queryString = "SELECT ID FROM IMAGEHOARD_MetaData WHERE ID = \'$_[0]\'"; my $query = $dbh->prepare($queryString); $query->execute() || die $dbh->errstr; while( $a = $query->fetchrow_array() ) { push( @results, $a ); } return @results; } sub get_Type { my $a; my @results; my $self = shift; my $queryString = "SELECT Type FROM IMAGEHOARD_MetaData WHERE ID = \'$_[0]\'"; my $query = $dbh->prepare($queryString); $query->execute() || die $dbh->errstr; while( $a = $query->fetchrow_array() ) { push( @results, $a ); } return @results; } sub get_referenceID { my $a; my @results; my $self = shift; my $queryString = "SELECT ReferenceID FROM IMAGEHOARD_MetaData WHERE ID = \'$_[0]\'"; my $query = $dbh->prepare($queryString); $query->execute() || die $dbh->errstr; while( $a = $query->fetchrow_array() ) { push( @results, $a ); } return @results; } sub get_name { my $a; my @results; my $self = shift; my $queryString = "SELECT Name FROM IMAGEHOARD_MetaData WHERE ID = \'$_[0]\'"; my $query = $dbh->prepare($queryString); $query->execute() || die $dbh->errstr; while( $a = $query->fetchrow_array() ) { push( @results, $a ); } return @results; } sub get_imgx { my $a; my @results; my $self = shift; my $queryString = "SELECT ImgX FROM IMAGEHOARD_MetaData WHERE ID = \'$_[0]\'"; my $query = $dbh->prepare($queryString); $query->execute() || die $dbh->errstr; while( $a = $query->fetchrow_array() ) { push( @results, $a ); } return @results; } sub get_imgy { my $a; my @results; my $self = shift; my $queryString = "SELECT ImgY FROM IMAGEHOARD_MetaData WHERE ID = \'$_[0]\'"; my $query = $dbh->prepare($queryString); $query->execute() || die $dbh->errstr; while( $a = $query->fetchrow_array() ) { push( @results, $a ); } return @results; } sub get_imgres { my $a; my @results; my $self = shift; my $queryString = "SELECT IngRes FROM IMAGEHOARD_MetaData WHERE ID = \'$_[0]\'"; my $query = $dbh->prepare($queryString); $query->execute() || die $dbh->errstr; while( $a = $query->fetchrow_array() ) { push( @results, $a ); } return @results; } sub set_database_handle { my $self = shift; $self->{dbh} = $_[0]; } sub set_directory { my $self = shift; $self->{directory} = $_[0]; } # Args: Binary Data, ReferenceID, Name, Type, X, Y, Res # Ret : Image code out sub create_record { my $self = shift; my $file = shift; my @args; my $path; my $dbh = $self->{'dbh'}; my $unique = 'false'; my $currentStatecode; my $duplicateStatecode; while( $unique eq 'false' ) { $unique = 'true'; $currentStatecode = rand( 99999999999 ); $currentStatecode = floor( $currentStatecode ); my $sqlQuery = "SELECT ID FROM IMAGEHOARD_MetaData WHERE ID = $currentStatecode"; my $query = $dbh->prepare( $sqlQuery ); $query->execute() || die $dbh->errstr; if( $duplicateStatecode = $query->fetchrow_array() ) { $unique = 'false'; } } foreach ( @_ ) { push( @args, format_for_mysql( $_ )); } $dbh->do("INSERT into IMAGEHOARD_MetaData ( ID, ReferenceID, NAME, Type, ImgX, ImgY, IngRes ) values ($currentStatecode,$args[0],\'$args[1]\', \'$args[2]\', \'$args[3]\',\'$args[4]\',\'$args[5]\')") || die $dbh->errstr; $path = $self->{directory} . $currentStatecode . "." . $args[2]; print $path . "\n"; open OUTF, ">$path" or die "\nCan't open $destfile for writing: $!\n"; binmode OUTF; print OUTF $file; close OUTF or die "Can't close $destfile: $!\n"; return( $currentStatecode ); } sub add_keyword { my $self = shift; my $file = shift; my @args = @_; my $path; my $dbh = $self->{'dbh'}; foreach ( @_ ) { push( @args, format_for_mysql( $_ )); } $dbh->do("INSERT into IMAGEHOARD_keywords ( ID, ReferenceID, Keyword ) values ($args[0],$args[1], \'$args[2]\'") || die $dbh->errstr; return 1; } sub delete_record { } #args: metadata name, value to search for sub find_matching_records { my $a; my @results; my $self = shift; my $dbh = $self->{'dbh'}; my $queryString = "SELECT ID FROM IMAGEHOARD_MetaData WHERE $_[0] = \'$_[1]\'"; my $query = $dbh->prepare($queryString); $query->execute() || die $dbh->errstr; while( $a = $query->fetchrow_array() ) { push( @results, $a ); } return @results; } sub keyword_search { my $a; my @results; my $self = shift; my $queryString = "SELECT ID FROM IMAGEHOARD_keywords WHERE $_[0] = \'$_[1]\'"; my $query = $dbh->prepare($queryString); $query->execute() || die $dbh->errstr; while( $a = $query->fetchrow_array() ) { push( @results, $a ); } return @results; } sub format_for_mysql( $ ) { my $string = $_[0]; $string =~ s/\'/\\\'/g; $string =~ s/\"/\\\"/g; $string =~ s/\`/\\\`/g; $string =~ s/\;//g; $string =~ s/\n/ /g; # replace newlines with spaces $string =~ s/\r//g; # remove hard returns $string =~ s/\cM//g; # delete ^M's return( $string ); } 1;