Foto's sorteren op alfabet

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Eric osman

eric osman

29/08/2010 17:23:25
Quote Anchor link
Hallo,

Ik ben bezig met een site maar krijg het niet voor elkaar om de foto's op alfabet te sorteren :(

Misschien dat iemand mij kan helpen of zo?

dit is het script dat ik gebruik :

[thumb_gen.php]

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
//This file takes a value passed via a get variable, creates a thumbnail and outputs the thumbnail.

// change header information, so browser reads this file as an image

header('Content-type: image/jpeg');
function
generateThumbnail($file) {
// get dimensions of target file
list($width, $height) = getimagesize($file);
// decide whether the image is portrait or landscape orientation
if($width > $height) $biggestSide = $width;
else $biggestSide = $height;
// create GD image
$current_image = imagecreatefromjpeg($file);
//The crop size will be half that of the largest side
$cropPercent = .5;
$cropWidth   = $biggestSide*$cropPercent;
$cropHeight  = $biggestSide*$cropPercent;
//getting the top left coordinate
$c1 = array("x"=>($width-$cropWidth)/2, "y"=>($height-$cropHeight)/2);
// resample
// if you want bigger thumbnails, change this value, it is defaulted at 100 px

$thumbSize = 100;
//create GD holding image object
$thumb = imagecreatetruecolor($thumbSize, $thumbSize);
//copy the cropped chunk of target file into out 100 x 100 thuumbnail
imagecopyresampled($thumb,$current_image, 0, 0, $c1['x'], $c1['y'], $thumbSize, $thumbSize, $cropWidth, $cropHeight);
//output thumbnail to this page.  if you wanted to save the thumbnail somewhere, change the null to a target file
imagejpeg($thumb, null, 50 );
}

// if the file has been passed a target, run function
if(!empty($_GET['target'])) {
generateThumbnail($_GET['target']);
}

?>



[gallery.php]
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
// Path to the folder which contains all of your image folders
$directory = "gallery";
// this is the page that you run the script on
$path = "index.php" . "?gallery=";
// This is the path to "thumbs.php" change this accordingly
$thumb_path = "thumb_gen.php";
// define opendir as a var
$scan = @opendir($directory) or die("Unable to open $directory");
// This array is used for storing the folder list, we will also use it to output a default directory if no gallery has been requested
$folders = array();
//This function lists all deirectories within the path specified by $directory
function genFolders($scan,$directory) {
     // Define these as global vatrs, so that we can use / update them inside the function
     global $path, $folders;
    // Open list tag, if you want to add css classes etc, alter the tag here
    echo '<ul>';
        while (false !== ($file = readdir($scan))) {
        $dir =$directory.'/'.$file;
            if(is_dir($dir) && $file != '.' && $file !='..' ) {
            array_push($folders, $dir);
                    $handle = @opendir($dir) or die("unable to open file $file");
            // output a link containing the folder name    
                //echo '<li><a href="' . $path .  $dir . '">' . $file . '</a></li>';
                    // Run function on file again if it is a folder, to check for other folders within

            genFolders($handle, $dir);
            }
    }

    //close list
        echo "</ul>";
    //close directory
        closedir($scan);
}


// This function outputs the thumbnails
function thumbOutput($target) {
    // define as global vars, so that we can use and update them in the function
    global $thumb_path;
    // open target directory, and output all image files within
    if ($handle = opendir($target)) {
        while (false !== ($file = readdir($handle))) {
            if (strpos($file, '.gif',1) ||strpos($file, '.jpg',1) || strpos($file, '.png',1) || strpos($file, '.JPG',1) || strpos($file, '.PNG',1) || strpos($file, '.GIF',1) ) {
                     echo '<a href="' . $target . '/' . $file . '" rel="launch"><img src="' . $thumb_path . '?target=' . $target . '/' . $file . '" class="thumb_img" border="0" /></a>';
            }
        }

        closedir($handle);
    }
}


// this function checks to see whether a specific directory has been requested, if not it uses the first value of the $folders array
function genThumbs() {
global $folders;
if(!empty($_GET['gallery'])) {
        thumbOutput($_GET['gallery']);
        }
else {
        thumbOutput($folders[0]);
        }
}


?>
 
PHP hulp

PHP hulp

29/04/2024 17:39:05
 
Noppes Homeland

Noppes Homeland

29/08/2010 17:43:39
Quote Anchor link
http://www.php.net/sort
http://www.php.net/exif
http://www.php.net/gd

en denk je nu echt dat je hier beter geholpen gaat worden dan elders?

"Als je zelf iets niet probeerd, zal er ook niets gebeuren!"
Gewijzigd op 29/08/2010 17:45:02 door Noppes Homeland
 
Jelmer -

Jelmer -

29/08/2010 18:46:09
Quote Anchor link
Als je al je foto's wilt sorteren zal je eerst een lijst (array) van alle foto's moeten maken alvorens je ze met elkaar kan vergelijken om te sorteren. Dus in plaats van echo'en zou je beter functies kunnen maken die een array teruggeven. Die arrays wil je dan gesorteerd hebben op alfabet.

Een wat uitgebreider voorbeeld waarin ik een map volledig scan, en en iedere map die ik daarin doorloop op het eind sorteer met ksort. En nu heb ik het scannen en het genereren van de html-lijst van elkaar losgehaald.
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

error_reporting(E_ALL ^ E_STRICT);
ini_set('display_errors', true);

function
is_dot($filename)
{

    return substr($filename, 0, 1) == '.';
}

function
is_imagefile($filename)
{

    return in_array(strtolower(extension($filename)),
        array('jpeg', 'jpg', 'gif', 'png'));
}

function
extension($filename)
{

    return ($dot_pos = strrpos($filename, '.')) !== false
        ? substr($filename, $dot_pos + 1)
        :
null;
}

function
scan_directory($dirname, &$scanned_directories = array())
{

    $files = array();
    
    // Om ervoor te zorgen dat je niet recursief gaat scannen wanneer er symlinks
    // in je mappen staan (bijv. logs die verwijst naar ../logs of ./ oid)

    $scanned_directories[] = fileinode($dirname);
    
    $files_in_directory = scandir($dirname);
    
    // Als ik de map niet kon scannen, heeft de rest vd code geen zin: uitstappen.
    if(!$files_in_directory)
        return array();
        
    foreach($files_in_directory as $filename)
    {

        $pathname = $dirname . DIRECTORY_SEPARATOR . $filename;
        
        if(is_dot($filename)) // dot-files overslaan
            continue;
        
        // Als het een map is, doorloop dan die map. Is het een bestand
        // en een plaatje? Sla dan het pad naar dat bestand op.

        if(is_dir($pathname)) {
            // niet doorlopen als we de map al eens gezien hebben
            if(!in_array(fileinode($pathname), $scanned_directories))
                $files[$filename] = scan_directory($pathname, $scanned_directories);
        }

        else {
            if(is_imagefile($filename))
                $files[$filename] = $pathname;
        }
    }

    
    // sorteer op bestandsnaam
    ksort($files);
    
    return $files;
}

function
print_recursive(array $files)
{

    echo '<ol>';
    
    foreach($files as $filename => $pathname)
    {

        // count($pathname) om alleen niet-lege mappen te tonen
        if(is_array($pathname) && count($pathname) > 0) {
            echo '<li>map '.$filename.'</li>';
            print_recursive($pathname);
        }

        else {
            echo '<li><img src="'.$pathname.'" title="'.$filename.'"></li>';
        }
    }

    
    echo '</ol>';
}


$files = scan_directory('./wordpress');

print_recursive($files);
?>
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.