I have a script that get images from a folder and then shows them on my site. But i have edited the script and now i have thumbnail-images in the same folder as the ordinary. Now i have a problem with the script, it shows all the images both ordinary and thumbnail-images on the site. All thumbnails have “thumb_” infront of the ordinary name, like thumb_hello.jpg, thumb_2.jpg, thumb_car.png etx etc.
How do i show only the ordinary images from the folder? Like hello.jpg, 2.jpg, car.png etc
The script im am using for the moment.
echo "<table class='reservdelar' style='width: 100%; padding: 4px;'>";
echo "<tr>";
echo "<td style='width: 70%;'>";
$kh = @opendir("bilder/trimdelar/$_GET[rID]");
while($bild = @readdir($kh)){
if(!($bild=="."||$bild=="..") && preg_match("#\\.(gif|jpeg|jpg)$#i", $bild)){
$flerabilder[] = $bild;
}
}
@closedir($kh);
@$enbild = array_slice($flerabilder, 0, 1);
if (!empty($enbild)) {
echo "<img id='imgBig' src='bilder/trimdelar/$_GET[rID]/$enbild[0]' alt='' border='1' width='570px' height='445px'>";
}
echo "</td>";
echo "<td style='width: 30%; vertical-align: top;'>";
if (!empty($flerabilder)) {
foreach ($flerabilder as $bild) {
echo "<img src='bilder/trimdelar/$_GET[rID]/$bild' vspace='2' width='75' height='56' border='1' onclick=\\"document.images['imgBig'].src='bilder/trimdelar/$_GET[rID]/$bild'\\"><br />";
}
}
echo "</td>";
echo "</tr>";
echo "</table>";
Is there anyone that has an idea? Maybe another preg_match or something? but i dont know how
Rubble
April 22, 2012, 8:58am
2
You could use strpos to see if the string contains thumb_
$thumbnail = strpos($bild,'thumb_');
if($pos === false) {
echo "<img src='bilder/trimdelar/$_GET[rID]/$bild' vspace='2' width='75' height='56' border='1' onclick=\\"document.images['imgBig'].src='bilder/trimdelar/$_GET[rID]/$bild'\\"><br />";
Also I would look at glob() for getting your directory contents as I think it is a bit easier/neater; from memory you may be able to weed out the thumb_ images within the glog function.
// Directory to read images from
$dir = "./";
// Read the directory and sellect jpg and png
$filenames = glob("$dir*.{jpg, JPG, png, PNG}", GLOB_BRACE);
Thanks for a quick respons. But after some “google’ing” i got an solution for my problem
I changed
if(!($bild=="."||$bild=="..") && preg_match("#\\.(gif|jpeg|jpg)$#i", $bild)){
to
if(!($bild=="."||$bild=="..") && preg_match("#\\.(gif|jpeg|jpg)$#i", $bild) && preg_match("#^thumb_#i", $bild) == false){
And it works fine.
Why should i use
$filenames = glob("$dir*.{jpg, JPG, png, PNG}", GLOB_BRACE);
instead of the code im using for the moment?
Rubble
April 22, 2012, 1:14pm
4
You could replace all this:
if(!($bild=="."||$bild=="..") && preg_match("#\\.(gif|jpeg|jpg)$#i", $bild)){
$flerabilder[] = $bild;
with
$filenames = glob("$dir*.{jpg, JPG, png, PNG}", GLOB_BRACE);
If you had an index file or other file in your folder you would need to add that to your ignore code; but with glob you do not.
Your code is selecting everything then weeding out what you do not want; whereas glob is just selecting what you want.
But as I say it is personal preferance.
You can also use patterns and I am sure you could have added something to the glob line to avoid loading any files starting with thumb_ into the array at thet point as well.
tom8
April 22, 2012, 1:17pm
5
Using glob could streamline your code somewhat, which it uses just a single statement. However, it does require an additional function to filter for the thumb images. Here is how your script would look like:
<?php
function thumb($var)
{
// returns whether the input is a thumbnail image
if(strstr($var,"thumb_"))return false;
return true;
}
$dir = "bilder/trimdelar/$_GET[rID]";
$filenames = glob("$dir*.{gif,jpeg,jpg}", GLOB_BRACE);
$filenames = array_filter($filenames,"thumb"); // get rid of thumb images
echo "<table class='reservdelar' style='width: 100%; padding: 4px;'>";
echo "<tr>";
echo "<td style='width: 70%;'>";
@$enbild = array_slice($filenames, 0, 1);
if (!empty($enbild)) {
echo "<img id='imgBig' src='$enbild[0]' alt='' border='1' width='570px' height='445px'>";
}
echo "</td>";
echo "<td style='width: 30%; vertical-align: top;'>";
foreach ($filenames as $bild)
{
echo "<img src='$bild' vspace='2' width='75' height='56' border='1' onclick=\\"document.images['imgBig'].src='$bild'\\"><br />";
}
echo "</td>";
echo "</tr>";
echo "</table>";
?>
I don’t see any advantage in either approach.
Rubble
April 22, 2012, 1:26pm
6
I am not an expert on this but something like this may work?
$filenames = glob("$dir.!thumb_*.{jpg, JPG, png, PNG}", GLOB_BRACE);
tom8
April 22, 2012, 4:11pm
7
I tried that and it didn’t work.
Rubble
April 22, 2012, 4:38pm
8
That was only a guess tom8; this seems to work:
<?php
$filenames = glob("photos/[!thumb_]*.{jpg,gif,png}", GLOB_BRACE);
foreach ( $filenames as $value ){ echo $value."<br>"; }
?>
Need to give credit to: http://cowburn.info/2010/04/30/glob-patterns/
tom8
April 22, 2012, 5:06pm
9
Yes, it works!
Here is the revised version of the script:
<?php
$dir = "bilder/trimdelar/$_GET[rID]";
$filenames = glob($dir."[thumb_]*.{gif,jpeg,jpg}", GLOB_BRACE);
echo "<table class='reservdelar' style='width: 100%; padding: 4px;'>";
echo "<tr>";
echo "<td style='width: 70%;'>";
@$enbild = array_slice($filenames, 0, 1);
if (!empty($enbild)) {
echo "<img id='imgBig' src='$enbild[0]' alt='' border='1' width='570px' height='445px'>";
}
echo "</td>";
echo "<td style='width: 30%; vertical-align: top;'>";
foreach ($filenames as $bild)
{
echo "<img src='$bild' vspace='2' width='75' height='56' border='1' onclick=\\"document.images['imgBig'].src='$bild'\\"><br />";
}
echo "</td>";
echo "</tr>";
echo "</table>";
?>
Now definitely this is a better approach. Did the whole thing with one single statement
Rubble
April 22, 2012, 5:11pm
10
Yes looks a lot better; I would put the start of the table and the end that do not need php outside the php tags and call it done.
<table class='reservdelar' style='width: 100%; padding: 4px;'>
<tr>
<td style='width: 70%;'>
<?php
$dir = "bilder/trimdelar/$_GET[rID]";
$filenames = glob($dir.[!thumb_]*.{gif,jpeg,jpg}", GLOB_BRACE);
@$enbild = array_slice($filenames, 0, 1);
if (!empty($enbild)) {
echo "<img id='imgBig' src='$enbild[0]' alt='' border='1' width='570px' height='445px'>";
}
echo "</td>";
echo "<td style='width: 30%; vertical-align: top;'>";
foreach ($filenames as $bild)
{
echo "<img src='$bild' vspace='2' width='75' height='56' border='1' onclick=\\"document.images['imgBig'].src='$bild'\\"><br />";
}
?>
</td>
</tr>
</table>
tom8
April 22, 2012, 5:38pm
11
Yes, that looks even better but I try not to alter the original script too much.
BTW, there is a " (quote) after dir.
$filenames = glob($dir.“[!thumb_]*.{gif,jpeg,jpg}”, GLOB_BRACE);
Rubble
April 22, 2012, 5:48pm
12
No there should be a quote before $dir - it seems to have been lost along the way somewhere and there needs to be a / in the $dir variable or on the glob after $dir.
$dir = "bilder/trimdelar/$_GET[rID]/";
$filenames = glob("$dir.[!thumb_]*.{gif,jpeg,jpg}", GLOB_BRACE);