|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use App\Http\Controllers\Controller; |
|
7
|
|
|
use Storage; |
|
8
|
|
|
use Requests; |
|
9
|
|
|
use Imagick; |
|
10
|
|
|
|
|
11
|
|
|
class LatexController extends Controller |
|
12
|
|
|
{ |
|
13
|
|
|
public function svg(Request $request) |
|
14
|
|
|
{ |
|
15
|
|
|
$ltxsource = $request->input('ltxsource'); |
|
16
|
|
|
if (is_null($ltxsource)) { |
|
17
|
|
|
return; |
|
18
|
|
|
} |
|
19
|
|
|
if (!Storage::exists('latex-svg/'.urlencode($ltxsource).'.svg')) { |
|
20
|
|
|
self::generateSVG($ltxsource); |
|
21
|
|
|
} |
|
22
|
|
|
return response()->make(Storage::get('latex-svg/'.urlencode($ltxsource).'.svg'), 200)->header('Content-Type', 'image/svg+xml'); |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function png(Request $request) |
|
26
|
|
|
{ |
|
27
|
|
|
$ltxsource = $request->input('ltxsource'); |
|
28
|
|
|
if (is_null($ltxsource)) { |
|
29
|
|
|
return; |
|
30
|
|
|
} |
|
31
|
|
|
if (!Storage::exists('latex-png/'.urlencode($ltxsource).'.png')) { |
|
32
|
|
|
self::generatePNG($ltxsource); |
|
33
|
|
|
} |
|
34
|
|
|
return response()->make(Storage::get('latex-png/'.urlencode($ltxsource).'.png'), 200)->header('Content-Type', 'image/png'); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private static function generatePNG($ltxsource) |
|
38
|
|
|
{ |
|
39
|
|
|
if (!Storage::exists('latex-svg/'.urlencode($ltxsource).'.svg')) { |
|
40
|
|
|
self::generateSVG($ltxsource); |
|
41
|
|
|
} |
|
42
|
|
|
$image = new Imagick(); |
|
43
|
|
|
$image->readImageBlob(Storage::get('latex-svg/'.urlencode($ltxsource).'.svg')); |
|
|
|
|
|
|
44
|
|
|
$res = $image->getImageResolution(); |
|
45
|
|
|
$x_ratio = $res['x'] / $image->getImageWidth(); |
|
46
|
|
|
$y_ratio = $res['y'] / $image->getImageHeight(); |
|
47
|
|
|
// $ratio=intval(200/$image->getImageHeight()); |
|
48
|
|
|
$ratio=10; |
|
49
|
|
|
$width=$image->getImageWidth()*$ratio; |
|
50
|
|
|
$height=$image->getImageHeight()*$ratio; |
|
51
|
|
|
$image->removeImage(); |
|
52
|
|
|
$image->setResolution($width*$x_ratio, $height*$y_ratio); |
|
53
|
|
|
$image->setBackgroundColor(new \ImagickPixel('transparent')); |
|
54
|
|
|
$image->readImageBlob(Storage::get('latex-svg/'.urlencode($ltxsource).'.svg')); |
|
55
|
|
|
$image->setImageFormat("png32"); |
|
56
|
|
|
Storage::put('latex-png/'.urlencode($ltxsource).'.png', $image->getImageBlob()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private static function generateSVG($ltxsource) |
|
60
|
|
|
{ |
|
61
|
|
|
$contents=str_replace('fill:rgb(0%,0%,0%)', 'fill:rgb(0,0,0)', Requests::get('http://www.tlhiv.org/ltxpreview/ltxpreview.cgi?' . http_build_query([ |
|
62
|
|
|
'width' => 10, |
|
63
|
|
|
'height' => 10, |
|
64
|
|
|
'ltx' => '', |
|
65
|
|
|
'ltxsource' => $ltxsource, |
|
66
|
|
|
'result' => 'preview', |
|
67
|
|
|
'init' => 0, |
|
68
|
|
|
]))->body); |
|
69
|
|
|
Storage::put('latex-svg/'.urlencode($ltxsource).'.svg', $contents); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|