-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_from_cam.php
53 lines (43 loc) · 1.5 KB
/
create_from_cam.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
<?php
require_once "./config/database.php";
// Login information
session_start();
if (!isset($_SESSION['loggued_on_user']))
return ;
$login = $_SESSION['loggued_on_user'];
// Chose image name
$img_name = $login . "_" . substr(hash("whirlpool", rand(0, 10000)), 1, 5);
// Create artwork
$rawData = json_decode($_POST['imgBase64'])->image;
$filteredData = str_replace('data:image/png;base64,', '', $rawData);
$filteredData = str_replace(' ', '+', $filteredData);
$unencoded = base64_decode($filteredData);
$prez_name = $_POST['prez'];
var_dump($prez_name);
//Create the image
file_put_contents('./src/img/usr/' . $img_name . '.png', $unencoded);
$im1 = imagecreatefrompng("./src/img/usr/$img_name.png");
imageflip($im1, IMG_FLIP_HORIZONTAL);
$im2 = imagecreatefrompng("./src/img/vote_" . $prez_name . ".png");
imagecopy($im1, $im2, 0, 0, 0, 0, 600, 450);
imagepng($im1, "./src/img/usr/$img_name.png");
imagedestroy($im1);
imagedestroy($im2);
// Adding the photo path in the photos table
try {
$dbh = new PDO($DB_DSN, $DB_USER, $DB_PASSWORD);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
return ;
}
$sql = "INSERT INTO Photos (user_login, img_name) VALUES (:user_login, :photo_path);";
$sth = $dbh->prepare($sql);
$sth->execute(array(':user_login' => $login, ':photo_path' => $img_name));
// // Debugging
// echo "\nPDOStatement::errorInfo():\n";
// $arr = $sth->errorInfo();
// print_r($arr);
//
$dbh = null;
?>