Completed
Push — master ( 212f49...e1cef3 )
by satoru
05:38 queued 04:29
created

TcpdfWrapper::setImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace TcpdfWrapper;
4
5
use setasign\Fpdi\Tcpdf\Fpdi;
6
use \TCPDF_FONTS;
7
/**
8
* TcpdfWrapper
9
* TcpdfWrapperを記載しやすくするためのラッパー
10
*/
11
class TcpdfWrapper
12
{
13
    private $__pdf;
14
    private $__fonts = [];
15
    private $__tcpdfFonts;
16
17
    /**
18
    * __construct
19
    *
20
    * @author hagiwara
21
    */
22
    public function __construct()
23
    {
24
        $this->__pdf = new Fpdi();
25
        $this->__tcpdfFonts = new TCPDF_FONTS();
26
    }
27
28
    /**
29
    * setPrintHeader
30
    *
31
    * @param boolean $print 出力フラグ
32
    * @author hagiwara
33
    */
34
    public function setPrintHeader($print)
35
    {
36
        $this->__pdf->setPrintHeader($print);
37
    }
38
39
    /**
40
    * setPrintFooter
41
    *
42
    * @param boolean $print 出力フラグ
43
    * @author hagiwara
44
    */
45
    public function setPrintFooter($print)
46
    {
47
        $this->__pdf->setPrintFooter($print);
48
    }
49
50
    /**
51
    * setFont
52
    *
53
    * @param string $name フォント名
54
    * @param string $path フォントパス nullでデフォルトセット
55
    * @author hagiwara
56
    */
57
    public function setFont($name, $path)
58
    {
59
        $this->__fonts[$name] = $this->__tcpdfFonts->addTTFfont($path);
60
    }
61
62
    /**
63
    * addPage
64
    *
65
    * @param string $template テンプレートパス
66
    * @param integer $templateIndex テンプレートページ
67
    * @author hagiwara
68
    */
69
    public function addPage($template, $templateIndex)
70
    {
71
        // ページを追加
72
        $this->__pdf->AddPage();
73
74
        // テンプレートを読み込み
75
        $this->__pdf->setSourceFile($template);
76
77
        // 読み込んだPDFの1ページ目のインデックスを取得
78
        $tplIdx = $this->__pdf->importPage($templateIndex);
79
80
        // 読み込んだPDFの1ページ目をテンプレートとして使用
81
        $this->__pdf->useTemplate($tplIdx, null, null, null, null, true);
82
    }
83
84
    /**
85
    * setVal
86
    *
87
    * @param string $text テキスト
88
    * @param array $option オプション
89
    * @author hagiwara
90
    */
91
    public function setVal($text, $option)
92
    {
93
        $default_option = [
94
            'w' => 0,
95
            'h' => 0,
96
            'border' => 0,
97
            'align' => '',
98
            'fill' => false,
99
            'link' => '',
100
            'x' => 0,
101
            'y' => 0,
102
            'color' => '000000',
103
            'font' => '',
104
            'size' => 11,
105
            'stretch' => 0,
106
            'auto_size' => false,
107
        ];
108
        $option = array_merge($default_option ,$option);
109
        
110
        // 自動で枠に収めるかどうかのチェック
111
        if ($option['auto_size'] == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
112
            $fontDefaultWidth = $this->getStringWidth($text, $option['font'], '', $option['size']);
113
            if ($fontDefaultWidth > $option['w']) {
114
                $option['align'] ='J';
115
                $option['stretch'] =1;
116
            }
117
        }
118
        
119
        // 書き込む文字列のフォントを指定
120
        $this->__pdf->SetFont($this->getFont($option['font']), '', $option['size']);
121
        // 書き込む文字列の文字色を指定
122
        $concertColor = $this->colorCodeConvert($option['color']);
123
        $this->__pdf->SetTextColor($concertColor['r'], $concertColor['g'], $concertColor['b']);
124
125
        $this->__pdf->SetXY($option['x'], $option['y']);
126
        // 文字列を書き込む
127
        $this->__pdf->Cell($option['w'], $option['h'], $text, $option['border'], 0, $option['align'], $option['fill'], $option['link'], $option['stretch']);
128
    }
129
130
    /**
131
    * setHtml
132
    *
133
    * @param string $html HTML
134
    * @param array $option オプション
135
    * @author hagiwara
136
    */
137
    public function setHtml($html, $option)
138
    {
139
        $default_option = [
140
            'w' => 0,
141
            'h' => 0,
142
            'border' => 0,
143
            'align' => '',
144
            'fill' => false,
145
            'link' => '',
146
            'x' => 0,
147
            'y' => 0,
148
            'color' => '000000',
149
            'font' => '',
150
            'size' => '',
151
            'reseth' => true,
152
            'autopadding' => false,
153
        ];
154
        $option = array_merge($default_option ,$option);
155
        // 書き込む文字列の文字色を指定
156
        //$concertColor = $this->colorCodeConvert($option['color']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
157
        //var_dump($concertColor);
158
        //$this->__pdf->SetTextColor($concertColor['r'], $concertColor['g'], $concertColor['b']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
159
160
        // 書き込む文字列のフォントを指定
161
        $this->__pdf->SetFont($this->getFont($option['font']), '', $option['size']);
162
        
163
        $this->__pdf->writeHTMLCell( $option['w'], $option['h'], $option['x'], $option['y'], $html, $option['border'], 0, $option['fill'], $option['reseth'], $option['align'], $option['autopadding']);
164
    }
165
166
    /**
167
    * getFont
168
    *
169
    * @param string $font フォント名
170
    * @author hagiwara
171
    */
172
    private function getFont($font)
173
    {
174
        if (array_key_exists($font, $this->__fonts)) {
175
            return $this->__fonts[$font];
176
        } else {
177
            return $font;
178
        }
179
    }
180
181
    /**
182
    * setImage
183
    *
184
    * @param string $image 画像パス
185
    * @param array $option オプション
186
    * @author hagiwara
187
    */
188
    public function setImage($image, $option)
189
    {
190
        $default_option = [
191
            'x' => 0,
192
            'y' => 0,
193
            'w' => 0,
194
            'h' => 0,
195
            'link' => '',
196
            'resize' => true,
197
            'dpi' => '300',
198
        ];
199
        $option = array_merge($default_option ,$option);
200
        $this->__pdf->Image($image, $option['x'], $option['y'], $option['w'], $option['h'], '', $option['link'], '', $option['resize'], $option['dpi']);
201
    }
202
203
204
    /**
205
    * colorCodeConvert
206
    *
207
    * @param string $color カラーコード(16進数)
208
    * @author hagiwara
209
    */
210
    private function colorCodeConvert($color)
211
    {
212
        if (
213
            preg_match('/^([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/', $color, $colorCheck)
214
        ) {
215
            return [
216
                'r' => hexdec($colorCheck[1]),
217
                'g' => hexdec($colorCheck[2]),
218
                'b' => hexdec($colorCheck[3]),
219
            ];
220
        } else {
221
            return [
222
                'r' => 0,
223
                'g' => 0,
224
                'b' => 0,
225
            ];
226
        }
227
    }
228
229
    /**
230
     * setAutoPageBreak
231
     * page brackeを自動で行うかどうか。画像を下部に埋め込む際には切っておいたほうが良さげ
232
     * @param int $auto
233
     * @param int $margin
234
     */
235
    public function setAutoPageBreak($auto, $margin = 0)
236
    {
237
        $this->__pdf->SetAutoPageBreak($auto, $margin);
238
    }
239
    
240
   /**
241
    * getStringWidth
242
    *
243
    * @param string $text テキスト
244
    * @param string $font フォント名
245
    * @param string $fontstyle フォントスタイル
246
    * @param integer $fontsize サイズ
247
    * @param bool $getarray 結果を1文字ずつ配列で返すか
248
    * @author hagiwara
249
    */
250
    public function getStringWidth($text, $font, $fontstyle, $fontsize, $getarray = false) {
251
        return $this->__pdf->GetStringWidth( $text, $font, $fontstyle, $fontsize, $getarray);
252
    }
253
254
    /**
255
    * write
256
    *
257
    * @param string $file 出力ファイル
258
    * @author hagiwara
259
    */
260
    public function write($file)
261
    {
262
        $pdf_info = $this->__pdf->Output(null, 'S');
263
264
        $fp = fopen($file, 'w');
265
        fwrite($fp ,$pdf_info);
266
        fclose($fp);
267
    }
268
269
}
270