ApiController.php
4.24 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
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
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers;
use App\pdfModel;
use URL;
use Storage;
use Response;
use File;
Use PDF;
use Illuminate\Support\Facades\Input;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Facade;
use League\Flysystem\Filesystem;
use Curl\Curl;
use Imagick;
class ApiController extends Controller
{
//Takes json data and passes it to model and grabs related files.
public function insert($json)
{
$json = '{"pdf":[{"name":"Sample PDF Document","folder":"Sample","pdfLocation":"http://hotelpodlipou.sk/uploads/files/sample.pdf"}],"changes":[{"label":"none","locationUp":"400","locationRight":90,"pages":"1","content":"logo-sample-text_355-558.jpg","z-index":null,"idstylesPDF":1,"order":"2","style":null,"idchange_typePDF":2,"change_type":"2","fileLocation":"https://image.freepik.com/free-vector/logo-sample-text_355-558.jpg"},{"label":"text style","locationUp":"60","locationRight":290,"pages":"1","content":"519.642.4404 London","z-index":null,"idstylesPDF":2,"order":"1","style":"a:3:{s:11:\"font-family\";s:11:\"TradeGothic\";s:9:\"font-size\";s:2:\"14\";s:10:\"font-color\";s:16:\"cmyk 0 0.6 0.5 0\";}","idchange_typePDF":1,"change_type":"1"}]}';
$apiCallId = new pdfModel();
$apiCallId = $apiCallId->addApiCall('1',$json);
//$apiCallId = "3";
$pdfId = new pdfModel();
$pdfId = $pdfId->addPdf($apiCallId, $json);
$json = json_decode($json);
$storagePath = Storage::disk('public')->getDriver()->getAdapter()->getPathPrefix();
$folder = str_replace(' ', '-', $json->pdf[0]->name); // Replaces all spaces with hyphens.
$folder = preg_replace('/[^A-Za-z0-9\-]/', '', $folder);
if(!is_dir($storagePath . $folder . '/')){
$old = umask(0);
mkdir($storagePath . $folder . '/', 0777);
umask($old);
}
if (isset($json->pdf[0]->pdfLocation)) {
$pdfLocation = $json->pdf[0]->pdfLocation;
$file = explode('/', $pdfLocation);
$file = end($file);
$curl = new Curl();
$curl->download($pdfLocation, $storagePath . $folder . '/' . $file);
$curl->close();
$fileEntry = New FileEntryController();
// echo $storagePath . $folder . '/' . $file;
//$fileEntry->addfile( $folder . '/' . $file);
$im = new Imagick();
$im->setResolution(300, 300); //set the resolution of the resulting jpg
if (! is_readable($storagePath . $folder . '/' . $file)) {
echo 'file not readable';
exit();
}
$im->readImage($storagePath . $folder . '/' . $file); //[0] for the first page
$im->setImageFormat('jpg');
$fileName = $file;
$fileName = explode('.', $fileName);
$im->writeImage($storagePath . $folder . '/' . $fileName[0].'.jpg');
}
foreach ($json->changes as $change) {
if (isset($change->fileLocation)) {
$fileLocation = $change->fileLocation;
$file = explode('/', $fileLocation);
$curl = new Curl();
$curl->download($fileLocation, $storagePath . $folder . '/' . end($file));
$curl->close();
}
}
}
public function getList($json)
{
$rows = new pdfModel();
$rows = $rows->getList($json)->get();
$response = $rows;
$statusCode = 200;
return Response::json($response, $statusCode);
}
public function getApi($json)
{
$statusCode = 200;
$response = include('js/interface.js');
return Response::json($response, $statusCode);
}
public function getPdf($json)
{
$rows = new pdfModel();
$rows = $rows->getpdf($json)->get();
$response = $rows;
$statusCode = 200;
return Response::json($response, $statusCode);
}
public function view(File $file)
{
$response = new BinaryFileResponse($file->getAbsolutePath());
$response->headers->set('Content-Disposition', 'inline; filename="' . $file->real_filename . '"');
return $response;
}
}