Изкарване на картинки с определено име

montana

Registered
Здравейте, не знам как точно да го обясня, но с кода по-долу извличам картинки от папка, но в папката се качват оригинала + thumb и искам да изкарам само thumb-овете , но не знам как да стане. На всичките thumb името на файла завършва с ''th'', демек:
ime.th.jpg
ime.th.jpeg
ime.th.gif и т.н

Някой може ли да преправи кода така, че само тях да ми изкарва?

Код:
<?php

// КОНФИГУРАЦИЯ //

$pics_folder = "/images"; // в коя папка са картинките и категориите
$max_width = 100; // максимална широчина
$max_height = 220; // максимална височина
$pics_per_page = 9; // по колко картинки на страница
$pics_ex = array('jpg','jpeg','gif','png'); // кои файлове да зачита като картинки

// КРАЙ НА КОНФИГУРАЦИЯТА //

function resize($pic)
{
    global $max_width, $max_height;
    $max_width_pic = $max_width;
    $max_height_pic = $max_height;
    list($width,$height) = getimagesize($pic);
    $ratio = $width / $height;
    
    if ($width > $max_width_pic || $height > $max_height_pic)
    {
        if ($width > $height)
        {
            $width = $max_width_pic;
            $height = $width / $ratio;
        }
        elseif ($width < $height)
        {
            $height = $max_height_pic;
            $width = $height * $ratio;
        }
        elseif ($width == $height)
        {
            $width = $max_width_pic;
            $height = $max_height_pic;
        }
    }
    $width = intval($width);
    $height = intval($height);
    return array("width" => $width, "height" => $height);
}

$pics_folder = preg_replace('/[\/]+/', '/', preg_replace('/^[\/]+/', '', $pics_folder).'/');

function all_images()
{
    global $pics_folder, $pics_ex;
    $folders = array($pics_folder);
    $out_files = array();
    while (count($folders))
    {
        foreach ($folders as $folder)
        {
            array_shift($folders);
            $dir = opendir($folder);
            while ($file = readdir($dir))
            {
                if ($file != '.' && $file != '..')
                {
		
                    if (is_file($folder.$file) && in_array(strtolower(end(explode(".", $file))), $pics_ex))
                    {
                        $out_files[] = $folder.$file;
                    }
                    elseif (is_dir($folder.$file))
                    {
                        $folders[] = $folder.$file.'/';
                    }
                }
            }
        }
    }
    return $out_files;
}

$all_images = all_images();

$category = false;
if (preg_match('/^[^\/\\\:\*\?\"\<\>\|]+((->)[^\/\\\:\*\?\"\<\>\|]+)*$/i', $_GET['kat']))
{
    $category = (is_dir($pics_folder.str_replace('->', '/', $_GET['kat']))) ? str_replace('->', '\/', $_GET['kat']) : false;
}

$out_images = array();
foreach ($all_images as $image)
{
    $raz = explode("/", $image);
    for ($n=1;$n<=(substr_count($pics_folder, "/"));$n++) array_shift($raz);
    array_pop($raz);
    
    if ($category && !preg_match("/^$category/", implode("/", $raz))) continue;
    $out_images[] = $image;
}

$masiv = @array_chunk($out_images, $pics_per_page, true);

$stranici = floor(count($out_images) / $pics_per_page);
if ((count($out_images) % $pics_per_page) > 0) { $stranici++; }

if (is_numeric($_GET['page']) && $_GET['page'] <= $stranici) {
    $page = $_GET['page'];
} else {
    $page = 1;
}

$red = 1;
echo "<span id=\"gallery\"><table border=\"0\">
<tr>\n";
foreach ($masiv[($page - 1)] as $pic)
{
    $resize = resize($pic);
    $razp = explode("/", $pic);
    for ($n=1;$n<=(substr_count($pics_folder, "/"));$n++) array_shift($razp);
    array_pop($razp);
    
    $kat = array();
    $kat[] = '<a href="?">Главна</a>';
    for ($i=0;$i<count($razp);$i++)
    {
        $url = array();
        for($i2=0;$i2<=$i;$i2++)
        {
            $url[] = $razp[$i2];
        }
        $kat[] = '<a href="?kat='.implode("->", $url).'">'.$razp[$i].'</a>';
    }
    
    echo "<td><a href=\"{$pic}\"><img src=\"{$pic}\"  width='150' heidth='120' border=\"0\"></a></td>\n";
    if ($red == floor(sqrt($pics_per_page))) { $red = 1; echo "</tr><tr>\n"; } else { $red++; }
}
echo "</tr>
</table></span>";

echo "<br><br><br />Страници: ";
for ($i=1;$i<=$stranici;$i++) {
    if ($page != $i) {
        echo "<a href=\"?page=$i".(($category) ? '&kat='.$_GET['kat'] : '')."\">$i</a> ";
    } else {
        echo "$i ";
    }
}
?>
 
Използвай preg_match който да търси дали стринга (името на картинката) съдържа .th.
 
PHP:
<?php

// КОНФИГУРАЦИЯ //

$pics_folder = "./images"; // в коя папка са картинките и категориите
$max_width = 100; // максимална широчина
$max_height = 220; // максимална височина
$pics_per_page = 9; // по колко картинки на страница
$pics_ex = array('jpg','jpeg','gif','png'); // кои файлове да зачита като картинки

// КРАЙ НА КОНФИГУРАЦИЯТА //

function resize($pic)
{
    global $max_width, $max_height;
    $max_width_pic = $max_width;
    $max_height_pic = $max_height;
    list($width,$height) = getimagesize($pic);
    $ratio = $width / $height;
   
    if ($width > $max_width_pic || $height > $max_height_pic)
    {
        if ($width > $height)
        {
            $width = $max_width_pic;
            $height = $width / $ratio;
        }
        elseif ($width < $height)
        {
            $height = $max_height_pic;
            $width = $height * $ratio;
        }
        elseif ($width == $height)
        {
            $width = $max_width_pic;
            $height = $max_height_pic;
        }
    }
    $width = intval($width);
    $height = intval($height);
    return array("width" => $width, "height" => $height);
}

$pics_folder = preg_replace('/[\/]+/', '/', preg_replace('/^[\/]+/', '', $pics_folder).'/');

function all_images()
{
    global $pics_folder, $pics_ex;
    $folders = array($pics_folder);
    $out_files = array();
    while (count($folders))
    {
        foreach ($folders as $folder)
        {
            array_shift($folders);
            $dir = opendir($folder);
            while ($file = readdir($dir))
            {
                if ($file != '.' && $file != '..')
                {
      
                    if (is_file($folder.$file) && in_array(strtolower(end(explode(".", $file))), $pics_ex))
                    {
                        $out_files[] = $folder.$file;
                    }
                    elseif (is_dir($folder.$file))
                    {
                        $folders[] = $folder.$file.'/';
                    }
                }
            }
        }
    }
    return $out_files;
}

$all_images = all_images();

$category = false;
if (preg_match('/^[^\/\\\:\*\?\"\<\>\|]+((->)[^\/\\\:\*\?\"\<\>\|]+)*$/i', $_GET['kat']))
{
    $category = (is_dir($pics_folder.str_replace('->', '/', $_GET['kat']))) ? str_replace('->', '\/', $_GET['kat']) : false;
}

$out_images = array();
foreach ($all_images as $image)
{
    $raz = explode("/", $image);
    for ($n=1;$n<=(substr_count($pics_folder, "/"));$n++) array_shift($raz);
    array_pop($raz);
   
    if ($category && !preg_match("/^$category/", implode("/", $raz))) continue;
    $out_images[] = $image;
}

$masiv = @array_chunk($out_images, $pics_per_page, true);

$stranici = floor(count($out_images) / $pics_per_page);
if ((count($out_images) % $pics_per_page) > 0) { $stranici++; }

if (is_numeric($_GET['page']) && $_GET['page'] <= $stranici) {
    $page = $_GET['page'];
} else {
    $page = 1;
}

$red = 1;
echo "<span id=\"gallery\"><table border=\"0\">
<tr>\n";
foreach ($masiv[($page - 1)] as $pic)
{
    $resize = resize($pic);
    $razp = explode("/", $pic);
    for ($n=1;$n<=(substr_count($pics_folder, "/"));$n++) array_shift($razp);
    array_pop($razp);
   
    $kat = array();
    $kat[] = '<a href="?">Главна</a>';
    for ($i=0;$i<count($razp);$i++)
    {
        $url = array();
        for($i2=0;$i2<=$i;$i2++)
        {
            $url[] = $razp[$i2];
        }
        $kat[] = '<a href="?kat='.implode("->", $url).'">'.$razp[$i].'</a>';
    }
	if(preg_match("/.th/u", $pic)) {
		echo "<td><a href=\"{$pic}\"><img src=\"{$pic}\"  width='150' heidth='120' border=\"0\"></a></td>\n";
	}
    if ($red == floor(sqrt($pics_per_page))) { $red = 1; echo "</tr><tr>\n"; } else { $red++; }
}
echo "</tr>
</table></span>";

echo "<br><br><br />Страници: ";
for ($i=1;$i<=$stranici;$i++) {
    if ($page != $i) {
        echo "<a href=\"?page=$i".(($category) ? '&kat='.$_GET['kat'] : '')."\">$i</a> ";
    } else {
        echo "$i ";
    }
}
?>

@ Hous, ме изпревари.. :)
 

Back
Горе