First Image Grab

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Roamer
    New Member
    • Dec 2012
    • 23

    First Image Grab

    I have some text with images and want to grab the first image only and include that with the print out of the first sentence.

    This is as far as I've got. I can see the sentence okay, but not the image.
    Code:
    # -- Get first image (do not use module)
    use strict;
    use warnings;
    print "Content-type: text/html\n\n";
    
    my $imget = '<img(.+?>)'; ## may not be right
    my @data = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. <img src=aaa.jpg> blah-blah-blah <img src=bbb.jpg> blah-blah-blah";
    
    my @abc = (@data =~ m/(\w+)\s+$imget\b/gi); # get all images - may not be right
    
    my $abc  = $abc[1];
    print qq ~$abc Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\n~;
    exit;
    How do I grab the image?

    Thanks in advance.
  • Roamer
    New Member
    • Dec 2012
    • 23

    #2
    Anybody got an idea of what I'm after yet?

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Hey! Sorry it took so long for you to get an answer. Holidays and such and I am just getting back into the swing of things. Here is where I just whipped up, which captures and prints exactly the first image file name. Hopefully this helps you.

      Code:
      #!/usr/bin/env perl
      
      # -- Get first image (do not use module)
      
      use strict;
      use warnings;
      
      # variables
      my $data;
      my $var1;
      my $var2;
      
      print "Content-type: text/html\n\n";
      
      $data = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. <img src=aaa.jpg> blah-blah-blah <img src=bbb.jpg> blah-blah-blah";
      
      my @abc = ($data =~ m/src\=(\w+\.jpg){1,1}\>/);
      
      print("@abc\n");
      
      exit;
      Here is what the output looks like:

      Code:
      $ ./images2.pl 
      Content-type: text/html
      
      aaa.jpg
      Regards,

      Jeff

      Comment

      • Roamer
        New Member
        • Dec 2012
        • 23

        #4
        Great,thanks a lot.

        Still need to check it out fully. I've written my own blog package and wanted this to grab an image (if there is one in the post) and slam it into the teaser text.

        Comment

        • RonB
          Recognized Expert Contributor
          • Jun 2009
          • 589

          #5
          If you only want the regex to capture a single value, then storing it in an array doesn't make much sense.

          This works with your example string, but would need a slight adjustment if your string contained valid syntax in the image tag.

          Code:
          #!/usr/bin/perl
          
          use warnings;
          use strict;
          use CGI qw(:standard);
          
          my $data = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. <img src=aaa.jpg> blah-blah-blah <img src=bbb.jpg> blah-blah-blah";
          my $img = $1 if $data =~ /<img src=([^>]+)/;
          
          print header(), start_html(),
                qq~$img Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\n~,
                end_html();
          This is the output it produces.
          Code:
          Content-Type: text/html; charset=ISO-8859-1
          
          <!DOCTYPE html
                  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
          <head>
          <title>Untitled Document</title>
          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
          </head>
          <body>
          aaa.jpg Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
          
          </body>
          </html>

          Comment

          • Roamer
            New Member
            • Dec 2012
            • 23

            #6
            Hi guys,
            Neither of these will work with my code. I don't use the CGI module because it's too overbloated and can't design around it easily.

            I should have posted the sub routine in the first place, sorry. Just thought it would be a little thing.


            Code:
            ## --- This is the part where image is needed --- ##
            else {
            @words = split(/\s+/, $fd[3]); ## the full text of the entry
            
            # I think it has to go into the join at the beginning of it (CSS takes care of rest)
            
            ## the teaser words defined by setting ($hpw = number of words in teaser)
            $ash = join(" ", @words[0..$hpw]); 
            
            ## the entry text (excluding title)
            print qq ~$ash.....~;
            }
            ## --- end part where image is needed --- ##
            I think (just a guess really) that I need to be able to find the first image (could be a thousand words down - each entry is different). Grab it. Hold it. Then insert that in front of the join (again - I think)

            Hope that all makes sense...

            Comment

            • RonB
              Recognized Expert Contributor
              • Jun 2009
              • 589

              #7
              I think you need to provide more code and better explanation of what you're needing.

              I don't see how your lasted code snippet fits into your original question and code.

              Comment

              • Roamer
                New Member
                • Dec 2012
                • 23

                #8
                I've decided that this is going to get too long winded just to get the image, let alone resize and position it. It'll be easer in the long run for me to just be picky about the first image. Then include it in the number of words assigned to my teaser text and have one less teaser.

                Thanks a lot for your input, I have learned something, so it hasn't been a total waste.

                Comment

                • Roamer
                  New Member
                  • Dec 2012
                  • 23

                  #9
                  Now sorted it. Thanks for input.

                  Comment

                  Working...