forked from bentglasstube/blosxom-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagesizer-v20030222
96 lines (71 loc) · 2.34 KB
/
imagesizer-v20030222
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Blosxom Plugin: imagesizer
# Author: Nelson Minar <[email protected]>
# Version: 20030222
# http://www.nelson.monkey.org/~nelson/weblog/
# License: Public Domain
# Imagesizer is a Blosxom plugin that inserts width and height
# tags into images you include in your Blosxom entries.
# It requires the Perl module Image::Size, available from
# http://www.blackperl.com/Image::Size/
# (Or if you use Debian, apt-get install libimage-size-perl)
# Installation:
# Modify the $rootURL and $docRoot variables below
# Drop imagesizer in your plugins directory
# Known bugs:
# You may have to modify the imgSrcToFile function
# Does not gracefully handle more than one <img> tag in an entry
# Perl gives me a rash, so the code is ugly.
package imagesizer;
use Image::Size 'html_imgsize';
# Root of URLs for your images
$rootURL = '^/~nelson/';
# Pathname those URLs map to
$docRoot = '/home/nelson/html/';
# Sub to turn a URL into a filename. With the default config,
# this maps URLs like /~nelson/img/foo.gif into filenames like
# /home/nelson/html/img/foo.gif
sub imgSrcToFile {
my ($src) = @_;
if ($src =~ s!$rootURL!$docRoot!) {
return $src;
}
return 0;
}
sub start {
1;
}
# Debug output holder - debug output is commented out at the bottom.
$debug = '';
# Modify the body of stories by including image width and height tags
sub story {
my($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
$body = $$body_ref;
# Find any <img> tag in the post body
if ($body =~ m/(<img [^>]+>)/is) {
$tag = $1;
$debug .= "Tag: " . $tag . "\n";
# Skip image tags that already have height or width specified
return if ($tag =~ m/(width|height)=/i);
# Grab the source out of the image tag
$tag =~ m/<img .*?src="(.*?)".*?>/is;
$debug .= "Src: " . $1 . "\n";
# Ask Image::Size to give us width & height data
$filename = imgSrcToFile($1);
$debug .= "File: " . $filename . "\n";
if (-r $filename) {
$size = html_imgsize($filename);
$debug .= "Size: " . $size . "\n";
}
# return if !$size;
# And substitute it into the image
$$body_ref =~ s/<img /<img $size /is;
$debug .= "<br>\n";
}
1;
}
# Only needed for debug output
sub end {
# print "<p>", $debug;
1;
}
1;