]> git.bts.cx Git - cx.git/blob - cx/lib/images.php
Fix CSS for images
[cx.git] / cx / lib / images.php
1 <?php
2
3 cx_require('lib', 'db.php');
4 cx_require('lib', 'setup.php');
5 cx_require('lib', 'user_data.php');
6
7
8 class Image {
9         public $id;
10         public $uid;
11         public $alt_text;
12         public $url;
13
14         public function __construct($dict) {
15                 $this->id = $dict['image_id']; // FIXME, hide when not used?
16                 $this->uid = $dict['image_uid'];
17                 $this->alt_text = $dict['image_alt_text'];
18                 $this->url = $this->uid . '.' . $dict['image_type'];
19         }
20
21         public function get_permalink_path() {
22                 $permalink = '/data/images/' . $this->url;
23                 return $permalink;
24         }
25 }
26
27 function cx_images_add_image($site_id, $alt_text, $image_path, $image_original_filename) {
28         $path_parts = pathinfo($image_original_filename);
29
30         $uid = hash_file("sha256", $image_path);
31
32         $target_name = $uid . "." . $path_parts['extension'];
33         $path = cx_user_data_path('images', $target_name);
34
35         move_uploaded_file($image_path, $path);
36
37         $creation_time = $update_time = time();
38         
39         $sql = 'INSERT INTO images (
40                         image_site_id,
41                         image_creation_time,
42                         image_update_time,
43                         image_uid,
44                         image_type,
45                         image_alt_text)
46                 VALUES (?, ?, ?, ?, ?, ?);';
47         cx_db_exec($sql, $site_id, $creation_time, $update_time, $uid, $path_parts['extension'], $alt_text);
48 }
49
50 function cx_images_get(int $limit = 0) {
51         $sql = 'SELECT
52                 image_id,
53                 image_uid,
54                 image_type,
55                 image_alt_text
56                 FROM images
57                 ORDER BY image_creation_time DESC';
58
59         if ($limit > 0) {
60                 $sql .= ' LIMIT ' . $limit;
61         }
62
63         $sql .= ';';
64
65         foreach (cx_db_query($sql) as $image) {
66                 $i = new Image($image);
67                 yield $i;
68         }
69 }
70
71 function cx_images_find_image($image_id) {
72         $sql = 'SELECT
73                 image_id,
74                 image_uid,
75                 image_type,
76                 image_alt_text
77                 FROM images
78                 WHERE image_id == ?
79                 OR image_uid LIKE ?
80                 LIMIT 1;';
81
82         foreach (cx_db_query($sql, $image_id, $image_id . "%") as $image) {
83                 $i = new Image($image);
84                 return $i;
85         }
86
87         return null;
88 }
89
90 cx_setup_register(1, function() {
91         cx_db_exec('CREATE TABLE images (
92                         image_id INTEGER PRIMARY KEY,
93                         image_site_id INTEGER,
94                         image_creation_time INTEGER,
95                         image_update_time INTEGER,
96                         image_uid STRING,
97                         image_type STRING,
98                         image_alt_text STRING,
99
100                         FOREIGN KEY(image_site_id) REFERENCES sites(site_id)
101                 );');
102
103         $image_dir = cx_user_data_path('images');
104         if (is_dir($image_dir) == false) {
105                 mkdir($image_dir, recursive: true);
106         }
107 });