Completed
Branch master (fd4537)
by Roberto
06:49 queued 03:43
created

Basic::getDimImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.6666
cc 2
eloc 6
nc 2
nop 0
crap 2.0185
1
<?php
2
3
namespace Posprint\Graphics;
4
5
/**
6
 * Basic method for graphics classes
7
 *
8
 * @category   NFePHP
9
 * @package    Posprint
10
 * @copyright  Copyright (c) 2016
11
 * @license    http://www.gnu.org/licenses/lesser.html LGPL v3
12
 * @author     Roberto L. Machado <linux dot rlm at gmail dot com>
13
 * @link       http://github.com/nfephp-org/posprint for the canonical source repository
14
 */
15
16
abstract class Basic
17
{
18
    /**
19
     * Image GD
20
     * @var resource
21
     */
22
    protected $img;
23
    /**
24
     * Image Height
25
     * @var int
26
     */
27
    protected $imgHeight = 0;
28
    /**
29
     * Image Width
30
     * @var int
31
     */
32
    protected $imgWidth = 0;
33
    
34
    /**
35
     * @return int height of the image in pixels
36
     */
37 1
    public function getHeight()
38
    {
39 1
        return $this->imgHeight;
40
    }
41
    
42
    /**
43
     * @return int Number of bytes to represent a row of this image
44
     */
45
    public function getHeightBytes()
46
    {
47
        return (int)(($this->imgHeight + 7) / 8);
48
    }
49
    
50
    /**
51
     * @return int Width of the image
52
     */
53 1
    public function getWidth()
54
    {
55 1
        return $this->imgWidth;
56
    }
57
    
58
    /**
59
     * @return int Number of bytes to represent a row of this image
60
     */
61 1
    public function getWidthBytes()
62
    {
63 1
        return (int)(($this->imgWidth + 7) / 8);
64
    }
65
    
66
    /**
67
     * zGetDimImage
68
     * Get width and height of resource image
69
     * and save in properties
70
     * @return array with dimentions of image
71
     */
72 7
    protected function getDimImage()
73
    {
74 7
        if (!is_resource($this->img)) {
75
            return;
76
        }
77 7
        $this->imgHeight = imagesy($this->img);
78 7
        $this->imgWidth = imagesx($this->img);
79 7
        return ['height'  => $this->imgHeight, 'width' => $this->imgWidth];
80
    }
81
    
82
    /**
83
     * @return boolean True if GD is supported, false otherwise (a wrapper for the version, for mocking in tests)
84
     */
85 10
    protected function isGdSupported()
86
    {
87 10
        return $this->isGdLoaded();
88
    }
89
90
    /**
91
     * @return boolean True if GD is loaded, false otherwise
92
     */
93 10
    protected function isGdLoaded()
94
    {
95 10
        $this->gdImgProcessing = extension_loaded('gd');
0 ignored issues
show
Bug introduced by
The property gdImgProcessing 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...
96 10
        return $this->gdImgProcessing;
97
    }
98
    
99
    /**
100
     * identifyImg
101
     * Identifies image file type
102
     * @param string $filename
103
     * @return string
104
     */
105 7
    protected function identifyImg($filename)
106
    {
107 7
        $imgtype = exif_imagetype($filename);
108
        $aImgTypes = [
109 7
            0 => 'NULL',
110 7
            1 => 'GIF',
111 7
            2 => 'JPEG',
112 7
            3 => 'PNG',
113 7
            4 => 'SWF',
114 7
            5 => 'PSD',
115 7
            6 => 'BMP',
116 7
            7 => 'TIFF_II',
117 7
            8 => 'TIFF_MM',
118 7
            9 => 'JPC',
119 7
            10 => 'JP2',
120 7
            11 => 'JPX',
121 7
            12 => 'JB2',
122 7
            13 => 'SWC',
123 7
            14 => 'IFF',
124 7
            15 => 'WBMP',
125 7
            16 => 'XBM',
126 7
            17 => 'ICO'];
127 7
        return $aImgTypes[$imgtype];
128
    }
129
}
130