-
Notifications
You must be signed in to change notification settings - Fork 0
/
img2html
executable file
·156 lines (110 loc) · 2.79 KB
/
img2html
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use GD;
my $resolution = shift or die('not enough arguments');
my $scale = shift or die('not enough arguments');
my $img = GD::Image->new(shift) or die('not enough arguments');
my ( $width, $height ) = $img->getBounds;
my $last_color;
my @out;
print << 'EOF';
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>img2html</title>
<style type="text/css">
div {
position: absolute;
overflow: hidden;
}
</style>
</head><body>\n
EOF
for ( my $y = 0 ; $y <= $height ; $y += $resolution ) {
for ( my $x = 0 ; $x <= $width ; $x += $resolution ) {
my ( $r, $g, $b ) = $img->rgb( $img->getPixel( $x, $y ) );
if ( @out
and $out[-1]{y} == $y
and $out[-1]{r} == $r
and $out[-1]{g} == $g
and $out[-1]{b} == $b )
{
$out[-1]{w}++;
}
else {
push(
@out,
{
x => $x,
y => $y,
r => $r,
g => $g,
b => $b,
w => 1,
h => 1
}
);
}
}
}
for my $div (@out) {
printf(
'<div style="background-color: #%02x%02x%02x; '
. 'width: %dpx; height: %dpx; '
. 'left: %dpx; top: %dpx"> </div>'
. "\n",
$div->{r},
$div->{g},
$div->{b},
$div->{w} * $resolution * $scale,
$div->{h} * $resolution * $scale,
$div->{x} * $scale,
$div->{y} * $scale
);
}
print "</body></html>";
__END__
=head1 NAME
img2html - Convert Image into HTML file
=head1 SYNOPSIS
B<img2html> I<resolution> I<scale> I<file>
=head1 DESCRIPTION
img2html converts an ordinary image into a HTML resembling it, which is
outputted on stdout.
Options:
=over
=item I<resolution>
resolution factor. 1 means the image will remain it's original quality, 2 means
it will consist of 2x2 pixel blocks, 3 means 3x3 pixel blocks, and so on.
=item I<scale>
scale factor. 1 means nothing will be changed, 0.5 means the image will be
scaled to half it's size.
NOTE: Unless you know what you're doing, make sure this equation is true:
I<resolution>/I<scale> >= 1
=item I<file>
Image file. For supported formats, see GD(3pm)
=back
=head1 EXAMPLES
=over
=item img2html 1 1 foo.jpg > full.html
Convert foo.jpg to full.html, with full quality and full resolution
=item img2html 10 0.1 foo.jpg > small.html
Convert foo.jpg to small.html, scaling it to 10% of it's size, with as high
quality as possible
=item img2html 3 1 foo.jpg > ugly.html
Convert foo.jpg to ugly.html, preserving it's size, but with lower quality
=back
=head1 BUGS
=over
=item * It will make your browser lag. Do NOT try images larger than 300*300
pixels without scaling.
=back
=head1 TODO
=over
=item * Some error checking
=item * Compression
=back
=head1 AUTHOR
Copyright (c) 2008 Birte Kristina Friesel <[email protected]>
=head1 LICENSE
0. You just DO WHAT THE FUCK YOU WANT TO.