Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add image file check mechanism to verify file type before passing to
mogrify
  • Loading branch information
perusiongreg committed May 5, 2016
1 parent 672b8a1 commit 68d3439
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions code/SystemTag/image.tag
Expand Up @@ -9,13 +9,15 @@ UserTag image Order src
UserTag image AttrAlias geometry makesize
UserTag image AttrAlias resize makesize
UserTag image AddAttr
UserTag image Version 1.26
UserTag image Version 1.27
UserTag image Routine <<EOR
sub {
my ($src, $opt) = @_;
my ($image, $path, $secure, $sku);
my ($imagedircurrent, $imagedir, $imagedirsecure);

use Image::Size;

my @descriptionfields = grep /\S/, split /\s+/,
$opt->{descriptionfields} || $::Variable->{DESCRIPTIONFIELDS} || $Vend::Cfg->{DescriptionField};
@descriptionfields = qw( description ) if ! @descriptionfields;
Expand All @@ -28,6 +30,22 @@ sub {
my $filere = qr/\.\w{2,4}$/;
my $absurlre = qr!^(?i:https?)://!;

my $verify_image = sub {
my $file = shift;

return unless -f $file;

my ($imgx, $imgy, $error) = imgsize($file);

if(! $imgx) {
::logError("Image::Size error on verify-image: $error");
return undef;
}

return 1 if ($error =~ /(jpg|jpeg|gif|png|bmp|tif|ico|xbm)/i);
return undef;
};

if ($opt->{ui}) {
# unless no image dir specified, add locale string
my $locale = $Scratch->{mv_locale} ? $Scratch->{mv_locale} : 'en_US';
Expand Down Expand Up @@ -222,6 +240,12 @@ sub {
}
}
last MOGIT unless $exec;

unless ($verify_image->($newpath)){
logError("Image file not valid image:%s", $newpath);
last MOGIT;
}

system qq{$exec -geometry "$siz" '$newpath'};
if($?) {
logError("%s: Unable to mogrify image '%s'", 'image tag', $newpath);
Expand All @@ -241,8 +265,7 @@ sub {

if ($opt->{getsize} and $path) {
eval {
require Image::Size;
my ($width, $height) = Image::Size::imgsize($path);
my ($width, $height) = imgsize($path);
$opt->{height} = $height
if defined($height) and not exists($opt->{height});
$opt->{width} = $width
Expand Down

0 comments on commit 68d3439

Please sign in to comment.