473,408 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

First Image Grab

23
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.
Expand|Select|Wrap|Line Numbers
  1. # -- Get first image (do not use module)
  2. use strict;
  3. use warnings;
  4. print "Content-type: text/html\n\n";
  5.  
  6. my $imget = '<img(.+?>)'; ## may not be right
  7. my @data = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. <img src=aaa.jpg> blah-blah-blah <img src=bbb.jpg> blah-blah-blah";
  8.  
  9. my @abc = (@data =~ m/(\w+)\s+$imget\b/gi); # get all images - may not be right
  10.  
  11. my $abc  = $abc[1];
  12. print qq ~$abc Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\n~;
  13. exit;
How do I grab the image?

Thanks in advance.
Dec 16 '12 #1
8 2328
Roamer
23
Anybody got an idea of what I'm after yet?
Jan 4 '13 #2
numberwhun
3,509 Expert Mod 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env perl
  2.  
  3. # -- Get first image (do not use module)
  4.  
  5. use strict;
  6. use warnings;
  7.  
  8. # variables
  9. my $data;
  10. my $var1;
  11. my $var2;
  12.  
  13. print "Content-type: text/html\n\n";
  14.  
  15. $data = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. <img src=aaa.jpg> blah-blah-blah <img src=bbb.jpg> blah-blah-blah";
  16.  
  17. my @abc = ($data =~ m/src\=(\w+\.jpg){1,1}\>/);
  18.  
  19. print("@abc\n");
  20.  
  21. exit;
  22.  
Here is what the output looks like:

Expand|Select|Wrap|Line Numbers
  1. $ ./images2.pl 
  2. Content-type: text/html
  3.  
  4. aaa.jpg
  5.  
Regards,

Jeff
Jan 5 '13 #3
Roamer
23
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.
Jan 5 '13 #4
RonB
589 Expert Mod 512MB
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.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5. use CGI qw(:standard);
  6.  
  7. my $data = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. <img src=aaa.jpg> blah-blah-blah <img src=bbb.jpg> blah-blah-blah";
  8. my $img = $1 if $data =~ /<img src=([^>]+)/;
  9.  
  10. print header(), start_html(),
  11.       qq~$img Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\n~,
  12.       end_html();
  13.  
This is the output it produces.
Expand|Select|Wrap|Line Numbers
  1. Content-Type: text/html; charset=ISO-8859-1
  2.  
  3. <!DOCTYPE html
  4.         PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  5.          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
  7. <head>
  8. <title>Untitled Document</title>
  9. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  10. </head>
  11. <body>
  12. aaa.jpg Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  13.  
  14. </body>
  15. </html>
Jan 5 '13 #5
Roamer
23
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.


Expand|Select|Wrap|Line Numbers
  1. ## --- This is the part where image is needed --- ##
  2. else {
  3. @words = split(/\s+/, $fd[3]); ## the full text of the entry
  4.  
  5. # I think it has to go into the join at the beginning of it (CSS takes care of rest)
  6.  
  7. ## the teaser words defined by setting ($hpw = number of words in teaser)
  8. $ash = join(" ", @words[0..$hpw]); 
  9.  
  10. ## the entry text (excluding title)
  11. print qq ~$ash.....~;
  12. }
  13. ## --- 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...
Jan 5 '13 #6
RonB
589 Expert Mod 512MB
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.
Jan 5 '13 #7
Roamer
23
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.
Jan 7 '13 #8
Roamer
23
Now sorted it. Thanks for input.
Jan 19 '13 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ken | last post by:
I would like to measure the fileSize of the image (without uploading it - php). I use: var size_pic = document.getElementById('num1').childNodes.fileSize; alert("size = " + size_pic); which...
6
by: Patrick Dugan | last post by:
Hello, I'm trying to load different images (icons) into a PictureBox1.Image. The first image loads just fine, but the second image always returns the error "Invalid property used." It doesn't...
3
by: =?Utf-8?B?SlIx?= | last post by:
I would like to add text to an image. I have tried to use DrawString and it works on some images but on others it is very very small. I am pretty sure it has something to do with the size of the...
9
by: tshad | last post by:
Is there a way to display images (imageButtons or linkbuttons for instance) as a max size (200px by 50px) and not have it stretch the image? What I want to be able to do is limit the real estate...
9
neit
by: neit | last post by:
hi every one, i'm new at this things in programing (visual basic 6.0). And i was wondering if i can make an image grab or attach a label to move it around and place it in another place, with the...
1
by: Lastknight | last post by:
Hi all, i am looking for your help regarding this topic... I am trying to download a image from the web through perl program. I have used Image::Grab module for that task,through the module...
1
by: Patrick | last post by:
Hello, Is it possible to get the first image of an AVI file from my PHP script. I need it to dynamically create a catalog of my multimedia files (jpeg, avi) and to display a thumbnail for each...
3
by: kayahr | last post by:
Hi there, I have a strange problem in Internet Explorer (IE6 and IE7). I'm writing a JavaScript application which allows the user to edit a photo composition. So when the user selects a photo then...
1
by: nanaalwi | last post by:
Hi, Currently I'm developing a software using VB.net that can grab an image using Matrox Morphis card and display it in a PictureBox. The software can grab the image already and display it in the...
0
by: David | last post by:
On Sun, May 4, 2008 at 5:28 PM, Valerio Valerio <vdv100@gmail.comwrote: I assume what you want to do is allow the user to drag the mouse across your desktop, and your program, running in the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.