-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimagesave.php
84 lines (82 loc) · 2.56 KB
/
imagesave.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
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
<?php
// requires php8
define('UPLOAD_DIR', 'pictures');
$dir = time();
// Check if the directory already exists
if (!is_dir(UPLOAD_DIR)) {
if (!mkdir(UPLOAD_DIR . $dir, 0755, true)) {
die('Failed to create directories: ' . UPLOAD_DIR . $dir);
}
}
$post = $_POST['img'] ?? '';
$images = explode('data:image/png;base64,', $post);
$snapstr = UPLOAD_DIR . '/snapstr.png';
if(!copy(UPLOAD_DIR . $dir . '/snapstr.png', $snapstr)) {
print "IMAGE DUPLICATE FAILED";
return FALSE;
}
$z = 100;
foreach($images as $img) {
if (strlen($img) > 1) {
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . $dir . '/' . uniqid() . '.png';
$success = file_put_contents($file, $data);
// Create image instances
$dest = imagecreatefrompng($snapstr);
$src = imagecreatefrompng($file);
$src = image_flip($src, 'horiz');
$x = imagesx($src);
$y = imagesy($src);
// Copy and merge
if(!imagecopy($dest, $src, 20, $z, 0, 0, $x, $y)) {
print "IMAGE COPY FAILED";
return FALSE;
}
$z = $z + imagesy($src) + 15;
// Output and free from memory
header('Content-Type: image/png');
if(!imagepng($dest, $snapstr)) {
print "IMAGE CREATE FAILED";
return FALSE;
}
imagedestroy($dest);
imagedestroy($src);
}
}
print $success ? '/' . $snapstr : 'Unable to save the file.';
return false;
function image_flip($img, $type=''){
$width = imagesx($img);
$height = imagesy($img);
$dest = imagecreatetruecolor($width, $height);
switch($type){
case '':
return $img;
break;
case 'vert':
for($i=0;$i<$height;$i++){
imagecopy($dest, $img, 0, ($height - $i - 1), 0, $i, $width, 1);
}
break;
case 'horiz':
for($i=0;$i<$width;$i++){
imagecopy($dest, $img, ($width - $i - 1), 0, $i, 0, 1, $height);
}
break;
case 'both':
for($i=0;$i<$width;$i++){
imagecopy($dest, $img, ($width - $i - 1), 0, $i, 0, 1, $height);
}
$buffer = imagecreatetruecolor($width, 1);
for($i=0;$i<($height/2);$i++){
imagecopy($buffer, $dest, 0, 0, 0, ($height - $i -1), $width, 1);
imagecopy($dest, $dest, 0, ($height - $i - 1), 0, $i, $width, 1);
imagecopy($dest, $buffer, 0, $i, 0, 0, $width, 1);
}
imagedestroy($buffer);
break;
}
return $dest;
}
?>