/    Sign up×
Community /Pin to ProfileBookmark

preg_match question/problem

Hi Everyone,

I am very new to php but learning. I have a script that displays random images from a directory. The directory stores the original size image along with the thumbnail image created from the component. The thumbnail image is identified by the prefix ‘thmb_’.

Now my question/problem how do I get the script to select only the thumbnails? I think it has something to do with the preg_match but not sure?

Here is a copy of my script:

function image_rand($dir)
{
if( empty($dir) ) {

return false;
} else if( !is_dir($dir) ) {
return false;
} else if( substr($dir, -1) != ‘/’) {
$dir .= ‘/’;
}

$images = array();

$rh = opendir($dir);

while( $filename = readdir($rh) ) {
if( is_file($dir . $filename) && strstr($filename, ‘.’) ) {
$extension = strrchr($filename, ‘.’);

if( preg_match(‘/^.(bmp|jpe?g|gif|png)$/i’, $extension) ) {

$images[] = $dir . $filename;
}
}

}

if( count($images) > 0 ) {

return $images[ array_rand($images) ];

} else {
return false;
}
}

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@JonaSep 06.2004 — [font=trebuchet ms]Replace this line:[/font]

[code=php]
if( preg_match('/^.(bmp|jpe?g|gif|png)$/i', $extension) ) {
[/code]


[font=trebuchet ms]with this one:[/font]

[code=php]
if( preg_match('/^thmb_.+$/i', $filename) ) {
[/code]
Copy linkTweet thisAlerts:
@ivainsauthorSep 06.2004 — Thank you Jona! I knew I was on the right track, but wasn't quite sure how to proceed.

Once again Thanks!
Copy linkTweet thisAlerts:
@JonaSep 06.2004 — [font=trebuchet ms]Happy to help. ? [/font]
Copy linkTweet thisAlerts:
@ivainsauthorSep 06.2004 — Sorry one more question. Where can find coding information concerning the same script, what I'm trying to do now is link the thumbnail to the object with a single query if possible.

Here is what is happening:

When the image is uploaded, it is uploaded full-size (e.g. image.jpg) and the component automatically creates a thumbnail (e.g. thmb_image.jpg) in the same directory which the script above nicely extracts for viewing. The db only saves the name of the full-size image, in this case it is image.jpg.

How can I query this or find information so that the thumbnail displayed can be hyperlinked to the object so once selected it can display the information? The information of the object is stored in a seperate table from the images.

Tables go like this:

estateagent_images

id

img_id

obj_img1

obj_img1_descript

estateagent_objects

id

obj_id

obj_cat

Now there are more fields of course, I'm just giving an example.

Any help would be gratefully appreciated. On the other hand I will continue to chop away and wreck my brain on learning this php coding.

Once again Thanks.
Copy linkTweet thisAlerts:
@JonaSep 06.2004 — [font=trebuchet ms]I'm sorry I've failed to understand what you want...[/font]
Copy linkTweet thisAlerts:
@ivainsauthorSep 07.2004 — I'm sorry I tend to get carried away sometimes... I'm trying to link the thumbnail. The thumbnails are pictures of realestate. It is random displaying now and I'm just trying to link it to the data.

The original component script creates the 'thmb_' automatically during the upload of the full-size image. However, it only stores the name of the full-size image in the _images db. Example you name the full-size image 'estate.jpg', thats what will be stored in the db. But the the thumbnail it creates (thmb_estate.jpg) will not be reference in the db.

I'm just trying to associate the 'thmb_estate.jpg' with the 'estate.jpg' in order to retrieve the data.

I think I got carried away again???
×

Success!

Help @ivains spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...