forked from andreknieriem/photobooth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchromakeying_save.php
More file actions
56 lines (46 loc) · 1.6 KB
/
chromakeying_save.php
File metadata and controls
56 lines (46 loc) · 1.6 KB
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
<?php
$my_config = 'my.config.inc.php';
if (file_exists($my_config)) {
require_once('my.config.inc.php');
} else {
require_once('config.inc.php');
}
if($config['file_format_date'] == true) {
$file = date('Ymd_His').'.jpg';
} else {
$file = md5(time()).'.jpg';
}
$uniqid = uniqid();
$filename_photo = $config['folders']['images'] . DIRECTORY_SEPARATOR . $file;
$filename_thumb = $config['folders']['thumbs'] . DIRECTORY_SEPARATOR . $file;
// get data from data.txt
if(!file_exists('data.txt')){
file_put_contents('data.txt', json_encode(array()));
}
$images = json_decode(file_get_contents('data.txt'));
$img = $_POST['imgData'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$image = imagecreatefromstring($data);
imagejpeg($image, $filename_photo, 100);
$image = ResizeJpgImage($image, 500, 500);
imagejpeg($image, $filename_thumb, 100);
imagedestroy($image);
function ResizeJpgImage($image, $max_width, $max_height)
{
$old_width = imagesx($image);
$old_height = imagesy($image);
$scale = min($max_width/$old_width, $max_height/$old_height);
$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);
$new = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
return $new;
}
// insert into database
$images[] = $file;
file_put_contents('data.txt', json_encode($images));
// send imagename to frontend
echo json_encode(array('success' => true, 'img' => $file));
?>