QRimage::jpg()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 5
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php
2
/*
3
* PHP QR Code encoder
4
*
5
* Image output of code using GD2
6
*
7
* PHP QR Code is distributed under LGPL 3
8
* Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
9
*
10
* This library is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Lesser General Public
12
* License as published by the Free Software Foundation; either
13
* version 3 of the License, or any later version.
14
*
15
* This library is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
* Lesser General Public License for more details.
19
*
20
* You should have received a copy of the GNU Lesser General Public
21
* License along with this library; if not, write to the Free Software
22
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
*/
24
namespace tinymeng\code\Gateways\qrcode;
25
26
define('QR_IMAGE', true);
27
28
class QRimage {
29
30
    //----------------------------------------------------------------------
31
    public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE)
32
    {
33
        $image = self::image($frame, $pixelPerPoint, $outerFrame);
34
35
        if ($filename === false) {
36
            Header("Content-type: image/png");
37
            ImagePng($image);
38
        } else {
39
            if($saveandprint===TRUE){
40
                ImagePng($image, $filename);
41
                header("Content-type: image/png");
42
                ImagePng($image);
43
            }else{
44
                ImagePng($image, $filename);
45
            }
46
        }
47
48
        ImageDestroy($image);
49
    }
50
51
    //----------------------------------------------------------------------
52
    public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outerFrame = 4, $q = 85)
53
    {
54
        $image = self::image($frame, $pixelPerPoint, $outerFrame);
55
56
        if ($filename === false) {
57
            Header("Content-type: image/jpeg");
58
            ImageJpeg($image, null, $q);
59
        } else {
60
            ImageJpeg($image, $filename, $q);
61
        }
62
63
        ImageDestroy($image);
64
    }
65
66
    //----------------------------------------------------------------------
67
    private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4)
68
    {
69
        $h = count($frame);
70
        $w = strlen($frame[0]);
71
72
        $imgW = $w + 2*$outerFrame;
73
        $imgH = $h + 2*$outerFrame;
74
75
        $base_image =ImageCreate($imgW, $imgH);
76
77
        $col[0] = ImageColorAllocate($base_image,255,255,255);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$col was never initialized. Although not strictly required by PHP, it is generally a good practice to add $col = array(); before regardless.
Loading history...
78
        $col[1] = ImageColorAllocate($base_image,0,0,0);
79
80
        imagefill($base_image, 0, 0, $col[0]);
81
82
        for($y=0; $y<$h; $y++) {
83
            for($x=0; $x<$w; $x++) {
84
                if ($frame[$y][$x] == '1') {
85
                    ImageSetPixel($base_image,$x+$outerFrame,$y+$outerFrame,$col[1]);
86
                }
87
            }
88
        }
89
90
        $target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
91
        ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
92
        ImageDestroy($base_image);
93
94
        return $target_image;
95
    }
96
}
97