This repository was archived by the owner on Aug 27, 2022. It is now read-only.
forked from andreknieriem/photobooth
-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathapplyEffects.php
More file actions
157 lines (129 loc) · 4.74 KB
/
applyEffects.php
File metadata and controls
157 lines (129 loc) · 4.74 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
header('Content-Type: application/json');
require_once('../lib/db.php');
require_once('../lib/config.php');
require_once('../lib/filter.php');
require_once('../lib/polaroid.php');
require_once('../lib/resize.php');
require_once('../lib/collage.php');
if (empty($_POST['file'])) {
die(json_encode([
'error' => 'No file provided',
]));
}
$file = $_POST['file'];
$filename_photo = $config['foldersAbs']['images'] . DIRECTORY_SEPARATOR . $file;
$filename_keying = $config['foldersAbs']['keying'] . DIRECTORY_SEPARATOR . $file;
$filename_tmp = $config['foldersAbs']['tmp'] . DIRECTORY_SEPARATOR . $file;
$filename_thumb = $config['foldersAbs']['thumbs'] . DIRECTORY_SEPARATOR . $file;
$frame_path = __DIR__ . DIRECTORY_SEPARATOR .$config['take_frame_path'];
$collage_frame_path = __DIR__ . DIRECTORY_SEPARATOR .$config['take_collage_frame_path'];
$collage_background = __DIR__ . DIRECTORY_SEPARATOR .$config['collage_background'];
$picture_permissions = $config['picture_permissions'];
if (isset($_POST['isCollage']) && $_POST['isCollage'] === 'true') {
$collageBasename = substr($filename_tmp, 0, -4);
$collageSrcImagePaths = [];
for ($i = 0; $i < 4; $i++) {
$collageSrcImagePaths[] = $collageBasename . '-' . $i . '.jpg';
}
if (!createCollage($collageSrcImagePaths, $filename_tmp, $config['take_collage_frame'], $config['take_collage_frame_always'], $collage_frame_path, $collage_background)) {
die(json_encode([
'error' => 'Could not create collage'
]));
}
if (!$config['keep_images']) {
foreach ($collageSrcImagePaths as $tmp) {
unlink($tmp);
}
}
}
if (!file_exists($filename_tmp)) {
die(json_encode([
'error' => 'File does not exist'
]));
}
// Only jpg/jpeg are supported
$imginfo = getimagesize($filename_tmp);
$mimetype = $imginfo['mime'];
if ($mimetype != 'image/jpg' && $mimetype != 'image/jpeg') {
die(json_encode([
'error' => 'The source file type ' . $mimetype . ' is not supported'
]));
}
$imageResource = imagecreatefromjpeg($filename_tmp);
$imageModified = false;
if (!$imageResource) {
die(json_encode([
'error' => 'Could not read jpeg file. Are you taking raws?',
]));
}
if (!isset($_POST['filter'])) {
die(json_encode([
'error' => 'No filter provided'
]));
}
$image_filter = false;
if (!empty($_POST['filter']) && $_POST['filter'] !== 'plain') {
$image_filter = $_POST['filter'];
}
// apply filter
if ($image_filter) {
applyFilter($image_filter, $imageResource);
$imageModified = true;
}
if ($config['pictureRotation'] !== '0') {
$rotatedImg = imagerotate($imageResource, $config['pictureRotation'], 0);
$imageResource = $rotatedImg;
$imageModified = true;
}
if ($config['polaroid_effect']) {
$polaroid_rotation = $config['polaroid_rotation'];
$imageResource = effectPolaroid($imageResource, $polaroid_rotation, 200, 200, 200);
$imageModified = true;
}
if ($config['take_frame'] && $_POST['isCollage'] !== 'true') {
$frame = imagecreatefrompng($frame_path);
$frame = resizePngImage($frame, imagesx($imageResource), imagesy($imageResource));
$x = (imagesx($imageResource)/2) - (imagesx($frame)/2);
$y = (imagesy($imageResource)/2) - (imagesy($frame)/2);
imagecopy($imageResource, $frame, $x, $y, 0, 0, imagesx($frame), imagesy($frame));
$imageModified = true;
}
if ($config['chroma_keying']) {
$chromaCopyResource = resizeImage($imageResource, 1500, 1000);
imagejpeg($chromaCopyResource, $filename_keying, $config['jpeg_quality_chroma']);
imagedestroy($chromaCopyResource);
}
// image scale, create thumbnail
$thumbResource = resizeImage($imageResource, 500, 500);
imagejpeg($thumbResource, $filename_thumb, $config['jpeg_quality_thumb']);
imagedestroy($thumbResource);
if ($imageModified || $config['jpeg_quality_image'] >= 0 && $config['jpeg_quality_image'] < 100) {
imagejpeg($imageResource, $filename_photo, $config['jpeg_quality_image']);
// preserve jpeg meta data
if ($config['preserve_exif_data'] && $config['exiftool']['cmd']) {
$cmd = sprintf($config['exiftool']['cmd'], $filename_tmp, $filename_photo);
exec($cmd, $output, $returnValue);
if ($returnValue) {
die(json_encode([
'error' => 'exiftool returned with an error code',
'cmd' => $cmd,
'returnValue' => $returnValue,
'output' => $output,
]));
}
}
} else {
copy($filename_tmp, $filename_photo);
}
if (!$config['keep_images']) {
unlink($filename_tmp);
}
imagedestroy($imageResource);
// insert into database
appendImageToDB($file);
// Change permissions
chmod($filename_photo, octdec($picture_permissions));
echo json_encode([
'file' => $file,
]);