Image_Transform_Driver_Imagick::_resize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* vim: set expandtab tabstop=4 shiftwidth=4: */
4
5
/**
6
 * Image Transformation interface using old ImageMagick extension
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
 * @copyright  2002-2005 The PHP Group
20
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
21
 * @deprecated
22
 * @link       http://pear.php.net/package/Image_Transform
23
 */
24
25
/**
26
 * Include of base class
27
 */
28
require_once XOOPS_ROOT_PATH . '/modules/extgallery/class/pear/Image/Image/Transform.php';
29
30
/**
31
 * Image Transformation interface using old ImageMagick extension
32
 *
33
 * DEPRECATED: current CVS/release imagick extension should use
34
 * the Imagick2 driver
35
 *
36
 * @deprecated
37
 */
38
class Image_Transform_Driver_Imagick extends Image_Transform
39
{
40
    /**
41
     * Handler of the imagick image ressource
42
     * @var array
43
     */
44
    public $imageHandle;
45
    /**
46
     * Handler of the image ressource before
47
     * the last transformation
48
     * @var array
49
     */
50
    public $oldImage;
51
52
    public function __construct()
53
    {
54
        if (!PEAR::loadExtension('imagick')) {
55
            return PEAR::raiseError('The imagick extension can not be found.', true);
0 ignored issues
show
Bug introduced by
The method raiseError() does not exist on PEAR. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

55
            return PEAR::/** @scrutinizer ignore-call */ raiseError('The imagick extension can not be found.', true);
Loading history...
56
        }
57
        require_once __DIR__ . '/Image/Transform/Driver/Imagick/ImageTypes.php';
58
        //return true;
59
    }
60
61
    // End Image_IM
62
63
    /**
64
     * Load image
65
     *
66
     * @param mixed $image
67
     *
68
     * @return mixed none or a PEAR error object on error
69
     * @see PEAR::isError()
70
     */
71
    public function load($image)
72
    {
73
        $this->imageHandle = imagick_create();
0 ignored issues
show
Bug introduced by
The function imagick_create was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

73
        $this->imageHandle = /** @scrutinizer ignore-call */ imagick_create();
Loading history...
74
        if (!is_resource($this->imageHandle)) {
75
            return PEAR::raiseError('Cannot initialize imagick image.', true);
76
        }
77
78
        if (!imagick_read($this->imageHandle, $image)) {
0 ignored issues
show
Bug introduced by
The function imagick_read was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

78
        if (!/** @scrutinizer ignore-call */ imagick_read($this->imageHandle, $image)) {
Loading history...
79
            return PEAR::raiseError('The image file ' . $image . ' does\'t exist', true);
80
        }
81
        $this->image = $image;
82
        $result      = $this->_get_image_details($image);
83
        if (PEAR::isError($result)) {
84
            return $result;
85
        }
86
    }
87
88
    // End load
89
90
    /**
91
     * Resize Action
92
     *
93
     * @param mixed $new_x
94
     * @param mixed $new_y
95
     *
96
     * @return none
0 ignored issues
show
Bug introduced by
The type none was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
97
     * @see PEAR::isError()
98
     */
99
    public function _resize($new_x, $new_y)
100
    {
101
        $img2 = imagick_copy_resize($this->imageHandle, $new_x, $new_y, IMAGICK_FILTER_CUBIC, 1);
0 ignored issues
show
Bug introduced by
The constant IMAGICK_FILTER_CUBIC was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The function imagick_copy_resize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

101
        $img2 = /** @scrutinizer ignore-call */ imagick_copy_resize($this->imageHandle, $new_x, $new_y, IMAGICK_FILTER_CUBIC, 1);
Loading history...
102
        if ($img2) {
103
            $this->oldImage    = $this->imageHandle;
104
            $this->imageHandle = $img2;
105
            $this->new_x       = $new_x;
106
            $this->new_y       = $new_y;
107
        } else {
108
            return PEAR::raiseError('Cannot create a new imagick imagick image for the resize.', true);
109
        }
110
    }
111
112
    // End resize
113
114
    /**
115
     * rotate
116
     * Note: color mask are currently not supported
117
     *
118
     * @param float      $angle
119
     * @param null|mixed $options
120
     *
121
     * @return none
122
     * @see PEAR::isError()
123
     */
124
    public function rotate($angle, $options = null)
125
    {
126
        $img2 = imagick_copy_rotate($this->imageHandle, $angle);
0 ignored issues
show
Bug introduced by
The function imagick_copy_rotate was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

126
        $img2 = /** @scrutinizer ignore-call */ imagick_copy_rotate($this->imageHandle, $angle);
Loading history...
127
        if ($img2) {
128
            $this->oldImage    = $this->imageHandle;
129
            $this->imageHandle = $img2;
130
            $this->new_x       = imagick_get_attribute($img2, 'width');
0 ignored issues
show
Bug introduced by
The function imagick_get_attribute was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

130
            $this->new_x       = /** @scrutinizer ignore-call */ imagick_get_attribute($img2, 'width');
Loading history...
131
            $this->new_y       = imagick_get_attribute($img2, 'height');
132
        } else {
133
            return PEAR::raiseError('Cannot create a new imagick imagick image for the resize.', true);
134
        }
135
    }
136
137
    // End rotate
138
139
    /**
140
     * addText
141
     *
142
     * @param mixed $params
143
     *
144
     * @see PEAR::isError()
145
     */
146
    public function addText($params)
147
    {
148
        $default_params = [
149
            'text'         => 'This is a Text',
150
            'x'            => 10,
151
            'y'            => 20,
152
            'size'         => 12,
153
            'color'        => 'red',
154
            'font'         => 'Arial.ttf',
155
            'resize_first' => false, // Carry out the scaling of the image before annotation?
156
        ];
157
        $params         = array_merge($default_params, $params);
158
        extract($params);
159
160
        $color = is_array($color) ? $this->colorarray2colorhex($color) : mb_strtolower($color);
161
162
        imagick_annotate(
0 ignored issues
show
Bug introduced by
The function imagick_annotate was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

162
        /** @scrutinizer ignore-call */ 
163
        imagick_annotate(
Loading history...
163
            $this->imageHandle,
164
            [
165
                'primitive' => "text $x,$y " . $text,
166
                'pointsize' => $size,
167
                'antialias' => 0,
168
                'fill'      => $color,
169
                'font'      => $font,
170
            ]
171
        );
172
    }
173
174
    // End addText
175
176
    /**
177
     * Save the image file
178
     *
179
     * @param string $filename the name of the file to write to
180
     *
181
     * @param string $type
182
     * @param int    $quality
183
     */
184
    public function save($filename, $type = '', $quality = 75)
185
    {
186
        if ('' == $type) {
187
            $type = mb_strtoupper($type);
188
            imagick_write($this->imageHandle, $filename, $type);
0 ignored issues
show
Bug introduced by
The function imagick_write was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

188
            /** @scrutinizer ignore-call */ 
189
            imagick_write($this->imageHandle, $filename, $type);
Loading history...
189
        } else {
190
            imagick_write($this->imageHandle, $filename);
191
        }
192
        imagick_free($handle);
0 ignored issues
show
Bug introduced by
The function imagick_free was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

192
        /** @scrutinizer ignore-call */ 
193
        imagick_free($handle);
Loading history...
Comprehensibility Best Practice introduced by
The variable $handle seems to be never defined.
Loading history...
193
    }
194
195
    // End save
196
197
    /**
198
     * Display image without saving and lose changes
199
     *
200
     * @param mixed $type
201
     * @param mixed $quality
202
     */
203
    public function display($type = '', $quality = 75)
204
    {
205
        if ('' == $type) {
206
            header('Content-type: image/' . $this->type);
207
            if (!imagick_dump($this->imageHandle)) {
0 ignored issues
show
Bug introduced by
The function imagick_dump was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

207
            if (!/** @scrutinizer ignore-call */ imagick_dump($this->imageHandle)) {
Loading history...
208
            }
209
        } else {
210
            header('Content-type: image/' . $type);
211
            if (!imagick_dump($this->imageHandle, $this->type)) {
212
            }
213
        }
214
        $this->free();
215
    }
216
217
    /**
218
     * Destroy image handle
219
     *
220
     * @return bool
221
     */
222
    public function free()
223
    {
224
        if (is_resource($this->imageHandle)) {
0 ignored issues
show
introduced by
The condition is_resource($this->imageHandle) is always false.
Loading history...
225
            imagick_free($this->imageHandle);
0 ignored issues
show
Bug introduced by
The function imagick_free was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

225
            /** @scrutinizer ignore-call */ 
226
            imagick_free($this->imageHandle);
Loading history...
226
        }
227
        if (is_resource($this->oldImage)) {
0 ignored issues
show
introduced by
The condition is_resource($this->oldImage) is always false.
Loading history...
228
            imagick_free($this->oldImage);
229
        }
230
231
        return true;
232
    }
233
} // End class ImageIM
234