XAO-ImageCache
view release on metacpan or search on metacpan
ImageCache.pm view on Meta::CPAN
=head1 NAME
XAO::ImageCache - Images caching by URLs stored in XAO::FS database
=head1 SYNOPSIS
use XAO::ImageCache;
# Making new instance of Image Cache object
my $image_Cache = XAO::ImageCache->new(
list => $odb->fetch("/Products"),
cache_path => "/var/httpd/shop/product/images/",
cache_url => "/products/images/",
source_url_key => "source_img",
) || die "Can't make Image cache!";
# Init new empty Cache
$image_cache->init() || die "Can't init new cache!";
# Start images checking and downloading to cache
$image_cache->check();
=head1 DESCRIPTION
When we store images links on own database we have no real
images on own site. Some time it may be a problem cause images may
have no right dimension or may be deleted from source site.
XAO::ImageCache made for cache locally images his URL stored
in XAO Founsation Server. Also, images may be resized automaticaly.
This module provide easy methods to scan XAO Foundation Server data
lists, to extract images source URLs from data objects, to download
images to local cache, to resize a local copy of the image to fit into
given dimensions and to store the new local URL of the image back to
the data object.
=head1 METHODS
=over
=cut
###############################################################################
package XAO::ImageCache;
use strict;
use Error qw(:try);
use XAO::Utils;
use XAO::FS;
use XAO::Errors qw(XAO::ImageCache);
use Digest::MD5 qw(md5 md5_hex md5_base64);
use LWP::UserAgent;
use URI;
use Image::Magick;
use Date::Manip;
use File::Path;
use File::Copy;
use vars qw($VERSION);
$VERSION='1.22';
###############################################################################
##
# Methods prototypes
#
sub new ($%);
sub init($);
sub check($);
sub download ($$);
sub scale_file($$$$;$);
sub scale_large($$$);
sub scale_thumbnail($$$);
sub remove_cache($);
# Special functions
sub get_filename($);
sub treat_filename($);
sub convert_time($);
sub throw($$);
###############################################################################
sub DESTROY {
my $self = shift;
dprint "----- XAO Image Cache finished -----";
}
###############################################################################
=item new($%)
The constructor returns a new C<XAO::ImageCache> object.
You can use it to make new images cache or check images
of already existent cache.
my $image_cache = XAO::ImageCache->new(
cache_path => "cache", # set cache directory to './cache/'
source_path => "cache/source", # set source directory to './cache/source/'
local_path => "images/copy", # (optional) try to resolve local urls
cache_url => "images/", # set cached images (relative) path to 'images/'
list => $odb->fetch("/Products"),
source_url_key => 'source_image_url',
dest_url_key => 'dest_image_url',
filename_key => 'product_id',
min_width => 50, # Source image is ignored if smaller
min_height => 50,
size => {
width => 320,
height => 200,
save_aspect_ratio => 1,
},
thumbnails => {
path => '/var/httpd/shop/product/images/tbn',
ImageCache.pm view on Meta::CPAN
return ($img_fnm, $thm_fnm);
}
###############################################################################
sub download_file {
my $self = shift;
my $source_url = shift;
my $source_file = shift;
dprint "DOWNLOAD ($source_url)->($source_file)";
my $response=$self->{'ua'}->get($source_url);
my $errstr;
if($response->is_success) {
if($response->content_type =~ /^image\//) {
open(F,"> $source_file.tmp") || $self->throw("- unable to save file '$source_file': $!");
binmode(F);
print F $response->content;
close(F);
rename("$source_file.tmp",$source_file);
}
else {
$errstr="downloaded '$source_url' is not an image (".$response->content_type.") (PERMANENT)";
}
}
else {
$errstr="can't download '$source_url' - ".$response->status_line." (NETWORK)";
}
return unless $errstr;
# If the file is already there (from previous downloads then just
# printing the error, but returning normally to let the file be scaled and
# stored as image/thumbnail
#
if(-f $source_file && !$self->{'clear_on_error'}) {
eprint "download_file: $errstr -- ignoring and keeping existing file";
return;
}
else {
$self->throw("- $errstr");
}
}
###############################################################################
sub scale_file ($$$$;$) {
my ($self,$infile,$outfile,$params,$label)=@_;
$label||='unknown';
if(!$params) {
dprint "Copying $infile to $outfile as is ($label)";
copy($infile,$outfile);
return;
}
my $geometry = ''; # image dimensions in ImageMagick geometry format
my $image = Image::Magick->new() || $self->throw("- Image::Magick creation failure!");
my $err = $image->ReadImage($infile);
### dprint ".source in '$infile'";
# Only throwing an error if no image was read. If we consider all
# warnings as errors then some 3M tiff files don't parse.
#
# From http://www.imagemagick.org/script/perl-magick.php:
#
# $x = $image->Read(...);
# warn "$x" if "$x"; # print the error message
# $x =~ /(\d+)/;
# print $1; # print the error number
# print 0+$x; # print the number of images read
#
### $self->throw("- parsing error ($err) (PERMANENT)") if $err;
(0 + $err)>0 ||
$self->throw("- parsing error ($err) (PERMANENT)");
# We only deal with image/* types -- otherwise ImageMagick can
# sometimes successfully open and convert HTML or text messages into
# images.
#
$image->Get('mime')=~/^image\// || $self->throw("- not an image file '$infile' (".$image->Get('mime').")");
# Get source image dimensions
#
my ($src_width, $src_height) = $image->Get('columns','rows');
my $min_width=$self->{'min_width'} || 10;
my $min_height=$self->{'min_height'} || $min_width;
if($src_height<=1 || $src_width<=1 || ($src_height<$min_height && $src_width<$min_width)) {
$self->throw("- image ($src_width,$src_height) is smaller than the minimum ($min_width,$min_height) for '$label' (PERMANENT)");
}
# Getting target image width and height
#
if($params->{'geometry'}){
$geometry = $params->{geometry}; # size was set as geometry string
}
elsif($params->{'save_aspect_ratio'}) {
my $src_aspect=$src_width/$src_height;
my $target_width=$params->{'width'} || $src_width;
my $target_height=$params->{'height'} || $src_height;
my $target_aspect=$target_width/$target_height;
my ($width,$height);
if($src_width<=$target_width && $src_height<=$target_height) {
if(lc($image->Get('mime')) eq 'image/jpeg' && ($image->get('colorspace') || '') =~ /^s?RGB$/i) {
copy($infile,$outfile);
dprint "..copied ${src_width}x${src_height} as is for '$label' (jpeg, fits into ${target_width}x${target_height})";
return;
}
else {
$width=$src_width;
$height=$src_height;
}
}
ImageCache.pm view on Meta::CPAN
=over
=item cache_path
- Path string where the cache should be placed.
May be absolute or relative from current execution directory path.
For example. Set it to C<./cache> if you whant to place cache in
C<cache> subdirectory of your script working directory.
=item cache_url
- complet URL (or relative location) to cached images.
Place here your URL reflection of cache directory in condition with
your HTTP server configuration.
For example. Set it to C<http://my.host.com/images/> if your HTTP
server configured for provide access to your cache directory by
hostname C<my.host.com> and location C<images/>. Cached images names
will be added to image URL automaticaly.
=item list
- reference to C<XAO::DO::FS::List> object containing the data objects
with Image source URL
Meaning, your data look like a XAO Foundation Server list of objects
with references to images. This parameter should contain reference to
to XAO::DO::FS::List object. This reference may be result of
XAO::Objects->fetch() methode.
XAO::ImageCache will process each record of this list.
=item source_url_key
- data key containing the URL of source image.
Contain the name of key of data object containing the source image reference.
=back
=head2 Optional parameters
=over
=item dest_url_key
- data key for storing URL of image in cache.
Optional parameter cause image name in cache will be a MD5 Base64
digest of source image path where C<=> character removed, C<\> and C<+>
translated to C<_> and C<-> simultaniosely.
To get cached image name
=item size
- Prefered image size may set as C<geometry> equal to C<geometry> parameter
of Image::Magick module to pass it dirrectly to Image::Magick Scale function.
Other way to set the image size is set a width and height keys to preffered
values.
If one of image dimension is not defined then corresponding parameter of
original image will be used.
This way, image will be resized with same aspect ratio (same proportions) to
the original image if C<save_aspect_ratio> parameter present.
Image width and height will be resized exactly to given size if
C<save_aspect_ratio> parameter not present.
Parameter C<geometry> has higher priority and other parameters has no effects
if C<geometry> peresent.
For example.
# Size 320x200 as geometry settings
%params = (size => {geometry => "320x200!"} );
# Size 320x200 as dimensions settings
%params = (size => {width => 320, height => 200} );
# Fit size into 320x200 with saving image proportions
%params = (
size => {
width => 320,
height => 200,
save_aspect_ratio => 1,
}
);
=item autocreate
- create or check cache content automaticaly.
If non zero value present, cache directory will be created
and images checking will be runned. Otherwithe you should run
init() and check() methodes manualy.
Existent cache directory will not be removed. You may do it
manualy using remove_cache() methode.
=item force_download
- each image should be reloaded to cache and processed without
dependance of source image modification time. Any conditions
ignored.
=item thumbnail
- thumbnails creator configuration
Some thubnails configuration parameters may be set for
automatic thumbnails creation. This parameter should contain
the reference to hash with thumbnails configuration parameters.
Only C<path> parameter is required. Other parameters are
optional.
ImageCache.pm view on Meta::CPAN
=item url
URL for access to thumbnails directory. Same way as C<cache_url>.
=item url_key
Data object key name where thumbnail URL should be stored.
=item geometry
Geometry string to set thumbnail images size in Image Magick geometry
format. May be set as dimension ("320x200!") or as persent of actual
size of cached image ("25%").
Default value is "50%" the half of actual image size.
=back
=item useragent
- configuration parameters hash for LWP::UserAgent
=over
=item agent
Default value C<XAO-ImageCache/#.##>
=item env_proxy
Default value 1
=item keep_alive
Default value 1
=item timeout
Default value 30
=back
For more information please follow to L<LWP::UserAgent>
=back
=head1 SEE ALSO
Specifics of List API can be found in
L<XAO::DO::FS::List>.
For additional information please see
L<XAO::DO::FS::Glue>,
L<XAO::DO::FS::Global>,
L<XAO::DO::FS::Glue::MySQL_DBI>,
Refer to L<Image::Magick> documentation for additional
information about setting of image scaling parameters.
Refer to L<LWP::UserAgent> documentation for additional
information about user agent parameters.
=head1 BUGS
Please, inform me about found bugs.
=head1 AUTHORS
The XAO::ImageCache package maintained by
Konstantin Safronov <skv@xao.com>. Specification by
Andrew Maltsew <am@xao.com>
=cut
( run in 0.752 second using v1.01-cache-2.11-cpan-39bf76dae61 )