Completed
Branch master (923121)
by Michael
05:29 queued 02:40
created

GD::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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
Bug introduced by
Why assign $sizey to itself?

This checks looks for cases where a variable has been assigned to itself.

This assignement can be removed without consequences.

Loading history...
72
        } else {
73
            $sizex = $sizex;
0 ignored issues
show
Bug introduced by
Why assign $sizex to itself?

This checks looks for cases where a variable has been assigned to itself.

This assignement can be removed without consequences.

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 || !is_numeric($rgb[$value])) {
108
                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...
109
            }
110
        }
111
112
        return $rgb;
113
    }
114
115
    /**
116
     * @param $width
117
     * @param $color
118
     */
119
    public function add_border($width, $color)
120
    {
121
        $rgb      = $this->make_color($color);
122
        $allocate = imagecolorallocate($this->image, $rgb['r'], $rgb['g'], $rgb['b']);
123
124
        if ($width < 1) {
125
            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...
126
        }
127
128
        $sizex     = $this->width + (2 * $width);
129
        $sizey     = $this->height + (2 * $width);
130
        $new_image = imagecreatetruecolor($sizex, $sizey);
131
132
        imagefill($new_image, 0, 0, $allocate);
133
        imagecopyresampled($new_image, $this->image, $width, $width, 0, 0, $this->width, $this->height, $this->width, $this->height);
134
135
        $this->image  = $new_image;
136
        $this->width  = $sizex;
137
        $this->height = $sizey;
138
    }
139
140
    /**
141
     * @param $text
142
     * @param $font
143
     * @param $color
144
     * @param $x
145
     * @param $y
146
     */
147
    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...
148
    {
149
        if ($font < 1 || $font > 5) {
150
            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...
151
        }
152
153
        $rgb         = $this->make_color($color);
154
        $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...
155
        $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...
156
        $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...
157
158
        //Dokoncaj
159
    }
160
161
    /**
162
     * @param        $text
163
     * @param        $size
164
     * @param        $color
165
     * @param        $x
166
     * @param        $y
167
     * @param string $font
168
     */
169
    public function add_ttf_text($text, $size, $color, $x, $y, $font = './tahoma.ttf')
170
    {
171
        if (!is_file($font)) {
172
            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...
173
        }
174
175
        $rgb      = $this->make_color($color);
176
        $allocate = imagecolorallocate($this->image, $rgb['r'], $rgb['g'], $rgb['b']);
177
        imagettftext($this->image, $size, 0, $x, $y, $allocate, $font, $text);
178
    }
179
180
    /**
181
     * @param        $location
182
     * @param string $quality
183
     */
184
    public function save($location, $quality = '80')
185
    {
186
        imagejpeg($this->image, $location, $quality);
187
        imagedestroy($this->image);
188
    }
189
}
190