Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-23 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found