File: import.inc.php

package info (click to toggle)
zoph 0.6-2.1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 1,716 kB
  • ctags: 2,283
  • sloc: php: 8,554; perl: 1,601; sh: 760; sql: 382; python: 338; makefile: 71
file content (152 lines) | stat: -rw-r--r-- 4,764 bytes parent folder | download | duplicates (2)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/*
 * This file is part of Zoph.
 *
 * Zoph is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Zoph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Zoph; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

require_once("exif.inc.php");

function get_files($dir_name) {
    $files = array();

    if ($dir = @opendir($dir_name)) {
        while(($file = readdir($dir)) !== false) {
            if (valid_image($file)) {
                $files[] = $dir_name . '/' . $file;
            }
        }
        closedir($dir);
    }
    else {
        echo translate("Could not open directory") . ": $dir_name<br>\n";
    }

    return $files;
}

function process_images($images, $path, $fields) {

    $date = "";
    $absolute_path = "/" . cleanup_path(IMAGE_DIR . $path);

    echo "<p>" . sprintf(translate("Processing %s image(s)."), count($images)) . "</p>\n";
    $loaded = 0;
    foreach ($images as $image) {

        if (file_exists($image) == false) {
            echo sprintf(translate("Skipping %s: File does not exist."), $image) . "<br>\n";
            continue;
        }
        create_dir("$absolute_path");
        if (minimum_version('4.2.0')) {
            $exif_data = process_exif($image);
            if ((USE_DATED_DIRS) && !(HIER_DATED_DIRS)) {
                $date = cleanup_path(str_replace("-", ".", $exif_data["date"]));
                create_dir($absolute_path . "/" . $date);
            } elseif ((USE_DATED_DIRS) && (HIER_DATED_DIRS)) {
                $date = cleanup_path(str_replace("-", "/", $exif_data["date"]));
                create_dir_recursive($absolute_path . "/" .  $date . "/");
            }
        } else {
            echo "PHP version too old (<4.2.0), not trying to get EXIF info";
        }
        create_dir("$absolute_path" . "/" . "$date" . "/" . THUMB_PREFIX);
        create_dir("$absolute_path" . "/" . "$date" . "/" . MID_PREFIX);
        $image_dir = dirname($image);
        $image_name = basename($image);

        if (cleanup_path($image_dir) != cleanup_path($absolute_path . "/" . $date)) {
            $new_image = $absolute_path . "/" . $date . "/" . $image_name;
            if (!copy($image, $new_image)) {
                echo sprintf(translate("Could not copy %s to %s."), $image, $new_image) . "<br>\n";
                continue;
            }

            echo "$image -> $new_image<br>\n";

            $image = $new_image;
        }
        else {
            echo "$image<br>\n";
        }

        flush();

        $photo = new photo();
        $photo->set("name", $image_name);
        $photo->set("path", cleanup_path($path . "/" . $date));

        //$width = imagesx($img_src);
        //$height = imagesy($img_src);
        $image_info = getimagesize($image);
        $width = $image_info[0];
        $height = $image_info[1];

        if (!$photo->thumbnail()) {
            echo translate("Could not create thumbnail") . ": $image_name<br>\n";
        }

        // first try the insert
        if ($photo->insert()) {

            // then do everything else as an update
            // (which is smart enough to handle the
            // album/category/people tables)

            $photo->set("size", filesize($image));
            $photo->set("width", $width);
            $photo->set("height", $height);

            if ($fields) {
                $photo->set_fields($fields);
            }

            // exif functions introduced in PHP 4.2.0
            if (minimum_version('4.2.0')) {
                $photo->set_fields($exif_data);
            }

            $photo->update($fields);

            $loaded++;
        }
        else {
            echo translate("Insert failed.") . "<br>\n";
        }

    }

    return $loaded;
}

function create_dir($directory) {
    if (file_exists($directory) == false) {
        if (mkdir($directory, DIR_MODE)) {
            echo translate("Created directory") . ": $directory<br>\n";
            return 0;
        }
        else {
            echo translate("Could not create directory") . ": $directory<br>\n";
            return -1;
        }
    }
}

function create_dir_recursive($directory){
  foreach(split('/',$directory) as $subdir) {
    return create_dir($nextdir="$nextdir$subdir/");
  }
}
?>