Passed
Push — master ( df5caf...cd5737 )
by Michael
02:02
created

GD   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 170
rs 10
c 0
b 0
f 0
wmc 24
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 22 5
A save() 0 4 1
A add_ttf_text() 0 9 2
A add_text() 0 10 3
A add_border() 0 19 2
A resize() 0 19 2
D make_color() 0 29 9
1
<?php
2
/*
3
-------------------------------------------------------------------------
4
                     ADSLIGHT 2 : Module for Xoops
5
6
        Redesigned and ameliorate By Luc Bizet user at www.frxoops.org
7
        Started with the Classifieds module and made MANY changes
8
        Website : http://www.luc-bizet.fr
9
        Contact : [email protected]
10
-------------------------------------------------------------------------
11
             Original credits below Version History
12
##########################################################################
13
#                    Classified Module for Xoops                         #
14
#  By John Mordo user jlm69 at www.xoops.org and www.jlmzone.com         #
15
#      Started with the MyAds module and made MANY changes               #
16
##########################################################################
17
 Original Author: Pascal Le Boustouller
18
 Author Website : [email protected]
19
 Licence Type   : GPL
20
-------------------------------------------------------------------------
21
*/
22
23
/**
24
 * Class GD
25
 */
26
class GD
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
27
{
28
    public $image;
29
    public $width;
30
    public $height;
31
32
    /**
33
     * @param $location
34
     */
35
    public function __construct($location)
36
    {
37
        $imageinfo = @getimagesize($location) || exit('Unknown picture');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
38
39
        $this->width  = $imageinfo['0'];
40
        $this->height = $imageinfo['1'];
41
42
        switch ($imageinfo['2']) {
43
            case '1':
44
                $this->image = imagecreatefromgif($location);
45
                break;
46
47
            case '2':
48
                $this->image = imagecreatefromjpeg($location);
49
                break;
50
51
            case '3':
52
                $this->image = imagecreatefrompng($location);
53
                break;
54
55
            default:
56
                exit('Unknown file format');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
57
        }
58
    }
59
60
    /**
61
     * @param $sizex
62
     * @param $sizey
63
     */
64
    public function resize($sizex, $sizey)
65
    {
66
        $org = round($this->width / $this->height, 2);
67
        $new = round($sizex / $sizey, 2);
68
69
        if ($new > $org) {
70
            $sizex = round($this->width / ($this->height / $sizey), 0);
71
            //            $sizey = $sizey;
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
72
        } else {
73
            //            $sizex = $sizex;
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
74
            $sizey = round($this->height / ($this->width / $sizex), 0);
75
        }
76
77
        $resized = imagecreatetruecolor($sizex, $sizey);
78
        imagecopyresampled($resized, $this->image, 0, 0, 0, 0, $sizex, $sizey, $this->width, $this->height);
79
80
        $this->image  = $resized;
81
        $this->width  = $sizex;
82
        $this->height = $sizey;
83
    }
84
85
    /**
86
     * @param $color
87
     *
88
     * @return array
89
     */
90
    public function make_color($color)
91
    {
92
        $rgb = [];
93
94
        if (is_array($color) && '3' == count($color)) {
95
            $rgb['r'] = $color['0'];
96
            $rgb['g'] = $color['1'];
97
            $rgb['b'] = $color['2'];
98
        } elseif (preg_match('/^#?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i', $color, $results)) {
99
            $rgb['r'] = hexdec($results['1']);
100
            $rgb['g'] = hexdec($results['2']);
101
            $rgb['b'] = hexdec($results['3']);
102
        } else {
103
            exit('Unknown color');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
104
        }
105
106
        foreach ([
107
                     'r',
108
                     'g',
109
                     'b'
110
                 ] as $value) {
111
            if (!array_key_exists($value, $rgb) || $rgb[$value] < 0
112
                || $rgb[$value] > 255
113
                || !is_numeric($rgb[$value])) {
114
                exit('Wrong color');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
115
            }
116
        }
117
118
        return $rgb;
119
    }
120
121
    /**
122
     * @param $width
123
     * @param $color
124
     */
125
    public function add_border($width, $color)
126
    {
127
        $rgb      = $this->make_color($color);
128
        $allocate = imagecolorallocate($this->image, $rgb['r'], $rgb['g'], $rgb['b']);
129
130
        if ($width < 1) {
131
            exit('Wrong frame width');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
132
        }
133
134
        $sizex     = $this->width + (2 * $width);
135
        $sizey     = $this->height + (2 * $width);
136
        $new_image = imagecreatetruecolor($sizex, $sizey);
137
138
        imagefill($new_image, 0, 0, $allocate);
139
        imagecopyresampled($new_image, $this->image, $width, $width, 0, 0, $this->width, $this->height, $this->width, $this->height);
140
141
        $this->image  = $new_image;
142
        $this->width  = $sizex;
143
        $this->height = $sizey;
144
    }
145
146
    /**
147
     * @param $text
148
     * @param $font
149
     * @param $color
150
     * @param $x
151
     * @param $y
152
     */
153
    public function add_text($text, $font, $color, $x, $y)
0 ignored issues
show
Unused Code introduced by
The parameter $y is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

153
    public function add_text($text, $font, $color, $x, /** @scrutinizer ignore-unused */ $y)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $x is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

153
    public function add_text($text, $font, $color, /** @scrutinizer ignore-unused */ $x, $y)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
154
    {
155
        if ($font < 1 || $font > 5) {
156
            exit('Wrong font');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
157
        }
158
159
        $rgb         = $this->make_color($color);
160
        $allocate    = imagecolorallocate($this->image, $rgb['r'], $rgb['g'], $rgb['b']);
0 ignored issues
show
Unused Code introduced by
The assignment to $allocate is dead and can be removed.
Loading history...
161
        $text_width  = imagefontwidth($font) * strlen($text);
0 ignored issues
show
Unused Code introduced by
The assignment to $text_width is dead and can be removed.
Loading history...
162
        $text_height = imagefontheight($font);
0 ignored issues
show
Unused Code introduced by
The assignment to $text_height is dead and can be removed.
Loading history...
163
164
        //Dokoncaj
165
    }
166
167
    /**
168
     * @param        $text
169
     * @param        $size
170
     * @param        $color
171
     * @param        $x
172
     * @param        $y
173
     * @param string $font
174
     */
175
    public function add_ttf_text($text, $size, $color, $x, $y, $font = './tahoma.ttf')
176
    {
177
        if (!is_file($font)) {
178
            exit('Unknown font');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
179
        }
180
181
        $rgb      = $this->make_color($color);
182
        $allocate = imagecolorallocate($this->image, $rgb['r'], $rgb['g'], $rgb['b']);
183
        imagettftext($this->image, $size, 0, $x, $y, $allocate, $font, $text);
184
    }
185
186
    /**
187
     * @param        $location
188
     * @param string $quality
189
     */
190
    public function save($location, $quality = '80')
191
    {
192
        imagejpeg($this->image, $location, $quality);
0 ignored issues
show
Bug introduced by
$quality of type string is incompatible with the type integer expected by parameter $quality of imagejpeg(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

192
        imagejpeg($this->image, $location, /** @scrutinizer ignore-type */ $quality);
Loading history...
193
        imagedestroy($this->image);
194
    }
195
}
196