Completed
Push — master ( 67bb37...e20777 )
by Michael
02:35
created

GD::add_border()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 2
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
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
Coding Style Compatibility introduced by
The method __construct() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

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
Coding Style Compatibility introduced by
The method __construct() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

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
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
89
     */
90
    public function make_color($color)
91
    {
92
        $rgb = array();
93
94
        if (is_array($color) && count($color) == '3') {
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
Coding Style Compatibility introduced by
The method make_color() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
104
        }
105
106
        foreach (array('r', 'g', 'b') as $value) {
107
            if (!array_key_exists($value, $rgb) || $rgb[$value] < 0 || $rgb[$value] > 255
108
                || !is_numeric($rgb[$value])
109
            ) {
110
                exit('Wrong color');
0 ignored issues
show
Coding Style Compatibility introduced by
The method make_color() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
111
            }
112
        }
113
114
        return $rgb;
115
    }
116
117
    /**
118
     * @param $width
119
     * @param $color
120
     */
121
    public function add_border($width, $color)
122
    {
123
        $rgb      = $this->make_color($color);
124
        $allocate = imagecolorallocate($this->image, $rgb['r'], $rgb['g'], $rgb['b']);
125
126
        if ($width < 1) {
127
            exit('Wrong frame width');
0 ignored issues
show
Coding Style Compatibility introduced by
The method add_border() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

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

This check looks from 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 $y is not used and could be removed.

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

Loading history...
150
    {
151
        if ($font < 1 || $font > 5) {
152
            exit('Wrong font');
0 ignored issues
show
Coding Style Compatibility introduced by
The method add_text() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
153
        }
154
155
        $rgb         = $this->make_color($color);
156
        $allocate    = imagecolorallocate($this->image, $rgb['r'], $rgb['g'], $rgb['b']);
0 ignored issues
show
Unused Code introduced by
$allocate is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
157
        $text_width  = imagefontwidth($font) * strlen($text);
0 ignored issues
show
Unused Code introduced by
$text_width is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
158
        $text_height = imagefontheight($font);
0 ignored issues
show
Unused Code introduced by
$text_height is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
159
160
        //Dokoncaj
161
    }
162
163
    /**
164
     * @param        $text
165
     * @param        $size
166
     * @param        $color
167
     * @param        $x
168
     * @param        $y
169
     * @param string $font
170
     */
171
    public function add_ttf_text($text, $size, $color, $x, $y, $font = './tahoma.ttf')
172
    {
173
        if (!is_file($font)) {
174
            exit('Unknown font');
0 ignored issues
show
Coding Style Compatibility introduced by
The method add_ttf_text() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
175
        }
176
177
        $rgb      = $this->make_color($color);
178
        $allocate = imagecolorallocate($this->image, $rgb['r'], $rgb['g'], $rgb['b']);
179
        imagettftext($this->image, $size, 0, $x, $y, $allocate, $font, $text);
180
    }
181
182
    /**
183
     * @param        $location
184
     * @param string $quality
185
     */
186
    public function save($location, $quality = '80')
187
    {
188
        imagejpeg($this->image, $location, $quality);
189
        imagedestroy($this->image);
190
    }
191
}
192