This is probably done a thousand times before, so this is the 1001st

Use this as a CGI, upload an image and get it back displayed as an HTML table. (not sure why you wanna do such a thing though ;)

View a live version here

Update: I fixed the code which now uses snippets from merlyn's column.
#!/usr/bin/perl -Tw use strict; $|++; use CGI qw/:standard *table/; $CGI::POST_MAX = 200000; # it's already too heavy print header, start_html('Image2HTML'); print h1 ('Image2HTML'); if (param('do')) { eval "use Image::Magick"; die "We need Image::Magick: $@\n" if $@; my $image; my $file = upload('file'); $image = Image::Magick->new or die "Cannot create Image::Magick Object: $image\n"; my $error = $image->Read(file=>$file); if ($error) { print strong('Bad File: '. $error ); } else { $image->Set(magick => 'rgb') && die "Cannot set to RGB, $_"; $image->Scale(geometry => '50x50'); my $width = $image->Get('columns'); my $height = $image->Get('rows'); print start_table({-border=>0, -cellpadding=>0, -cellspacing=>0}); my @blobs = unpack "C*", $image->ImageToBlob(); for (1..$height) { print q|<tr>|; for (1..$width) { print q|<td bgcolor="#|; my ($r,$g,$b) = splice @blobs, 0, 3; print sprintf("%02x%02x%02x", $r, $g, $b); print q|"><img src="/data/trans.gif" width="5" + height="5" /></td>|; } print qq|</tr>\n|; } print end_table; } } print hr; print p(q|This is Image2HTML, just another way to waste computer resources. Use the file field to upload an image (maximum size 200,000 bytes) and get it back as colored HTML &lt;table&gt;. Any kind of image will mostly work.|); print p(q|Note: The image is scaled down/up(?) for your own interest ; +)|); print p(q|This works best with images that have smooth color changes and gradients, brisk turns in colors can come out ugly.|); print start_form('POST', CGI::url(), 'multipart/form-data'); print filefield('file'), submit('do', 'Upload'), br; print end_form, end_html;

He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

Chady | http://chady.net/

In reply to Image2HTML by Chady

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.