TABLE OF CONTENTS
- 1. Image/calc_hash
- 2. Image/create_localcopy
- 3. Image/create_thumbnail
- 4. Image/get_filename_and_ext
- 5. Image/handle_image
- 6. Image/record_exists
- 7. Image/record_image
Image/calc_hash [ Functions ]
FUNCTION
Calculates hash from imagedata using MD5.
INPUTS
imagedata -- raw imagedata
RESULT
The calculated hash.
SOURCE
sub Image::calc_hash { my $imagedata = shift; my $md5 = Digest::MD5->new; $md5->add($imagedata); my $hash = $md5->hexdigest; return $hash; }
Image/create_localcopy [ Functions ]
FUNCTION
Stores image with unique filename on filesystem.
INPUTS
imagedir -- directory where local copy of images are stored hash -- MD5 hash of image ext -- extension of image, e.g. "png" imagedata -- raw imagedata
RESULT
Filesize of local copy.
SOURCE
sub Image::create_localcopy { my $imagedir = shift; my $hash = shift; my $ext = shift; my $imagedata = shift; my $filename = "$hash.$ext"; # save localcopy of image open IMG, ">$imagedir/$filename"; binmode IMG; print IMG $imagedata; close IMG; # return filesize my $st = stat("$imagedir/$filename"); return $st->size; }
Image/create_thumbnail [ Functions ]
FUNCTION
Create a thumbnail version of the local copy.
INPUTS
imagedir -- directory where local copy of images are stored hash -- MD5 hash of image ext -- extension of image, e.g. "png"
RESULT
orig_width -- original width of the image orig_height -- original height of the image
NOTES
Candidate for a rewrite, should not return anything.
SOURCE
sub Image::create_thumbnail { my $imagedir = shift; my $hash = shift; my $ext = shift; my $filename = "$hash.$ext"; # load localcopy of image my $image = GD::Image->new("$imagedir/$filename"); if (!$image) {return;} $image->trueColor(1); my $orig_width = $image->width; my $orig_height = $image->height; # render thumbnail my $thumb = $image->thumb( { side => 128, small => 1}, 1); # save thumbnail open THUMB, ">$imagedir/thumbs/$hash.jpg"; binmode THUMB; print THUMB $thumb->jpeg(85); close THUMB; return ($orig_width, $orig_height); }
Image/get_filename_and_ext [ Functions ]
FUNCTION
Returns last part of url as filename.
INPUTS
url -- URL of image, e.g. "http://www.irc-collective.org/bar.png"
RESULT
filename -- filename of image, e.g. "bar" ext -- extension of image, e.g. "png"
SOURCE
sub Image::get_filename_and_ext { my $url = shift; my $uri = URI->new($url); my $path = $uri->path; my $path_length = length($path); my $rindex_slash = rindex($path, "/"); my $rindex_dot = rindex($path, "."); my $filename = substr($path, $rindex_slash + 1, $path_length - $rindex_slash); my $ext = substr($path, $rindex_dot + 1, $path_length - $rindex_dot); return ($filename, $ext); }
Image/handle_image [ Functions ]
FUNCTION
Wrapper function to create local copy and thumbnail of image.
INPUTS
imagedir -- directory where local copy of images are stored dbh -- database handle year -- year in YYYY format, e.g. "2007" month -- month in MM format, e.g. "08" day -- day in DD format, e.g. "13" line_no -- line number in logfile, e.g. "210" time -- time in HH:MM[:SS] format, e.g. "14:48" nick -- nickname, e.g. "rjm" url -- URL of image, e.g. "http://www.irc-collective.org/bar.png" ct -- content type, e.g. "image/jpeg"
SOURCE
sub Image::handle_image { my $imagedir = shift; my $dbh = shift; my ($year, $month, $day, $line_no, $time, $nick, $url, $ct) = @_; my $date = "$year$month$day"; # fetch image my $ua = LWP::UserAgent->new( agent => "irc-collective/0.1.4" ); my $response = $ua->get($url); my $imagedata = $response->content(); # todo: add to debug info # my $length = $response->content_length(); # my $ct = $response->content_type(); # my $lm = $response->last_modified(); # create hash for unique filename my $hash = Image::calc_hash($imagedata); # skip if image is already recorded return if Image::record_exists($dbh, "$year$month$day", $line_no); # extract filename and fileext from URL my ($filename, $ext) = Image::get_filename_and_ext($url); # save image to filesystem for localcopy my $filesize = Image::create_localcopy($imagedir, $hash, $ext, $imagedata); # create thumb for image gallery my ($orig_width, $orig_height) = Image::create_thumbnail($imagedir, $hash, $ext); # add to images table my $result = Image::record_image($dbh, $url, $hash, $orig_width, $orig_height, $filename, $filesize, $ext, $ct, $date, $line_no, $time, $nick); if ($result > 0) { Debug::print("Added new image ($filename)."); } }
Image/record_exists [ Functions ]
FUNCTION
Checks if record is already present.
INPUTS
dbh -- database handle date -- date in YYYYMMDD format, e.g. "20070813" line_no -- line number in logfile, e.g. "210"
RESULT
True if record was found, false otherwise.
SOURCE
sub Image::record_exists { my $dbh = shift; my $date = shift; my $line_no = shift; my $sth = $dbh->prepare(qq[SELECT imageid FROM images WHERE from_date = ? AND from_lineno = ? LIMIT 1]) or die "ERROR: Could not prepare statement: ", $dbh->errstr, "\n"; $sth->execute($date, $line_no) or die "ERROR: Could not execute statement: ", $dbh->errstr, "\n"; if ($sth->rows) { Debug::print("Image $date:$line_no already present."); } return $sth->rows; }
Image/record_image [ Functions ]
FUNCTION
Records image to database.
INPUTS
dbh -- database handle url -- location of the image on the web hash -- MD5 hash of image width -- width of image in pixels height -- height of image in pixels filename -- filename of image filesize -- filesize of image fileext -- extension of image, e.g. "png" content_type -- content type of image, e.g. "image/png" from_date -- date of logfile where image was mentioned from_lineno -- line number where image was mentioned from_time -- time when image was mentioned from_nick -- who mentioned image
RESULT
True on success, false otherwise.
SOURCE
sub Image::record_image { my $dbh = shift; my $url = shift; my $hash = shift; my $width = shift; my $height = shift; my $filename = shift; my $filesize = shift; my $fileext = shift; my $content_type = shift; my $from_date = shift; my $from_lineno = shift; my $from_time = shift; my $from_nick = shift; my $sth = $dbh->prepare( qq[INSERT INTO images (url, hash, width, height, filename, filesize, fileext, content_type, from_date, from_lineno, from_time, from_nick) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)] ); $sth->execute( $url, $hash, $width, $height, $filename, $filesize, $fileext, $content_type, $from_date, $from_lineno, $from_time, $from_nick ) or print STDERR "ERROR: Could not add image to database: ", $dbh->errstr, "\n"; $sth->finish; return $sth->rows; }