BCGDrawJPG::setInternalProperties()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace tinymeng\code\Gateways\barcode\drawer;
3
4
/**
5
 *--------------------------------------------------------------------
6
 *
7
 * Image Class to draw JPG images with possibility to set DPI
8
 *
9
 *--------------------------------------------------------------------
10
 * Copyright (C) Jean-Sebastien Goupil
11
 * http://www.barcodephp.com
12
 */
13
if (!function_exists('file_put_contents')) {
14
    function file_put_contents($filename, $data) {
15
        $f = @fopen($filename, 'w');
16
        if (!$f) {
0 ignored issues
show
introduced by
$f is of type false|resource, thus it always evaluated to false.
Loading history...
17
            return false;
18
        } else {
19
            $bytes = fwrite($f, $data);
20
            fclose($f);
21
            return $bytes;
22
        }
23
    }
24
}
25
26
class BCGDrawJPG extends BCGDraw {
27
    private $dpi;
28
    private $quality;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param resource $im
34
     */
35
    public function __construct($im) {
36
        parent::__construct($im);
37
    }
38
39
    /**
40
     * Sets the DPI.
41
     *
42
     * @param int $dpi
43
     */
44
    public function setDPI($dpi) {
45
        if(is_int($dpi)) {
0 ignored issues
show
introduced by
The condition is_int($dpi) is always true.
Loading history...
46
            $this->dpi = max(1, $dpi);
47
        } else {
48
            $this->dpi = null;
49
        }
50
    }
51
52
    /**
53
     * Sets the quality of the JPG.
54
     *
55
     * @param int $quality
56
     */
57
    public function setQuality($quality) {
58
        $this->quality = $quality;
59
    }
60
61
    /**
62
     * Draws the JPG on the screen or in a file.
63
     */
64
    public function draw() {
65
        ob_start();
66
        imagejpeg($this->im, null, $this->quality);
67
        $bin = ob_get_contents();
68
        ob_end_clean();
69
70
        $this->setInternalProperties($bin);
71
72
        if (empty($this->filename)) {
73
            echo $bin;
74
        } else {
75
            file_put_contents($this->filename, $bin);
76
        }
77
    }
78
79
    private function setInternalProperties(&$bin) {
80
        $this->internalSetDPI($bin);
81
        $this->internalSetC($bin);
82
    }
83
84
    private function internalSetDPI(&$bin) {
85
        if ($this->dpi !== null) {
86
            $bin = substr_replace($bin, pack("Cnn", 0x01, $this->dpi, $this->dpi), 13, 5);
87
        }
88
    }
89
90
    private function internalSetC(&$bin) {
91
        if(strcmp(substr($bin, 0, 4), pack('H*', 'FFD8FFE0')) === 0) {
92
            $offset = 4 + (ord($bin[4]) << 8 | ord($bin[5]));
93
            $firstPart = substr($bin, 0, $offset);
94
            $secondPart = substr($bin, $offset);
95
            $cr = pack('H*', 'FFFE004447656E657261746564207769746820426172636F64652047656E657261746F7220666F722050485020687474703A2F2F7777772E626172636F64657068702E636F6D');
96
            $bin = $firstPart;
97
            $bin .= $cr;
98
            $bin .= $secondPart;
99
        }
100
    }
101
}
102
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...