-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
133 lines (88 loc) · 3.3 KB
/
index.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
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
$app = new Silex\Application();
//$app['debug'] = true;
$app->register(new Silex\Provider\MonologServiceProvider(), array(
'monolog.logfile' => __DIR__ . '/log/development.log',
));
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__ . '/views',
));
$app->get('/', function () use ($app) {
$images = array();
if ($handle = opendir(__DIR__ . '/images/')) {
while (false !== ($entry = readdir($handle))) {
$images[] = $entry;
}
closedir($handle);
}
return $app['twig']->render('master_page.twig', $images);
});
$app->post('/upload', function (Request $request) use ($app) {
$new_height = 200;
$padding = 10; //Отступ от края
$response = new Response();
try {
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
$file = $request->files->get('payload');
$rightFileType = preg_match('/^text\/plain$/', $file->getMimeType());
if ($rightFileType) {
$handle = fopen($file->getRealPath(), 'r');
if ($handle) {
$watermark = imagecreatefrompng(__DIR__ . '/watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesY($watermark);
$errors = $images = array(); //Неуспешные загрузки
while (false !== ($pathToImage = fgets($handle))) {
#region Scale
$pathToImage = trim($pathToImage,"\r\n");
$filename = pathinfo($pathToImage, PATHINFO_BASENAME);
$filename = urldecode($filename);
$destination = __DIR__.'/images/'. $filename;
if(file_exists($destination)){
$errors[] = "File already exists: " . $filename;
$images[] = $filename;
continue;
}
$file = file_get_contents($pathToImage);
if(!$file){
$errors[] = "Can't load file: " . $pathToImage;
continue;
}
$image = imagecreatefromstring($file);
$image_width = imagesx($image);
$image_height = imagesy($image);
$new_width = $image_width / $image_height * $new_height;
$scaled_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($scaled_image, $image, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
#endregion
imagecopy($scaled_image, $watermark, $new_width - $watermark_width - $padding, $new_height - $watermark_height - $padding, 0, 0, $watermark_width, $watermark_height);
$images[] = $filename;
switch (pathinfo($pathToImage, PATHINFO_EXTENSION)) {
case 'png':
imagepng($scaled_image, $destination);
break;
case 'jpeg':
case 'jpg':
imagejpeg($scaled_image, $destination);
break;
}
}
$response->setStatusCode(201);
$response->setContent(json_encode(array('files' => $images, 'errors' => $errors)));
} else {
throw new \Symfony\Component\Filesystem\Exception\IOException;
}
} else {
throw new \Symfony\Component\Filesystem\Exception\IOException("Wrong file format.");
}
} catch (IOExceptionInterface $e) {
$response->setStatusCode(500);
$response->setContent("An error occurred while creating your directory at " . $e->getPath());
}
return $response;
});
$app->run();