-
Notifications
You must be signed in to change notification settings - Fork 120
/
QRCodeController.php
75 lines (67 loc) · 2.09 KB
/
QRCodeController.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
<?php
/*
* UrlHum (https://urlhum.com)
*
* @link https://github.com/urlhum/UrlHum
* @copyright Copyright (c) 2019 Christian la Forgia
* @license https://github.com/urlhum/UrlHum/blob/master/LICENSE.md (MIT License)
*/
namespace App\Http\Controllers;
use App\Url;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Storage;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
/**
* Controller handling creating/fetching the QR code associated with the short URL.
*
* Class QRCodeController
* @author Michael Lindahl <[email protected]>
*/
class QRCodeController
{
/**
* Retrieves the QR Code in svg format for the short URL.
*
* @param string $shortUrl
* @return Application|ResponseFactory|Response
* @throws FileNotFoundException
*/
public function svg(string $shortUrl)
{
$url = Url::where('short_url', $shortUrl)->firstOrFail();
return $this->qrCode($url, 'svg', 'image/svg+xml');
}
/**
* Retrieves the QR Code in png format for the short URL.
*
* @param string $shortUrl
* @return Application|ResponseFactory|Response
* @throws FileNotFoundException
*/
public function png(string $shortUrl)
{
$url = Url::where('short_url', $shortUrl)->firstOrFail();
return $this->qrCode($url, 'png', 'image/png');
}
/**
* @param Url $url
* @param $format
* @param $contentType
* @return Application|ResponseFactory|Response
* @throws FileNotFoundException
*/
private function qrCode(Url $url, $format, $contentType)
{
$path = 'qrcodes/'.$url->short_url.'.'.$format;
if (Storage::exists($path)) {
$qrCode = Storage::get($path);
} else {
$qrCode = QrCode::format($format)->size(300)->generate(route('click', $url));
Storage::put($path, $qrCode);
}
return response($qrCode)->header('Content-Type', $contentType);
}
}