Completed
Push — master ( 600996...668d7d )
by Roberto
04:33 queued 02:10
created

DaCommon::imagePNGtoJPG()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 10
cp 0
crap 2
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace NFePHP\DA\Common;
4
5
use NFePHP\DA\Legacy\Common;
6
7
class DaCommon extends Common
8
{
9
10
    protected $debugmode;
11
    protected $orientacao = 'P';
12
    protected $force;
13
    protected $papel = 'A4';
14
    protected $margsup = 2;
15
    protected $margesq = 2;
16
    protected $wPrint;
17
    protected $hPrint;
18
    protected $xIni;
19
    protected $yIni;
20
    protected $maxH;
21
    protected $maxW;
22
    protected $fontePadrao = 'times';
23
    protected $aFont = ['font' => 'times', 'size' => 8, 'style' => ''];
24
    protected $creditos;
25
26
    /**
27
     * Ativa ou desativa o modo debug
28
     *
29
     * @param bool $activate Ativa ou desativa o modo debug
30
     *
31
     * @return bool
32
     */
33
    public function debugMode($activate = null)
34
    {
35
        if (isset($activate) && is_bool($activate)) {
36
            $this->debugmode = $activate;
37
        }
38
        if ($this->debugmode) {
39
            //ativar modo debug
40
            error_reporting(E_ALL);
41
            ini_set('display_errors', 'On');
42
        } else {
43
            //desativar modo debug
44
            error_reporting(0);
45
            ini_set('display_errors', 'Off');
46
        }
47
        return $this->debugmode;
48
    }
49
50
    /**
51
     * Renderiza o pdf e retorna como raw
52
     *
53
     * @return string
54
     */
55
    public function render()
56
    {
57
        if (empty($this->pdf)) {
0 ignored issues
show
Bug introduced by
The property pdf does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
58
            $this->monta();
0 ignored issues
show
Bug introduced by
The method monta() does not seem to exist on object<NFePHP\DA\Common\DaCommon>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
        }
60
        if (!$this->debugmode) {
61
            return $this->pdf->getPdf();
62
        }
63
        throw new \Exception("Modo Debug Ativado");
64
    }
65
66
    /**
67
     * Add the credits to the integrator in the footer message
68
     *
69
     * @param string $message Mensagem do integrador a ser impressa no rodapé das paginas
70
     *
71
     * @return void
72
     */
73
    public function creditsIntegratorFooter($message = '')
74
    {
75
        $this->creditos = trim($message);
76
    }
77
78
    /**
79
     *
80
     * @param string $font
81
     */
82
    public function setFontType(string $font = 'times')
83
    {
84
        $this->aFont['font'] = $font;
85
    }
86
87
    /**
88
     * Seta as margens superior e esquerda
89
     * a margem direita é igual a esquerda e
90
     * a inferior é igual a superior
91
     * @param int $margSup
92
     * @param int $margEsq
93
     *
94
     * @return void
95
     */
96
    public function margins(int $margSup = null, int $margEsq = null)
97
    {
98
        $this->margsup = $margSup ?? 2;
99
        $this->margesq = $margEsq ?? 2;
100
    }
101
102
    /**
103
     * Seta o tamanho da fonte
104
     *
105
     * @param int $size
106
     *
107
     * @return void
108
     */
109
    protected function setFontSize(int $size = 8)
110
    {
111
112
        $this->aFont['size'] = $size;
113
    }
114
115
    /**
116
     *
117
     * @param string $style
118
     */
119
    protected function setFontStyle(string $style = '')
120
    {
121
        $this->aFont['style'] = $style;
122
    }
123
124
    /**
125
     * Seta a orientação
126
     *
127
     * @param string $force
128
     * @param string $tpImp
129
     *
130
     * @return void
131
     */
132
    protected function setOrientationAndSize($force = null, $tpImp = null)
133
    {
134
        $this->orientacao = 'P';
135
        if (!empty($force)) {
136
            $this->orientacao = $force;
137
        } elseif (!empty($tpImp)) {
138
            if ($tpImp == '2') {
139
                $this->orientacao = 'L';
140
            } else {
141
                $this->orientacao = 'P';
142
            }
143
        }
144
        if (strtoupper($this->papel) == 'A4') {
145
            $this->maxW = 210;
146
            $this->maxH = 297;
147
        } else {
148
            $this->papel = 'legal';
149
            $this->maxW = 216;
150
            $this->maxH = 355;
151
        }
152
        if ($this->orientacao == 'L') {
153
            if (strtoupper($this->papel) == 'A4') {
154
                $this->maxH = 210;
155
                $this->maxW = 297;
156
            } else {
157
                $this->papel = 'legal';
158
                $this->maxH = 216;
159
                $this->maxW = 355;
160
            }
161
        }
162
        $this->wPrint = $this->maxW - $this->margesq * 2;
163
        $this->hPrint = $this->maxH - $this->margsup - 5;
164
    }
165
    
166
    protected function imagePNGtoJPG($original)
167
    {
168
        $image = imagecreatefrompng($original);
169
        ob_start();
170
        imagejpeg($image, null, 100);
171
        imagedestroy($image);
172
        $stringdata = ob_get_contents(); // read from buffer
173
        ob_end_clean();
174
        return 'data://text/plain;base64,'.base64_encode($stringdata);
175
    }
176
}
177