-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.php
executable file
·176 lines (155 loc) · 5.94 KB
/
scan.php
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php /**/ ?>#!/home/nivshah/php5/bin/php
<?php
//goes into src directory
// checks if file has a thumbnail
// if it doesn't, build a thumbnail
include('inc/constants.inc.php');
include('inc/functions.php');
function scangalleries($path) {
$handle = opendir($path);
for(;(false !== ($file = readdir($handle)));) {
if($file != '.' && $file != '..') {
$next_step = $path . '/' . $file;
if(is_dir($next_step)) {
scangalleries($next_step);
}
if(is_file($next_step)) {
$extension = '';
$filetype = exif_imagetype($next_step);
if($filetype == IMAGETYPE_PNG) {
$extension = '.png';
} else if($filetype == IMAGETYPE_JPEG) {
$extension = '.jpg';
} else {
$extension = '';
}
// check if a thumbnail exists
// if it doesn't, make one
// there are a crapload of files in the cache directory. if we created a few random directories and
// populated those with thumbnails, that might be better... how to do this?
$directories = array();
$directories = explode('/',$path);
$cache_subdir = base64_encode(array_pop($directories));
$thumb_name = $cache_subdir . '/' . base64_encode($file) . $extension;
if(!is_file(GALLERY_LOCAL_CACHE . $thumb_name)) {
// no thumbnail, create one
// we have to make directories if they don't exist
if(!is_dir(GALLERY_LOCAL_CACHE . $cache_subdir)) {
mkdir(GALLERY_LOCAL_CACHE . $cache_subdir);
}
$thumb_img = imagecreatetruecolor(GALLERY_THUMBNAIL_WIDTH,GALLERY_THUMBNAIL_HEIGHT);
if($filetype == IMAGETYPE_PNG) {
$src_img = imagecreatefrompng($next_step);
} else if($filetype == IMAGETYPE_JPEG) {
$src_img = imagecreatefromjpeg($next_step);
} else {
// err
}
if(imagesy($src_img) > GALLERY_MAX_HEIGHT) {
$new_width = (GALLERY_MAX_HEIGHT / imagesy($src_img)) * imagesx($src_img);
$temp_img = imagecreatetruecolor($new_width,GALLERY_MAX_HEIGHT);
imagecopyresampled($temp_img,$src_img,0,0,0,0,$new_width,GALLERY_MAX_HEIGHT,imagesx($src_img),imagesy($src_img));
imagecopyresampled($thumb_img,$temp_img,0,0,(imagesx($temp_img) / 2) - (GALLERY_THUMBNAIL_WIDTH / 2),(imagesy($temp_img) / 2) - (GALLERY_THUMBNAIL_HEIGHT / 2),GALLERY_THUMBNAIL_WIDTH,GALLERY_THUMBNAIL_HEIGHT,GALLERY_THUMBNAIL_WIDTH,GALLERY_THUMBNAIL_HEIGHT);
}
else {
imagecopyresampled($thumb_img,$src_img,0,0,(imagesx($src_img) / 2) - (GALLERY_THUMBNAIL_WIDTH / 2),(imagesy($src_img) / 2) - (GALLERY_THUMBNAIL_HEIGHT / 2),GALLERY_THUMBNAIL_WIDTH,GALLERY_THUMBNAIL_HEIGHT,GALLERY_THUMBNAIL_WIDTH,GALLERY_THUMBNAIL_HEIGHT);
}
if($filetype == IMAGETYPE_PNG) {
imagepng($thumb_img,GALLERY_LOCAL_CACHE . $thumb_name);
} else if($filetype == IMAGETYPE_JPEG) {
imagejpeg($thumb_img,GALLERY_LOCAL_CACHE . $thumb_name);
} else {
// err
}
// could check if the file was in the database here
// and if it wasn't, add it.
$web_path = str_replace(GALLERY_LOCAL_HOME.'src/',GALLERY_WEB_HOME,$next_step);
$sql = "select count(*) as c from image_index where img_local_path='$next_step'";
$result = mysql_query($sql);
$result = mysql_fetch_array($result);
if($result['c']==0) {
// add this file to the db
$sql = "insert into image_index (img_web_path,img_local_path,img_thumbnail) VALUES ('$web_path','$next_step','$thumb_name')";
mysql_query($sql);
if(mysql_errno()) {
print mysql_error();
exit;
}
// extract key words and insert them into the db
$keywords = array();
$keywords = extract_xmp_keywords($next_step);
$image_id = mysql_insert_id();
if(!empty($keywords)) {
foreach($keywords as $word) {
addtag_to_db($word, $image_id);
}
}
} else {
// the file is in the database but its thumbnail didn't exist, so we should update the thumbnail
$sql = "update image_index set img_thumbnail='$thumb_name', img_web_path='$web_path' where img_local_path='$next_step'";
mysql_query($sql);
if(mysql_errno()) {
print mysql_error();
exit;
}
}
} else {
// the file's thumbnail exists!
// check if the file is in the database
$web_path = str_replace(GALLERY_LOCAL_HOME.'src/',GALLERY_WEB_HOME,$next_step);
$sql = "select count(*) as c from image_index where img_local_path='$next_step'";
$result = mysql_query($sql);
$result = mysql_fetch_array($result);
if($result['c']==0) {
// add this file to the db
$sql = "insert into image_index (img_web_path,img_local_path,img_thumbnail) VALUES ('$web_path','$next_step','$thumb_name')";
mysql_query($sql);
if(mysql_errno()) {
print mysql_error();
exit;
}
// extract key words and insert them into the db
$keywords = array();
$keywords = extract_xmp_keywords($next_step);
$image_id = mysql_insert_id();
if(!empty($keywords)) {
foreach($keywords as $word) {
addtag_to_db($word, $image_id);
}
}
}
// update thumbnail name
// just in case
$sql = "update image_index set img_thumbnail='$thumb_name', img_web_path='$web_path' where img_local_path='$next_step'";
mysql_query($sql);
if(mysql_errno()) {
print mysql_error();
exit;
}
// extract key words and insert them into the db
$keywords = array();
$keywords = extract_xmp_keywords($next_step);
$image_id = mysql_insert_id();
if(!empty($keywords)) {
foreach($keywords as $word) {
addtag_to_db($word, $image_id);
}
}
}
}
}
}
closedir($handle);
}
$db = mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASSWD);
mysql_select_db(MYSQL_DB,$db);
if($argv[1]) {
$year = $argv[1];
}
if($year) {
scangalleries(GALLERY_LOCAL_HOME . "src/$year");
}
else {
scangalleries(GALLERY_LOCAL_HOME . "src");
}
?>