Skip to content

Commit

Permalink
Item13756: fixed rewrite of html img tags
Browse files Browse the repository at this point in the history
also:
- don't auto-render svg to png anymore
- extract only the first frame of tiff now, as we did for movies and animated gifs already
  • Loading branch information
MichaelDaum committed Sep 26, 2015
1 parent cd66e6e commit ad1bb2d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 27 deletions.
35 changes: 21 additions & 14 deletions data/System/ImagePlugin.txt
@@ -1,5 +1,7 @@
%META:TOPICINFO{author="ProjectContributor" date="1437152721" format="1.1" version="1"}%
---+!! <nop>%TOPIC%
%FORMFIELD{"Description"}%

%TOC%

Use this plugin to control the display and alignment of images using an easy
Expand Down Expand Up @@ -215,18 +217,17 @@ By default Image::Magick is used as it is the more common extension for perlbase
$Foswiki::cfg{ImagePlugin}{Mode} = 'Image::Magick';
</verbatim>

---++ Plugin Installation Instructions
---++ Installation Instructions
%$INSTALL_INSTRUCTIONS%

---++ Plugin Info
<!--
* Set SHORTDESCRIPTION = %$SHORTDESCRIPTION%
-->
| Plugin Author: | Craig Meyer, Foswiki:Main.MichaelDaum |
| Copyright: | &copy; 2006, Craig Meyer, %BR% &copy; 2006-2015 Michael Daum http://michaeldaumconsulting.com |
| License: | GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]]) |
| Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
---++ Dependencies
%$DEPENDENCIES%

---++ Change History

%TABLE{columnwidths="7em" tablewidth="100%"}%
| 26 Sep 2015 | fixed rewrite of html img tags to bail out when width/height units aren't px values; \
don't auto-render svg to png anymore |
| 17 Jul 2015 | optionally extract and convert inline svg to real images; \
optionally process html img tags and downscale them on the server; \
add support for Foswiki-2.x |
Expand Down Expand Up @@ -300,10 +301,16 @@ $Foswiki::cfg{ImagePlugin}{Mode} = 'Image::Magick';
| 17 Aug 2006: | width and height arguments had no effect; floating images not clearing proberly on IE |
| 16 June 2006: | released into the public |
| 10 May 2006: | Initial version by forking <nop>ImgPlugin |
| Dependencies: | %$DEPENDENCIES% |
| Home: | Foswiki:Extensions/%TOPIC% |
| Support: | Foswiki:Support/%TOPIC% |


%META:FILEATTACHMENT{name="WestminstpalaceSample.png" attr="h" comment="" date="1437152721" path="WestminstpalaceSample.png" size="107728" user="ProjectContributor" version="1"}%
%META:FILEATTACHMENT{name="logo.gif" attachment="logo.gif" attr="h" date="1437152721" path="http://www.google.com/intl/en_ALL/images/logo.gif" size="8558" user="ProjectContributor" version="1"}%
%META:FORM{name="PackageForm"}%
%META:FIELD{name="Author" title="Author" value="Craig Meyer, Michael Daum"}%
%META:FIELD{name="Copyright" title="Copyright" value="&copy; 2006, Craig Meyer, %BR% &copy; 2006-2015 Michael Daum http://michaeldaumconsulting.com"}%
%META:FIELD{name="Home" title="Home" value="Foswiki:Extensions/%TOPIC%"}%
%META:FIELD{name="License" title="License" value="GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]])"}%
%META:FIELD{name="Release" title="Release" value="%25$RELEASE%25"}%
%META:FIELD{name="Description" title="Release" value="%25$SHORTDESCRIPTION%25"}%
%META:FIELD{name="Repository" title="Repository" value="https://github.com/foswiki/ImagePlugin"}%
%META:FIELD{name="Support" title="Support" value="Foswiki:Support/%TOPIC%"}%
%META:FIELD{name="Version" title="Version" value="%25$VERSION%25"}%
17 changes: 11 additions & 6 deletions lib/Foswiki/Plugins/ImagePlugin.pm
Expand Up @@ -30,13 +30,12 @@ use warnings;
use Foswiki::Func ();
use Foswiki::Plugins ();

our $VERSION = '7.00';
our $RELEASE = '7.00';
our $VERSION = '7.01';
our $RELEASE = '26 Sep 2015';
our $NO_PREFS_IN_TOPIC = 1;
our $SHORTDESCRIPTION = 'Image and thumbnail services to display and alignment images using an easy syntax';
our $core;


###############################################################################
sub initPlugin {

Expand Down Expand Up @@ -88,10 +87,15 @@ sub getCore {
HERE

require Foswiki::Plugins::ImagePlugin::Core;
$core = new Foswiki::Plugins::ImagePlugin::Core(@_);
$core = Foswiki::Plugins::ImagePlugin::Core->new(@_);
return $core;
}

###############################################################################
sub finishPlugin {
$core->finishPlugin if defined $core;
}

###############################################################################
sub afterRenameHandler {

Expand Down Expand Up @@ -130,7 +134,7 @@ sub commonTagsHandler {
$text =~ s/(^|(?<!url)[-*\s(|])
(https?:
([^\s<>"]+[^\s*.,!?;:)<|][^\s]*\.(?:gif|jpe?g|png|bmp|svg)(?:\?.*)?(?=[^\w\-])))/
renderExternalImage($web, $topic, $1, $2)/geox;
renderExternalImage($web, $topic, $1, $2)/gieox;

}

Expand Down Expand Up @@ -195,6 +199,8 @@ sub renderLocalImage {
#print STDERR "not a local image: $val\n";
return $text;
}
} elsif (($attr eq 'width' || $attr eq 'height') && $val !~ /^\d+(px)?$/) {
return $text; # this is not a px unit ... keep the img as it is
} else {
push @args, "$attr=\"$val\"";
}
Expand All @@ -208,7 +214,6 @@ sub renderLocalImage {
my $result = "%IMAGE{\"$file\" ".join(" ", @args)."}%";

#print STDERR "result=$result\n";
#print STDERR "text=$text\n";

return $result;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/Foswiki/Plugins/ImagePlugin/Config.spec
Expand Up @@ -10,6 +10,10 @@ $Foswiki::cfg{ImagePlugin}{RenderExternalImageLinks} = 1;
# Activate this flag to process HTML <code>img</code> markup to local images
$Foswiki::cfg{ImagePlugin}{RenderLocalImages} = 1;

# **BOOLEAN**
# Activate this flag to convert inline-svg to png
$Foswiki::cfg{ImagePlugin}{ConvertInlineSVG} = 0;

# **BOOLEAN**
# Turn on/off attaching a mirrored image to the current topic. If switched on
# an attachment-record is generated for this image in addition to
Expand Down
28 changes: 21 additions & 7 deletions lib/Foswiki/Plugins/ImagePlugin/Core.pm
Expand Up @@ -88,15 +88,26 @@ sub new {
|| $Foswiki::cfg{ImageGalleryPlugin}{Impl}
|| 'Image::Magick';

#writeDebug("creating new image mage using $impl");
eval "use $impl";

writeDebug("creating new image mage using $impl");
eval "require $impl";
die $@ if $@;
$this->{mage} = new $impl;
#writeDebug("done");

return $this;
}

###############################################################################
sub finishPlugin {
my $this = shift;

$this->{mage} = undef;
$this->{ua} = undef;
$this->{types} = undef;
$this->{imageplugin} = undef;
}

###############################################################################
sub handleREST {
my ($this, $subject, $verb, $response) = @_;
Expand Down Expand Up @@ -242,6 +253,9 @@ sub handleIMAGE {
$params->{size} = $3 ? "$1x$3" : $1;
}

$params->{height} =~ s/px$//;
$params->{width} =~ s/px$//;

my $imgWeb = $params->{web} || $theWeb;
my $imgTopic;
my $imgPath;
Expand Down Expand Up @@ -543,7 +557,7 @@ sub processImage {
} elsif (defined $params->{frame}) {
$frame = $params->{frame};
} else {
$frame = '0' if $imgInfo{origImgPath} =~ /\.gif|mp4|mov|m4v|mpeg|mpg|wmv$/i;
$frame = '0' if $imgInfo{origImgPath} =~ /\.(gif|mp4|mov|m4v|mpeg|mpg|wmv|tiff?)$/i;
}
if (defined $frame) {
$frame =~ s/^.*?(\d+).*$/$1/g;
Expand Down Expand Up @@ -670,7 +684,7 @@ sub processImage {
}

# write
writeDebug("write");
writeDebug("writing to $imgInfo{imgPath}");
$error = $this->{mage}->Write($imgInfo{imgPath});
if ($error =~ /(\d+)/) {
$this->{errorMsg} .= " $error";
Expand Down Expand Up @@ -946,7 +960,7 @@ sub urlEncode {
my $text = shift;

$text = Encode::encode_utf8($text) if $Foswiki::UNICODE;
$text =~ s/([^0-9a-zA-Z-_.:~!*'\/])/'%'.sprintf('%02x',ord($1))/ge;
$text =~ s/([^0-9a-zA-Z-_.:~!*'\/])/sprintf('%%%02x',ord($1))/ge;

return $text;
}
Expand Down Expand Up @@ -1052,7 +1066,7 @@ sub getImageFile {
my $digest = Digest::MD5::md5_hex($size, $zoom, $crop, $rotate, $frame, $fileSize);

# force conversion of some non-webby image formats
$imgFile =~ s/\.(svgz?|tiff?|xcf|psd)$/\.png/g;
$imgFile =~ s/\.(tiff?|xcf|psd)$/\.png/g;

# switch manually specified output format
if ($output && $imgFile =~ /^(.+)\.([^\.]+)$/) {
Expand Down Expand Up @@ -1198,7 +1212,7 @@ sub sanitizeAttachmentName {

$fileName =~ s{[\\/]+$}{}; # Get rid of trailing slash/backslash (unlikely)
$fileName =~ s!^.*[\\/]!!; # Get rid of leading directory components
$fileName =~ s/[\*?~^\$@%`"'&;|<>\[\]#\x00-\x1f]//g; # Get rid of a subset of Namefilter
$fileName =~ s/[\\\\\*?~^\$@%`"'&;|<>\[\]#\x00-\x1f]//g; # Get rid of a subset of Namefilter

return Foswiki::Sandbox::untaintUnchecked($fileName);
}
Expand Down

0 comments on commit ad1bb2d

Please sign in to comment.