-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.php
More file actions
55 lines (41 loc) · 1.88 KB
/
Copy pathbootstrap.php
File metadata and controls
55 lines (41 loc) · 1.88 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
<?php
require __DIR__ . '/vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Terravision\QRCode\QRCodeFactory;
use KPhoen\Provider\NegotiationServiceProvider;
use Terravision\QRCode\Exception\BadRequestAcceptHeaderNotValid;
$app = new Silex\Application();
$app['default.qrcode.size']=150;
$app->register(new NegotiationServiceProvider());
$app["format.negotiator"]->registerFormat('svg', ['image/svg+xml']);
$app["format.negotiator"]->registerFormat('png', ['image/png']);
$app["format.negotiator"]->registerFormat('jpeg', ['image/jpeg', 'image/jpg']);
$app['format.qrcode.priorities'] = ['image/png', 'image/jpg'];
$app->get( '/', function() {
return "You should ask me something to create like /barcode";
});
$app->get( '/{content}', function($content, Request $request) use ($app)
{
$formatNegotiator = $app["format.negotiator"];
$mimeType = $formatNegotiator->getBest($request->headers->get('Accept'), $app['format.qrcode.priorities']);
if (null == $mimeType) {
throw new BadRequestAcceptHeaderNotValid($request->headers->get('Accept'));
}
$format = $formatNegotiator->getFormat($mimeType->getValue());
$size = ((int) $request->query->get('size'))?:$app['default.qrcode.size'];
$factory = new QRCodeFactory();
$qrCode = $factory->createQRCode($content, $size, $format);
return $qrCode;
})
->assert('content', '.+')
->after(function (Request $request, Response $response) use ($app)
{
$formatNegotiator = $app["format.negotiator"];
$mimeType = $formatNegotiator->getBest($request->headers->get('Accept'), $app['format.qrcode.priorities']);
if (null !== $mimeType) {
$headers = ['Content-Type'=>$mimeType->getValue()];
$response->headers->add($headers);
}
});
return $app;