php: read dir with images and create style-rule with position based on filename?
I'm not much of a php junkie so I need your help in creating the best and simplest solution for this. I have a folder "path/images/headers" where I have multiple header images (all cut to the right size). The filenames look like this "something_right.jpg" , "xyz_left.jpg", "dfjakdfjs_center.jpg", etc.
I need a way to read this folder, provide a random image file and finally create a style rule that sets this image as background value and sets the background-X position开发者_开发技巧 based on the _underscore value of the filename!
<?php
function header_image() {
$imgDir = bloginfo('template_url'); ?>/images/headers;
$pos =
return "transparent url('images/headers/" . $img . "') no-repeat " . $pos . " center;"
}
?>
<style type="text/css">
.header-image {
background: <?php echo header_image(); ?>
}
</style>
So imagine I have "xyz_right.jpg" the style rule should look like this:
background: transparent url('images/headers/xyz_right.jpg') no-repeat right center;
Any idea what's the best solution to do so?
I'd go with glob
, shuffle
and preg_match
, not DirectoryIterator
and strpos
.
There is quite a lot to this, you need a way to point to each file (DirectoryIterator class might help here, especially the getBasename method) and a way to get the value from the file name (I would opt for strpos to search the string for each condition).
How many files are in/will be in the folder?
精彩评论