-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.php
177 lines (118 loc) · 6.45 KB
/
process.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
function addLogo($selectedImage, $logoImagePath, $outputImagePath,$outputStoryImagePath)
{
try {
if (empty($logoImagePath)) {
throw new ImagickException('Diretório da logo não foi configurado.');
}
if($_POST['selectedStoryImage'] && isset($_POST['selectedStoryImage']))
{
$story = new Imagick();
$selectedStoryImage = __DIR__.'/images/storyImages/'.$_POST['selectedStoryImage'];
if (file_exists($selectedStoryImage)) {
$story->readImage($selectedStoryImage);
$storyWidth = $story->getImageWidth();
$storyHeight = $story->getImageHeight();
} else {
throw new ImagickException('Imagem selecionada não existe.');
}
$logo = new Imagick($logoImagePath);
$logoWidth = 200;
$logoHeight = 200;
// Resize the logo image
$logo->scaleImage($logoWidth, $logoHeight, true);
// Calculate the position to place the logo in the bottom left corner
$positionX = 0; // Left margin
$positionY = $storyHeight - $logoHeight - 246; // Bottom margin
$story->compositeImage($logo, Imagick::COMPOSITE_OVER, $positionX, $positionY);
// Set the image format to png and quality to 90 (adjust as needed)
$story->setImageFormat('png');
$story->setImageCompressionQuality(90);
// Save the result image to the output path
$story->writeImage($outputStoryImagePath);
header('Content-disposition: inline');
// Destroy the Imagick objects to free up memory
$story->destroy();
$logo->destroy();
}
// Create new Imagick objects for source image and logo
$anuncio = new Imagick();
$selectedImage = __DIR__.'/images/'.$_POST['selectedImage'];
if (file_exists($selectedImage)) {
$anuncio->readImage($selectedImage);
$anuncioWidth = $anuncio->getImageWidth();
$anuncioHeight = $anuncio->getImageHeight();
} else {
throw new ImagickException('Imagem selecionada não existe.');
}
$logo = new Imagick($logoImagePath);
// $logoWidth = $logo->getImageWidth();
// $logoHeight = $logo->getImageHeight();
$logoWidth = 200;
$logoHeight = 200;
// Resize the logo image
$logo->scaleImage($logoWidth, $logoHeight, true);
// Calculate the position to place the logo in the bottom right corner
$positionX = 170; // Left margin
$positionY = $anuncioHeight - $logoHeight - 11; // Bottom margin
$anuncio->compositeImage($logo, Imagick::COMPOSITE_OVER, $positionX, $positionY);
// Set the image format to JPEG and quality to 90 (adjust as needed)
$anuncio->setImageFormat('png');
$anuncio->setImageCompressionQuality(90);
$anuncio->writeImage($outputImagePath);
// Destroy the Imagick objects to free up memory
$anuncio->destroy();
$logo->destroy();
} catch (ImagickException $e) {
// Handle any exceptions that occur during the process
echo 'Error: ' . $e->getMessage();
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Check if a file was uploaded
if (isset($_FILES['logo']) && $_FILES['logo']['error'] === UPLOAD_ERR_OK) {
$selectedImage = '/images/'.$_POST['selectedImage'];
$selectedStoryImage = '/images/storyImages/'.$_POST['selectedStoryImage'];
$logoTmpPath = $_FILES['logo']['tmp_name']; // caminho temporario
$logoFileName = $_FILES['logo']['name']; // nome do arquivo original
$logoImagePath = __DIR__.'/images/uploads/logos/' . $logoFileName;
$outputImagePath = __DIR__.'/images/imagensGeradas/result_image.png';
$outputStoryImagePath = __DIR__.'/images/imagensGeradas/story/result_story_image.png';
// echo $selectedImage;
// echo "saida imagem selecionada:", $outputImagePath;
// echo "<br><br>";
// echo "saida imagem selecionada para story:", $outputStoryImagePath;
// echo "<br><br>";
// exit;
// echo "caminho temporario da logo:", $logoTmpPath;
// echo "<br><br>";
// echo "imagem da logo:", $logoImagePath;
// echo "<br><br>";
// echo "caminho de saida da imagem:", $outputImagePath;
// echo "<br><br>";
// echo "caminho de saida do story:", $outputStoryImagePath;
// echo "<br><br>";
// exit;
// Move the uploaded logo file to the permanent location
move_uploaded_file($logoTmpPath, $logoImagePath);
// Process the image and add the watermark/logo
addLogo($selectedImage, $logoImagePath, $outputImagePath, $outputStoryImagePath);
header('Content-disposition: inline');
if($_POST['selectedStoryImage'] && isset($_POST['selectedStoryImage']))
{
header('Content-disposition: inline');
echo '<h1>Resultado Imagem de Story :</h1>';
echo '<img src="data:image/png;base64,' . base64_encode(file_get_contents($outputStoryImagePath)) . '" alt="Result Image" width="200" height="300"><br><br>';
echo '<a href="' . $outputStoryImagePath . '" download>Download</a>';
}
// Display the result and provide a download link
echo '<h1>Resultado Imagem :</h1>';
echo '<img src="data:image/png;base64,' . base64_encode(file_get_contents($outputImagePath)) . '" alt="Result Image" width="400" height="400"><br><br>';
echo '<a href="' . $outputImagePath . '" download>Download</a>';
exit;
} else {
echo 'Error: Por favor, selecione uma imagem para logo.';
}
} else {
echo 'Error: Requisição inválida.';
}