Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Item13533: add support for foswiki-2.0
also:
- optionally extract and convert inline svg to real images
- optionally process html img tags and downscale them on the server
  • Loading branch information
MichaelDaum committed Jul 17, 2015
1 parent bfb6ecd commit cd66e6e
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 43 deletions.
8 changes: 4 additions & 4 deletions data/Sandbox/ImagePluginTest.txt
@@ -1,4 +1,4 @@
%META:TOPICINFO{author="ProjectContributor" comment="" date="1418735227" format="1.1" version="1"}%
%META:TOPICINFO{author="ProjectContributor" comment="" date="1437152721" format="1.1" version="1"}%
---+!! Exhaustive Test Page for !ImagePlugin
%TOC%

Expand Down Expand Up @@ -87,6 +87,6 @@ colon before "image" in an intra-wiki link, like this
%IMAGE{":STS-32_crew.jpg|STS-32 crew"}%


%META:FILEATTACHMENT{name="Westminstpalace.jpg" attr="h" comment="" date="1418735227" path="Westminstpalace.jpg" size="19790" user="ProjectContributor" version="1"}%
%META:FILEATTACHMENT{name="Tst.png" attr="h" comment="" date="1418735227" path="Tst.png" size="499" user="ProjectContributor" version="1"}%
%META:FILEATTACHMENT{name="STS-32_crew.jpg" attr="h" comment="" date="1418735227" path="STS-32_crew.jpg" size="49583" user="ProjectContributor" version="1"}%
%META:FILEATTACHMENT{name="Westminstpalace.jpg" attr="h" comment="" date="1437152721" path="Westminstpalace.jpg" size="19790" user="ProjectContributor" version="1"}%
%META:FILEATTACHMENT{name="Tst.png" attr="h" comment="" date="1437152721" path="Tst.png" size="499" user="ProjectContributor" version="1"}%
%META:FILEATTACHMENT{name="STS-32_crew.jpg" attr="h" comment="" date="1437152721" path="STS-32_crew.jpg" size="49583" user="ProjectContributor" version="1"}%
11 changes: 7 additions & 4 deletions data/System/ImagePlugin.txt
@@ -1,4 +1,4 @@
%META:TOPICINFO{author="ProjectContributor" date="1418735227" format="1.1" version="1"}%
%META:TOPICINFO{author="ProjectContributor" date="1437152721" format="1.1" version="1"}%
---+!! <nop>%TOPIC%
%TOC%

Expand Down Expand Up @@ -223,10 +223,13 @@ $Foswiki::cfg{ImagePlugin}{Mode} = 'Image::Magick';
* Set SHORTDESCRIPTION = %$SHORTDESCRIPTION%
-->
| Plugin Author: | Craig Meyer, Foswiki:Main.MichaelDaum |
| Copyright: | &copy; 2006, Craig Meyer, %BR% &copy; 2006-2014 Michael Daum http://michaeldaumconsulting.com |
| 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; |
| 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 |
| 16 Dec 2014 | work around call to unofficial internal api to get absolute pub urls;\
support extracting thumbnails from movies (mp4,mov,m4v,mpeg,mpg and wmv) |
| 17 Sep 2014 | added feature to convert inline image data to proper attachments |
Expand Down Expand Up @@ -302,5 +305,5 @@ $Foswiki::cfg{ImagePlugin}{Mode} = 'Image::Magick';
| Support: | Foswiki:Support/%TOPIC% |


%META:FILEATTACHMENT{name="WestminstpalaceSample.png" attr="h" comment="" date="1418735227" path="WestminstpalaceSample.png" size="107728" user="ProjectContributor" version="1"}%
%META:FILEATTACHMENT{name="logo.gif" attachment="logo.gif" attr="h" date="1418735227" path="http://www.google.com/intl/en_ALL/images/logo.gif" size="8558" user="ProjectContributor" version="1"}%
%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"}%
114 changes: 87 additions & 27 deletions lib/Foswiki/Plugins/ImagePlugin.pm
@@ -1,7 +1,7 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2006 Craig Meyer, meyercr@gmail.com
# Copyright (C) 2006-2014 Michael Daum http://michaeldaumconsulting.com
# Copyright (C) 2006-2015 Michael Daum http://michaeldaumconsulting.com
#
# Based on ImgPlugin
# Copyright (C) 2006 Meredith Lesly, msnomer@spamcop.net
Expand All @@ -27,15 +27,15 @@ package Foswiki::Plugins::ImagePlugin;
use strict;
use warnings;

our $imageCore;
use Foswiki::Func ();
use Foswiki::Plugins ();

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

use Foswiki::Plugins ();
use Foswiki::Meta ();

###############################################################################
sub initPlugin {
Expand All @@ -47,7 +47,7 @@ sub initPlugin {
}

# init plugin variables
$imageCore = undef;
$core = undef;

# register the tag handlers
Foswiki::Func::registerTagHandler(
Expand Down Expand Up @@ -81,15 +81,15 @@ sub initPlugin {
###############################################################################
# lazy initializer
sub getCore {
return $imageCore if $imageCore;
return $core if $core;

Foswiki::Func::addToZone("head", "IMAGEPLUGIN", <<'HERE');
<link rel="stylesheet" href="%PUBURLPATH%/%SYSTEMWEB%/ImagePlugin/style.css" type="text/css" media="all" />
HERE

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

###############################################################################
Expand All @@ -105,32 +105,40 @@ sub beforeSaveHandler {
getCore->beforeSaveHandler(@_);
}

###############################################################################
sub completePageHandler {
#my $text = $_[0];

return unless $Foswiki::cfg{ImagePlugin}{ConvertInlineSVG};

getCore->completePageHandler(@_);
}


###############################################################################
sub commonTagsHandler {

return unless $Foswiki::cfg{ImagePlugin}{RenderExternalImageLinks};

# only render an external image link when in view mode
return unless Foswiki::Func::getContext()->{view};
return unless $Foswiki::cfg{ImagePlugin}{RenderExternalImageLinks} || $Foswiki::cfg{ImagePlugin}{RenderLocalImages};

my ($text, $topic, $web) = @_;

#print STDERR "called commonTagsHandler($web, $topic, $included)\n";

# Have our own _externalLink early enough in the rendering loop
# so that we know which topic we are rendering the url for. This now
# happens as part of the macro expansion and not as part of the tml rendering
# loop.

my $removed = {};
$text = takeOutBlocks($text, 'noautolink', $removed);

$text =~ s/(^|(?<!url)[-*\s(|])
(https?:
([^\s<>"]+[^\s*.,!?;:)<|][^\s]*\.(?:gif|jpe?g|png|bmp|svg)(?:\?.*)?(?=[^\w\-])))/
renderExternalLink($web, $topic, $1, $2)/geox;
putBackBlocks(\$text, $removed, 'noautolink', 'noautolink' );
if ($Foswiki::cfg{ImagePlugin}{RenderExternalImageLinks}) {
$text =~ s/(^|(?<!url)[-*\s(|])
(https?:
([^\s<>"]+[^\s*.,!?;:)<|][^\s]*\.(?:gif|jpe?g|png|bmp|svg)(?:\?.*)?(?=[^\w\-])))/
renderExternalImage($web, $topic, $1, $2)/geox;

}

if ($Foswiki::cfg{ImagePlugin}{RenderLocalImages}) {
$text =~ s/(<img ([^>]+)?\/>)/renderLocalImage($web, $topic, $1)/ge;
}

putBackBlocks(\$text, $removed, 'noautolink', 'noautolink');

# restore the text
$_[0] = $text;
Expand All @@ -155,10 +163,61 @@ sub putBackBlocks {
}

###############################################################################
sub renderExternalLink {
sub renderLocalImage {
my ($web, $topic, $text) = @_;

#print STDERR "renderLocalImage at $web.$topic from $text\n";

my @args = ();

my $file;
my $imgWeb;
my $imgTopic;

my $defaultUrlHost = $Foswiki::cfg{DefaultUrlHost};
$defaultUrlHost =~ s/^http:/https?:/;

foreach my $attr (qw(src width height title align style class alt)) {
if ($text =~ /$attr=["']([^"']+)["']/) {
my $val = $1;

if ($attr eq 'src') {
if ($val =~ /^(?:$defaultUrlHost)?$Foswiki::cfg{PubUrlPath}\/(.*)\/([^\/]+)$/) {
($imgWeb, $imgTopic) = Foswiki::Func::normalizeWebTopicName(undef, $1);
$file = $2;
if ($imgWeb ne $web || $imgTopic ne $topic) {
#print STDERR "excluding image at $imgWeb.$imgTopic\n" if $imgWeb ne $Foswiki::cfg{SystemWebName};
return $text;
}
push @args, "topic=\"$imgWeb.$imgTopic\"" if $imgWeb ne $web || $imgTopic ne $topic;
} else {
# not a local image -> return original text
#print STDERR "not a local image: $val\n";
return $text;
}
} else {
push @args, "$attr=\"$val\"";
}
}
}

return $text unless defined $file; # oh well ... can't extract src

push @args, 'type="plain"';

my $result = "%IMAGE{\"$file\" ".join(" ", @args)."}%";

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

return $result;
}

###############################################################################
sub renderExternalImage {
my ($web, $topic, $prefix, $url) = @_;

#print STDERR "called renderExternalLink($web, $topic, $url)\n";
#print STDERR "called renderExternalImage($web, $topic, $url)\n";

my $href = '';
my $title = $url;
Expand Down Expand Up @@ -192,6 +251,7 @@ sub renderExternalLink {
return $prefix.$url;
}

###############################################################################
sub getPubUrl {
my $session = $Foswiki::Plugins::SESSION;

Expand Down
10 changes: 7 additions & 3 deletions lib/Foswiki/Plugins/ImagePlugin/Config.spec
Expand Up @@ -6,6 +6,10 @@
# Turn on/off downloading and mirroring of links to external images, e.g. http://some.external.site/avatar.jpg
$Foswiki::cfg{ImagePlugin}{RenderExternalImageLinks} = 1;

# **BOOLEAN**
# Activate this flag to process HTML <code>img</code> markup to local images
$Foswiki::cfg{ImagePlugin}{RenderLocalImages} = 1;

# **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 All @@ -31,9 +35,9 @@ $Foswiki::cfg{ImagePlugin}{InlineImageTemplate} = "<img %BEFORE% src='%PUBURLPAT
# Enable efficient delivery of resized images
# using the xsendfile feature available in apache, nginx and lighttpd.
# Use <ul>
# <li>X-Sendfile for Apache2 <li>
# <li>X-LIGHTTPD-send-file for Lighttpd<li>
# <li>X-Accel-Redirect for Nginx<li>
# <li>X-Sendfile for Apache2 </li>
# <li>X-LIGHTTPD-send-file for Lighttpd<l/i>
# <li>X-Accel-Redirect for Nginx</li>
# </ul>
# Note that you will need to configure your http server accordingly.
# If you installed <a href="http://foswiki.org/Extensions/XSendFileContrib">XSendFileContrib</a> as well, its {XSendFileContrib}{Header}
Expand Down

0 comments on commit cd66e6e

Please sign in to comment.