Initial commit
[pics.git] / upload.php
1 <?php
2
3 require_once __DIR__ . '/db.php';
4 require_once __DIR__ . '/auth.php';
5 require_once __DIR__ . '/uuid.php';
6
7 if ($_SERVER['REQUEST_METHOD'] === 'POST') {
8 $user = check_auth();
9 $db = get_db();
10 $db->exec('BEGIN TRANSACTION');
11 $post_id = uuid_v4();
12 $post_time = time();
13 $images = $_FILES['image'];
14 $num_files = count($images['name']);
15 for ($i = 0; $i < $num_files; $i++) {
16 $name = $images['name'][$i];
17 $mime_type = $images['type'][$i];
18 $tmp_file = $images['tmp_name'][$i];
19 $error = $images['error'][$i];
20 $size = $images['size'][$i];
21 // strip EXIF
22 $img = new Imagick($tmp_file);
23 // keep ICC for quality
24 $profiles = $img->getImageProfiles("icc", true);
25 $img->stripImage();
26 if(!empty($profiles)) {
27 $img->profileImage('icc', $profiles['icc']);
28 }
29
30 $file_b64 = base64_encode($img->getImageBlob());
31 $pic_id = uuid_v4();
32 $upload_stmt = $db->prepare('INSERT into pics values (:id, :post_id, :mime_type, :b64_bytes)');
33 $upload_stmt->bindValue(':id', $pic_id);
34 $upload_stmt->bindValue(':post_id', $post_id);
35 $upload_stmt->bindValue(':mime_type', $mime_type);
36 $upload_stmt->bindValue(':b64_bytes', $file_b64);
37 $upload_stmt->execute();
38
39 echo "<img src='data:{$mime_type};base64,{$file_b64}' alt='$name'/>";
40 }
41
42 $post_stmt = $db->prepare('INSERT into posts VALUES(:id, :user, :time)');
43 $post_stmt->bindValue(':id', $post_id);
44 $post_stmt->bindValue(':user', $user);
45 $post_stmt->bindValue(':time', $post_time);
46 $post_stmt->execute();
47
48 $db->exec('COMMIT');
49 } else {
50 echo '
51 <html>
52 <body>
53 <form enctype="multipart/form-data" method="POST" action="upload.php">
54 <label>Pick Images:<input type="file" id="upload" name="image[]" multiple>
55 </input></label>
56 <br/>
57 <textarea id="comment" name="comment">Title/comment your post</textarea>
58 <br/>
59 <input type="submit" value="Upload"/>
60 </form>
61 </body>
62 </html>
63 ';
64 }