Jump to content

[SOLVED] Extract all image references and place in an array


Jalz

Recommended Posts

Hi all,

 

Sorry to post again, tried doing an search but that feature doesn't work on my computer? Perhaps the servers too busy?

 

I have a string of text which contain several references to images. How can I collect all the references and store them in an array. I'm trying to automate a picture gallery using this feature.

 

Secondly would anyone recoomend any pointers regarding the best way to size images on the server. I've got images in the system which are about 1024 wide, although on a page they are displayed at 200px wide (but sizes vary), so just trying to figure out the most efficient way of storing them and making the server resize them if at all possible.

 

Many Thanks for your help.

 

Jalz

Link to comment
Share on other sites

Question 1.) You'll be wanting regular expressions. Have you used them at all in the past?

 

Question 2.) Indeed, that's pretty inefficient. If you're just using the img tag's attributes to set the size of the image, then the full image is still being loaded. That's bad for you and the visitor to your website, because of the increased bandwidth and hence download time. If you have the GD library installed, you can resize images using the imagecopyresampled() function. As for the best way to do it...well that depends on your requirements and current set up.

 

Obviously you have existing images on the server and you'll have to process them initially. You could write a script to open each of the images and create a thumbnail. I'd probably recommend keeping the larger images saved though, perhaps you'll want them in the future and, let's face it, storage space is pretty cheap. If you have lots of images, you'll probably be better off doing this in batches of the images.

 

If you're also uploading images, then you'll probably also want to add a resize feature on the upload too.

Link to comment
Share on other sites

Hi GingerRobot,

 

Thanks for replying. I've only usedd regular expressions once with the help of another forum member. I used preg_match('~<img[^>]*src\s?=\s?[\'"]([^\'"]*)~i',$string,$imgsrc); which would do what I want for the first <img> tag and properties, but if there are several I need to store them in an array (lets say $imgsrc) which im not quite sure how to do.

 

Secondly, at the moment I've only go a dozen or so images, im playing around with this system, see what is achievable and what isn't. Im using fckedior to upload images, think their might be a plugin or something that may reduce the size of the image when uploaded.

 

Cheers

 

J

Link to comment
Share on other sites

Hi GingerRobot,

 

Thanks for that. I'm having a little difficulty trying to echo out the results as all I see is the word Array.

 

If I use the print_r($imgsrc) command I get the following returned which is correct, so can't see where I am going wrong.

 

This is what is being stored in $imgsrc

 

Array
(
    [0] => Array
        (
            [0] => <img height="150" alt="" width="200" src="/OO_online/image/Humpback%20Whale.jpg" />
            [1] => <img height="150" width="200" alt="" src="/OO_online/image/Garden.jpg" />
        )

)

 

 

 

The code below is what I have and what I am trying to echo, but is only displaying the word Array. We know $imgsrc contains the value, because of the print_r command used above.

 

$string = html_entity_decode($test_row->getField('text'));
preg_match_all('~<img[^>]*>~i',$string,$imgsrc);

foreach( $imgsrc as $key => $img_ref){
echo $img_ref;
}
			?>

 

Many thanks for input, i'll add a <br /> command at the end of the $img_ref once I know where I've gone wrong.

 

Jalz

Link to comment
Share on other sites

If you take another look at the manual page and look at the 'flags' parameter and I tell you that the default value of that parameter is PREG_PATTERN_ORDER, then hopefully you might see what's going on.

 

The function returns an array of arrays. By default index 0 is the full match, index 1 is, as the manual says, the first parenthesized expression. In your case, there are no parenthesized expressions, so you just have index 0. This is shown in the output from print_r.

 

When you use the foreach loop on $imgsrc, the only time it runs is for that index 0. (I.e. $key is 0 and $img_ref is set as the array. You want to loop through $imgsrc[0] and not $imgsrc:

 

foreach( $imgsrc[0] as $key => $img_ref){
echo $img_ref;
}

Link to comment
Share on other sites

Thanks GingerRobot, that worked a treat and also read the manial as suggested. Now trying to figure out how I can put a text a string, i.e. imageresize.php in the source of the image name as I've found quite a nice little script that will resize images as I want, but will do som reading before I post another thread :)

 

Jalz

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.