Although it may be unwise, I'm trying to insert the image names as image alt attributes in web pages. I'm using HTML::Element, but can't seem to get it quite right. I think I know how to create a new tag but not how to creat a new attribute within say all img tags that don't have alt attributes.

Here's what I have:

$tree = HTML::TreeBuilder->new_from_content($html); foreach my $img ($tree->look_down('_tag', 'img')) { my $alatt = $img->attr('alt') if (!$alatt) { $imgsrcattribute = $img->attr('src'); use File::Basename; my @suffixlist = qw(gif, jpg, jpeg, png, bmp, php, ico, GIF, J +PG, JPEG, PNG, BMP, PHP, ICO); my $imgfilenopathnoext = fileparse($imgsrcattribute,@suffixlis +t); my $newalttag = HTML::Element->new('img', alt=>""); $newalttag->attr('alt', "$imgfilenopathnoext"); $html->push_content($imgfilenopathnoext); $imgfilenopathnoext = $imgfilenopathnoext->delete; print "Alt tag not found for $imgsrcattribute \nInserted src f +ile name $imgfilenopathnoext for alt attribute"; } }

So how do I find all the images that don't have alt tags and insert the image file name (not the full path or extension) as the alt attribute?

I thought of another possibility of using the nearest non-html content text as the alt tag "suggestion". If anybody has any ideas for other ways to automatically create the alt tag contents I'd be happy to hear.

The dream way to make alt tags automatically (or at least have a temporary space filler) would be to hook into a semantic image search API and have the image identified some way. For instance possibly using this service: http://www.revimg.net/add.php. However for this to work, with a Google Goggles type program, the service would have to make correct identity guesses pretty accurately.


In reply to Trying to Insert Alt Tags Programmatically by socrtwo

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.