1 | <?php |
---|
2 | class Images { |
---|
3 | function load($data){ |
---|
4 | $db = new SQLiteDatabase("sql/imgorg.db"); |
---|
5 | $tags = $data->tags; |
---|
6 | $album = $data->album; |
---|
7 | $qry = 'select i.filename as filename, i.url as url, i.id as id from Images i'; |
---|
8 | $where = array(); |
---|
9 | if ($tags) { |
---|
10 | for ($i = 0;$i < sizeof($tags);$i++) { |
---|
11 | $qry .= ' INNER JOIN Images_Tags it'.$i.' ON i.id = it'.$i.'.image_id'; |
---|
12 | array_push($where,' it'.$i.'.tag_id = "'.$tags[$i].'"'); |
---|
13 | } |
---|
14 | } |
---|
15 | if ($album) { |
---|
16 | $qry .= ' INNER JOIN Albums a ON i.album_id = a.id'; |
---|
17 | array_push($where, ' a.id ="'.$album.'"'); |
---|
18 | } |
---|
19 | if ($where) { |
---|
20 | $qry .= ' WHERE'.join(" AND", $where); |
---|
21 | } |
---|
22 | $res = $db->query($qry); |
---|
23 | return $res->fetchAll(); |
---|
24 | // return $qry; |
---|
25 | } |
---|
26 | |
---|
27 | function upload($data, $files){ |
---|
28 | $name = $files["Filedata"]["name"]; |
---|
29 | $db = new SQLiteDatabase("sql/imgorg.db"); |
---|
30 | $db->queryExec('INSERT INTO Images (filename, url) VALUES("'.$name.'","images/'.$name.'")'); |
---|
31 | $q = $db->query('SELECT * FROM Images WHERE filename = "'.$name.'"'); |
---|
32 | move_uploaded_file($files["Filedata"]["tmp_name"],"../images/".$name); |
---|
33 | |
---|
34 | return array( |
---|
35 | 'data' => $files["Filedata"], |
---|
36 | 'res' => $q->fetchObject() |
---|
37 | //, |
---|
38 | //'test' => $phm->getImageQuality() |
---|
39 | ); |
---|
40 | } |
---|
41 | |
---|
42 | function addToAlbum($data) { |
---|
43 | $images = $data->images; |
---|
44 | $album = $data->album; |
---|
45 | $db = new SQLiteDatabase("sql/imgorg.db"); |
---|
46 | for ($i = 0;$i < sizeof($images);$i++) { |
---|
47 | // $db->queryExec('INSERT INTO Albums_Images (image_id, album_id) VALUES ("'.$images[$i].'","'.$album.'")'); |
---|
48 | $db->queryExec('UPDATE Images SET album_id = "'.$album.'" WHERE id ="'.$images[$i].'"'); |
---|
49 | } |
---|
50 | return array('success' => true, 'images' => $images, 'album' => $album); |
---|
51 | } |
---|
52 | |
---|
53 | function tagImage($data) { |
---|
54 | $images = $data->images; |
---|
55 | $tag = $data->tag; |
---|
56 | $db = new SQLiteDatabase("sql/imgorg.db"); |
---|
57 | // if it is a known tag the id is sent, otherwise a new string is, so we need to insert |
---|
58 | if (!is_numeric($tag)) { |
---|
59 | $db->queryExec('INSERT INTO Tags (text) VALUES ("'.$tag.'")'); |
---|
60 | $q = $db->query('SELECT id FROM Tags WHERE text = "'.$tag.'"'); |
---|
61 | $tag = $q->fetchObject()->id; |
---|
62 | } |
---|
63 | for ($i = 0;$i < sizeof($images);$i++) { |
---|
64 | $db->queryExec('INSERT INTO Images_Tags (image_id, tag_id) VALUES ("'.$images[$i].'","'.$tag.'")'); |
---|
65 | } |
---|
66 | return array('success' => true, 'images' => $images, 'tag' => $tag); |
---|
67 | } |
---|
68 | |
---|
69 | function rename($data) { |
---|
70 | $db = new SQLiteDatabase("sql/imgorg.db"); |
---|
71 | $image = $data->image; |
---|
72 | $name = $data->name; |
---|
73 | $url = $data->url; |
---|
74 | $urls = split('/',$url); |
---|
75 | array_pop($urls); |
---|
76 | $newUrl = (join('/',$urls)).'/'.$name; |
---|
77 | |
---|
78 | $db->queryExec('UPDATE Images SET url = "'.$newUrl.'", filename = "'.$name.'" WHERE id = "'.$image.'"'); |
---|
79 | rename('../'.$url, '../'.$newUrl); |
---|
80 | |
---|
81 | return array('image' => $image, 'name' => $name, 'url' => $newUrl); |
---|
82 | } |
---|
83 | |
---|
84 | function remove($data) { |
---|
85 | $db = new SQLiteDatabase("sql/imgorg.db"); |
---|
86 | $images = $data->images; |
---|
87 | for ($i = 0;$i < sizeof($images);$i++) { |
---|
88 | $res = $db->query('SELECT url FROM Images where id ="'.$images[$i].'"'); |
---|
89 | $url = $res->fetchObject()->url; |
---|
90 | unlink('../'.$url); |
---|
91 | $db->queryExec('DELETE FROM Images WHERE id ="'.$images[$i].'"'); |
---|
92 | $db->queryExec('DELETE FROM Images_Tags WHERE image_id ="'.$images[$i].'"'); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | function getInfo($data) { |
---|
97 | $db = new SQLiteDatabase("sql/imgorg.db"); |
---|
98 | $image = $data->image; |
---|
99 | $q = $db->query('SELECT url FROM Images WHERE id = "'.$image.'"'); |
---|
100 | $path = $q->fetchObject()->url; |
---|
101 | $ret = exif_read_data('../'.$path); |
---|
102 | return $ret; |
---|
103 | } |
---|
104 | } |
---|