Completed
Push — master ( 603564...3d1dbb )
by Michael
03:27
created

Image_Transform_Driver_IM::addBorders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 45 and the first side effect is on line 28.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/* vim: set expandtab tabstop=4 shiftwidth=4: */
4
5
/**
6
 * ImageMagick binaries implementation for Image_Transform package
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * LICENSE: This source file is subject to version 3.0 of the PHP license
11
 * that is available through the world-wide-web at the following URI:
12
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
13
 * the PHP License and are unable to obtain it through the web, please
14
 * send a note to [email protected] so we can mail you a copy immediately.
15
 *
16
 * @category   Image
17
 * @package    Image_Transform
18
 * @author     Peter Bowyer <[email protected]>
19
 * @author     Philippe Jausions <[email protected]>
20
 * @copyright  2002-2005 The PHP Group
21
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
22
 * @version    CVS: $Id: IM.php 266859 2008-09-30 22:28:47Z dufuz $
23
 * @link       http://pear.php.net/package/Image_Transform
24
 */
25
26
//require_once 'Image/Transform.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
27
//require_once 'System.php';
28
require_once XOOPS_ROOT_PATH . '/modules/extgallery/class/pear/Image/Transform.php';
29
require_once XOOPS_ROOT_PATH . '/modules/extgallery/class/pear/System.php';
30
31
/**
32
 * ImageMagick binaries implementation for Image_Transform package
33
 *
34
 * @category   Image
35
 * @package    Image_Transform
36
 * @subpackage Image_Transform_Driver_IM
37
 * @author     Peter Bowyer <[email protected]>
38
 * @author     Philippe Jausions <[email protected]>
39
 * @copyright  2002-2005 The PHP Group
40
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
41
 * @version    Release: @package_version@
42
 * @link       http://pear.php.net/package/Image_Transform
43
 * @link http://www.imagemagick.org/
44
 **/
45
class Image_Transform_Driver_IM extends Image_Transform
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...
46
{
47
    /**
48
     * associative array commands to be executed
49
     * @var array
50
     * @access private
51
     */
52
    var $command;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $command.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
53
54
    /**
55
     * Class constructor
56
     */
57
    function Image_Transform_Driver_IM()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
58
    {
59
        $this->__construct();
60
    } // End Image_IM
61
62
    /**
63
     * Class constructor
64
     */
65 View Code Duplication
    function __construct()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
66
    {
67
        $this->_init();
68
        if (!defined('IMAGE_TRANSFORM_IM_PATH')) {
69
            $path = dirname(System::which('convert'))
70
                    . DIRECTORY_SEPARATOR;
71
            define('IMAGE_TRANSFORM_IM_PATH', $path);
72
        }
73
        if (System::which(IMAGE_TRANSFORM_IM_PATH . 'convert' . ((OS_WINDOWS) ? '.exe' : ''))) {
74
            include 'Image/Transform/Driver/Imagick/ImageTypes.php';
75
        } else {
76
            $this->isError(PEAR::raiseError('Couldn\'t find "convert" binary',
0 ignored issues
show
Bug introduced by
The method raiseError() does not exist on PEAR. Did you maybe mean _raiseError()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
77
                IMAGE_TRANSFORM_ERROR_UNSUPPORTED));
78
        }
79
    } // End Image_IM
80
81
    /**
82
     * Initialize the state of the object
83
     **/
84
    function _init()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
85
    {
86
        $this->command = array();
87
    }
88
89
    /**
90
     * Load an image.
91
     *
92
     * This method doesn't support remote files.
93
     *
94
     * @param string filename
95
     *
96
     * @return mixed TRUE or a PEAR error object on error
97
     * @see PEAR::isError()
98
     */
99
    function load($image)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
100
    {
101
        $this->_init();
102
        if (!file_exists($image)) {
103
            return PEAR::raiseError('The image file ' . $image
0 ignored issues
show
Bug introduced by
The method raiseError() does not exist on PEAR. Did you maybe mean _raiseError()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
104
                . ' doesn\'t exist', IMAGE_TRANSFORM_ERROR_IO);
105
        }
106
        $this->image = $image;
107
        $result = $this->_get_image_details($image);
108
        if (PEAR::isError($result)) {
109
            return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (none) is incompatible with the return type of the parent method Image_Transform::load of type PEAR_Error.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
110
        }
111
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type of the parent method Image_Transform::load of type PEAR_Error.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
112
113
    } // End load
114
115
    /**
116
     * Image_Transform_Driver_IM::_get_image_details()
117
     *
118
     * @param string $image the path and name of the image file
119
     * @return none
120
     */
121
    function _get_image_details($image)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
122
    {
123
        $retval = Image_Transform::_get_image_details($image);
124
        if (PEAR::isError($retval)) {
125
            unset($retval);
126
127
            if (!System::which(IMAGE_TRANSFORM_IM_PATH . 'identify' . ((OS_WINDOWS) ? '.exe' : ''))) {
128
                $this->isError(PEAR::raiseError('Couldn\'t find "identify" binary', IMAGE_TRANSFORM_ERROR_UNSUPPORTED));
0 ignored issues
show
Bug introduced by
The method raiseError() does not exist on PEAR. Did you maybe mean _raiseError()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
129
            }
130
            $cmd = $this->_prepare_cmd(IMAGE_TRANSFORM_IM_PATH,
131
                'identify',
132
                '-format %w:%h:%m ' . escapeshellarg($image));
133
            exec($cmd, $res, $exit);
134
135
            if ($exit != 0) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $exit of type integer|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
136
                return PEAR::raiseError("Cannot fetch image or images details.", true);
0 ignored issues
show
Bug introduced by
The method raiseError() does not exist on PEAR. Did you maybe mean _raiseError()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
137
            }
138
139
            $data  = explode(':', $res[0]);
140
            $this->img_x = $data[0];
141
            $this->img_y = $data[1];
142
            $this->type  = strtolower($data[2]);
143
            $retval = true;
144
145
        }
146
147
        return $retval;
148
    }
149
150
    /**
151
     * Resize the image.
152
     *
153
     * @access private
154
     *
155
     * @param int   $new_x   New width
156
     * @param int   $new_y   New height
157
     * @param mixed $options Optional parameters
158
     *
159
     * @return true on success or PEAR Error object on error
160
     * @see PEAR::isError()
161
     */
162
    function _resize($new_x, $new_y, $options = null)
0 ignored issues
show
Unused Code introduced by
The parameter $options 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...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
163
    {
164
        if (isset($this->command['resize'])) {
165
            return PEAR::raiseError('You cannot scale or resize an image more than once without calling save() or display()', true);
0 ignored issues
show
Bug introduced by
The method raiseError() does not exist on PEAR. Did you maybe mean _raiseError()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
166
        }
167
        $this->command['resize'] = '-geometry '
168
            . ((int) $new_x) . 'x' . ((int) $new_y) . '!';
169
170
        $this->new_x = $new_x;
171
        $this->new_y = $new_y;
172
173
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type documented by Image_Transform_Driver_IM::_resize of type TRUE.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
174
    } // End resize
175
176
    /**
177
     * rotate
178
     *
179
     * @param   int     angle   rotation angle
180
     * @param   array   options no option allowed
181
     * @return mixed TRUE or a PEAR error object on error
182
     */
183
    function rotate($angle, $options = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
184
    {
185
        $angle = $this->_rotation_angle($angle);
186
        if ($angle % 360) {
187
            $this->command['rotate'] = '-rotate ' . (float) $angle;
188
        }
189
        return true;
190
191
    } // End rotate
192
193
    /**
194
     * Crop image
195
     *
196
     * @author Ian Eure <[email protected]>
197
     * @since 0.8
198
     *
199
     * @param int width Cropped image width
200
     * @param int height Cropped image height
201
     * @param int x X-coordinate to crop at
202
     * @param int y Y-coordinate to crop at
203
     *
204
     * @return mixed TRUE or a PEAR error object on error
205
     */
206
    function crop($width, $height, $x = 0, $y = 0) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
207
        // Do we want a safety check - i.e. if $width+$x > $this->img_x then we
208
        // raise a warning? [and obviously same for $height+$y]
209
        $this->command['crop'] = '-crop '
210
            . ((int) $width)  . 'x' . ((int) $height)
211
            . '+' . ((int) $x) . '+' . ((int) $y) . '!';
212
213
        // I think that setting img_x/y is wrong, but scaleByLength() & friends
214
        // mess up the aspect after a crop otherwise.
215
        $this->new_x = $this->img_x = $width - $x;
0 ignored issues
show
Documentation Bug introduced by
It seems like $width - $x can also be of type double. However, the property $img_x is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Documentation Bug introduced by
It seems like $this->img_x = $width - $x can also be of type double. However, the property $new_x is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
216
        $this->new_y = $this->img_y = $height - $y;
0 ignored issues
show
Documentation Bug introduced by
It seems like $height - $y can also be of type double. However, the property $img_y is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Documentation Bug introduced by
It seems like $this->img_y = $height - $y can also be of type double. However, the property $new_y is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
217
218
        return true;
219
    }
220
221
    /**
222
     * addText
223
     *
224
     * @param   array   options     Array contains options
225
     *                              array(
226
     *                                  'text'  The string to draw
227
     *                                  'x'     Horizontal position
228
     *                                  'y'     Vertical Position
229
     *                                  'Color' Font color
230
     *                                  'font'  Font to be used
231
     *                                  'size'  Size of the fonts in pixel
232
     *                                  'resize_first'  Tell if the image has to be resized
233
     *                                                  before drawing the text
234
     *                              )
235
     *
236
     * @return mixed TRUE or a PEAR error object on error
237
     * @see PEAR::isError()
238
     */
239
    function addText($params)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
240
    {
241
        $this->old_image = $this->imageHandle;
0 ignored issues
show
Bug introduced by
The property old_image 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...
Bug introduced by
The property imageHandle 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...
242
         $params = array_merge($this->_get_default_text_params(), $params);
243
         extract($params);
244
245
         if (true === $resize_first) {
246
             // Set the key so that this will be the last item in the array
247
            $key = 'ztext';
248
         } else {
249
            $key = 'text';
250
         }
251
         $this->command[$key] = '-font ' . escapeshellarg($font)
252
            . ' -fill ' . escapeshellarg($color)
253
            . ' -draw \'text ' . escapeshellarg($x . ',' . $y)
254
            . ' "' . escapeshellarg($text) . '"\'';
255
         // Producing error: gs: not found gs: not found convert: Postscript delegate failed [No such file or directory].
256
        return true;
257
258
    } // End addText
259
260
    /**
261
     * Adjust the image gamma
262
     *
263
     * @access public
264
     * @param float $outputgamma
265
     * @return mixed TRUE or a PEAR error object on error
266
     */
267
    function gamma($outputgamma = 1.0) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
268
        if ($outputgamme != 1.0) {
0 ignored issues
show
Bug introduced by
The variable $outputgamme does not exist. Did you mean $outputgamma?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
269
            $this->command['gamma'] = '-gamma ' . (float) $outputgamma;
270
        }
271
        return true;
272
    }
273
274
    /**
275
     * Convert the image to greyscale
276
     *
277
     * @access public
278
     * @return mixed TRUE or a PEAR error object on error
279
     */
280
    function greyscale() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
281
        $this->command['type'] = '-type Grayscale';
282
        return true;
283
    }
284
285
    /**
286
     * Horizontal mirroring
287
     *
288
     * @access public
289
     * @return TRUE or PEAR Error object on error
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

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...
290
     */
291 View Code Duplication
    function mirror() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
292
        // We can only apply "flop" once
293
        if (isset($this->command['flop'])) {
294
            unset($this->command['flop']);
295
        } else {
296
            $this->command['flop'] = '-flop';
297
        }
298
        return true;
299
    }
300
301
    /**
302
     * Vertical mirroring
303
     *
304
     * @access public
305
     * @return TRUE or PEAR Error object on error
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

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...
306
     */
307 View Code Duplication
    function flip() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
308
        // We can only apply "flip" once
309
        if (isset($this->command['flip'])) {
310
            unset($this->command['flip']);
311
        } else {
312
            $this->command['flip'] = '-flip';
313
        }
314
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type of the parent method Image_Transform::flip of type TRUE.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
315
    }
316
317
    /**
318
     * Save the image file
319
     *
320
     * @access public
321
     *
322
     * @param $filename string  the name of the file to write to
323
     * @param $quality  quality image dpi, default=75
324
     * @param $type     string  (JPEG, PNG...)
325
     *
326
     * @return mixed TRUE or a PEAR error object on error
327
     */
328
    function save($filename, $type = '', $quality = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
329
    {
330
        $type = strtoupper(($type == '') ? $this->type : $type);
331
        switch ($type) {
332
            case 'JPEG':
333
                $type = 'JPG';
334
                break;
335
        }
336
        $options = array();
337
        if (!is_null($quality)) {
338
            $options['quality'] = $quality;
339
        }
340
        $quality = $this->_getOption('quality', $options, 75);
341
342
        $cmd = $this->_prepare_cmd(
343
            IMAGE_TRANSFORM_IM_PATH,
344
            'convert',
345
            implode(' ', $this->command)
346
               . ' -quality ' . ((int) $quality) . ' '
347
               . escapeshellarg($this->image) . ' ' . $type . ':'
348
               . escapeshellarg($filename) . ' 2>&1');
349
        exec($cmd, $res, $exit);
350
351
        if (!$this->keep_settings_on_save) {
352
            $this->free();
353
        }
354
355
        return ($exit == 0) ? true : PEAR::raiseError(implode('. ', $res),
0 ignored issues
show
Bug introduced by
The method raiseError() does not exist on PEAR. Did you maybe mean _raiseError()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Bug introduced by
It seems like you are loosely comparing $exit of type integer|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
356
            IMAGE_TRANSFORM_ERROR_IO);
357
    } // End save
358
359
    /**
360
     * Display image without saving and lose changes
361
     *
362
     * This method adds the Content-type HTTP header
363
     *
364
     * @access public
365
     *
366
     * @param string type (JPEG,PNG...);
367
     * @param int quality 75
368
     *
369
     * @return mixed TRUE or a PEAR error object on error
370
     */
371
    function display($type = '', $quality = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
372
    {
373
        $type    = strtoupper(($type == '') ? $this->type : $type);
374
        switch ($type) {
375
            case 'JPEG':
376
                $type = 'JPG';
377
                break;
378
        }
379
        $options = array();
380
        if (!is_null($quality)) {
381
            $options['quality'] = $quality;
382
        }
383
        $quality = $this->_getOption('quality', $options, 75);
384
385
        $this->_send_display_headers($type);
386
387
        $cmd = $this->_prepare_cmd(
388
            IMAGE_TRANSFORM_IM_PATH,
389
            'convert',
390
            implode(' ', $this->command) . " -quality $quality "  .
391
                   $this->image . ' ' . $type . ":-");
392
        passthru($cmd);
393
394
        if (!$this->keep_settings_on_save) {
395
            $this->free();
396
        }
397
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type of the parent method Image_Transform::display of type PEAR_Error.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
398
    }
399
400
    /**
401
     * Destroy image handle
402
     *
403
     * @return void
404
     */
405
    function free()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
406
    {
407
        $this->command = array();
408
        $this->image = '';
409
        $this->type = '';
410
    }
411
412
} // End class ImageIM
413