Passed
Push — master ( b831b6...96f132 )
by John
06:04 queued 21s
created

LatexController::svg()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
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');
0 ignored issues
show
Bug introduced by
Storage::get('latex-svg/...e($ltxsource) . '.svg') of type Illuminate\Contracts\Filesystem\Filesystem is incompatible with the type string expected by parameter $content of Illuminate\Routing\ResponseFactory::make(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

22
        return response()->make(/** @scrutinizer ignore-type */ Storage::get('latex-svg/'.urlencode($ltxsource).'.svg'), 200)->header('Content-Type', 'image/svg+xml');
Loading history...
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');
0 ignored issues
show
Bug introduced by
Storage::get('latex-png/...e($ltxsource) . '.png') of type Illuminate\Contracts\Filesystem\Filesystem is incompatible with the type string expected by parameter $content of Illuminate\Routing\ResponseFactory::make(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        return response()->make(/** @scrutinizer ignore-type */ Storage::get('latex-png/'.urlencode($ltxsource).'.png'), 200)->header('Content-Type', 'image/png');
Loading history...
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'));
0 ignored issues
show
Bug introduced by
Storage::get('latex-svg/...e($ltxsource) . '.svg') of type Illuminate\Contracts\Filesystem\Filesystem is incompatible with the type string expected by parameter $image of Imagick::readImageBlob(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
        $image->readImageBlob(/** @scrutinizer ignore-type */ Storage::get('latex-svg/'.urlencode($ltxsource).'.svg'));
Loading history...
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