Completed
Push — master ( e5db64...945d9a )
by Schlaefer
05:09 queued 28s
created

FileUploadHelper   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 283
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 283
rs 9.28
c 0
b 0
f 0
wmc 39
lcom 1
cbo 4

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A reset() 0 14 1
A image() 0 26 5
A input() 0 15 2
A _getImageById() 0 17 2
A _getFullPath() 0 8 2
A _getImagePath() 0 8 2
A _getUploadPath() 0 3 1
A _getExt() 0 3 1
A _getImageByName() 0 10 6
A _getResizeNameOrPath() 0 7 2
A _resizeImage() 0 9 2
B _htmlImage() 0 27 11
A _isOutsideSource() 0 3 1
1
<?php
2
/**
3
  * FileUPloadHelper is the helper to the component FileUploadComponent.
4
  * This helper REQUIRES the FileUploadComponent.
5
  *
6
  * @author: Nick Baker
7
  * @version: 6.1.1
8
  * @email: [email protected]
9
  * @link: http://www.webtechnick.com/blogs/view/221/CakePHP_File_Upload_Plugin
10
  *
11
  * @example
12
  *      Show an already uploaded image
13
  *      $fileUpload->image('filename.jpg', array('width' => 250)); //resizes a thumbnail of 'filename.jpg' to 250
14
  *      $fileUpload->image('filename.jpg', array('width' => 250, 'uploadDir' => 'custom/dir')); //resizes a thumbnail of 'webroot/custom/dir/filename.jpg' to 250
15
  *
16
  *      Show the upload field form
17
  *      $fileUpload->input(); //builds the input form based on your FileUploadComponent defaults
18
  *           -or-
19
  *      $fileUpload->input(array('var' => 'fileVar', 'model' => 'Picture')); //customized input form.
20
  *
21
  */
22
App::import('Config', 'FileUpload.file_upload_settings');
23
class FileUploadHelper extends AppHelper{
24
  var $helpers = array('Html', 'Form');
25
26
  /**
27
    * the name of the file passed in.
28
    */
29
  var $fileName = NULL;
30
31
  /**
32
    * Holds the FileUpload component
33
    */
34
  var $FileUpload = NULL;
35
36
  /**
37
    * Counts the number of inputs, for multiple fileUpload inputing.
38
    */
39
  var $inputCount = 0;
40
41
  /**
42
    * Default options for showImage
43
    *
44
    * - width: the width of the image to display (0 means no resizing (default))
45
    * - resizedDir: is the directory in which to save the resized files into (resized by default)
46
    * - imagePathOnly: will only return the requested image_path (false by default)
47
    * - autoResize: will resize the file automatically if given a valid width. (true by default)
48
    * - resizeThumbOnly: will only resize the image down -- not up past the original's size (true by default)
49
    */
50
  var $options = array(
51
    'width' => 0, //0 means no resizing
52
    'resizedDir' => 'resized', // make sure webroot/files/resized is chmod 777
53
    'imagePathOnly' => false, //if true, will only return the requested image_path
54
    'autoResize' => true, //if true, will resize the file automatically if given a valid width.
55
    'resizeThumbOnly' => true //if true, will only resize the image down -- not up past the original's size
56
  );
57
58
  /**
59
    * FileUpload Settings set in config/file_upload_settings.php
60
    */
61
  var $settings = array();
62
63
  /**
64
    * Constructor, initiallizes the FileUpload Component
65
    * and sets the default options.
66
    */
67
  function __construct(View $View, $settings = array()){
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...
68
		parent::__construct($View, $settings);
69
    $this->FileUploadSettings = new FileUploadSettings;
0 ignored issues
show
Bug introduced by
The property FileUploadSettings does not seem to exist. Did you mean FileUpload?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
70
71
    //setup settings
72
    $this->settings = array_merge($this->FileUploadSettings->defaults, $this->options);
0 ignored issues
show
Bug introduced by
The property FileUploadSettings does not seem to exist. Did you mean FileUpload?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
73
  }
74
75
  /**
76
    * Reset the helper to its initial state
77
    * @access public
78
    * @return void
79
    */
80
  function reset(){
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...
81
    $this->fileName = null;
82
    $this->options = array(
83
      'width' => 0,
84
      'resizedDir' => 'resized',
85
      'imagePathOnly' => false,
86
      'autoResize' => true,
87
      'resizeThumbOnly' => true
88
    );
89
90
    //setup settings
91
    $this->settings = array_merge($this->FileUploadSettings->defaults, $this->options);
0 ignored issues
show
Bug introduced by
The property FileUploadSettings does not seem to exist. Did you mean FileUpload?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
92
    unset($this->newImage);
93
  }
94
95
  /**
96
    * image takes a file_name or Upload.id and returns the HTML image
97
    *
98
    * @param String|Int $name takes a file_name or ID of uploaded file.
99
    * @param Array|Int $options takes an array of options passed to the image helper, or an integer representing the width of the image to display
100
    *         options: width = 100 (default), if width is set along with autoResize the uploaded image will be resized.
101
    * @access public
102
    * @return mixed html tag, url string, or false if unable to find image.
103
    */
104
  function image($name, $options = array()){
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...
105
    $this->fileName = $name;
106
    //options takes in a width as well
107
    if(is_int($options)){
108
      $width = $options;
109
      $options = array();
110
      $options['width'] = $width;
111
    }
112
    $this->options = array_merge($this->options, $options);
113
    $this->settings = array_merge($this->settings, $options);
114
115
    $img = false;
116
    if(is_string($name)){
117
      $img = $this->_getImageByName();
118
    }
119
    elseif(is_int($name)){
120
      $img = $this->_getImageById();
121
    }
122
123
    if($img){
0 ignored issues
show
Bug Best Practice introduced by
The expression $img of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
124
      return $img;
125
    }
126
127
    // $this->log("Unable to find $img");
128
    return false;
129
  }
130
131
  /**
132
    * input takes an array of options and display the file browser html input
133
    * options.
134
    * @param Array $options of model and file options.  Defaults to default FileUpload component configuration
135
    * @return String HTML form input element configured for the FileUploadComponent
136
    * @access public
137
    */
138
  function input($options = array()){
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...
139
    $options = array_merge(
140
      array('var' => $this->settings['fileVar'],'model' => $this->settings['fileModel']),
141
      $options
142
    );
143
    $configs = $options;
144
    if($configs['model']){
145
      unset($options['model'], $options['var']);
146
147
      return $this->Form->input("{$configs['model']}.".$this->inputCount++.".{$configs['var']}", array_merge(array('type'=>'file'), $options));
0 ignored issues
show
Documentation introduced by
The property Form does not exist on object<FileUploadHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
148
    }
149
    else {
150
      return "<input type='file' name='data[{$configs['var']}][".$this->inputCount++."]' />";
151
    }
152
  }
153
154
  /**
155
    * @access protected
156
    */
157
  function _getImageById(){
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...
158
    App::import('Component', 'FileUpload.FileUpload');
159
    $this->FileUpload = new FileUploadComponent;
160
161
    $id = $this->fileName;
162
    $this->FileUpload->options['fileModel'] = $this->settings['fileModel'];
163
    $Model =& $this->FileUpload->getModel();
164
    $Model->recursive = -1;
165
    $upload = $Model->findById($id);
166
    if(!empty($upload)){
167
      $this->fileName = $upload[$this->settings['fileModel']][$this->settings['fields']['name']];
168
      return $this->_getImageByName();
169
    }
170
    else{
171
      return false;
172
    }
173
  }
174
175
  /**
176
    * _getFullPath returns the full path of the file name
177
    * @access protected
178
    * @return String full path of the file name
179
    */
180
  function _getFullPath(){
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...
181
    if($this->_isOutsideSource()){
182
      return $this->fileName;
183
    }
184
    else {
185
      return WWW_ROOT . $this->_getUploadPath();
186
    }
187
  }
188
189
  /**
190
    * _getImagePath returns the image path of the file name
191
    * @access protected
192
    * @return String full path of the file name
193
    */
194
  function _getImagePath(){
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...
195
    if($this->_isOutsideSource()){
196
      return $this->fileName;
197
    }
198
    else {
199
      return '/' . $this->_getUploadPath();
200
    }
201
  }
202
203
  /**
204
    * _getUploadPath returns the upload path of all files
205
    * @access protected
206
    * @return String upload path of all files
207
    */
208
  function _getUploadPath(){
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...
209
    return $this->settings['uploadDir'] . '/' . $this->fileName;
210
  }
211
212
  /**
213
    * _getExt returns the extension of the filename.
214
    * @access protected
215
    * @return String extension of filename
216
    */
217
  function _getExt(){
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...
218
    return strrchr($this->fileName,".");
219
  }
220
221
  /**
222
    * Get the image by name and width.
223
    * if width is not specified return full image
224
    * if width is specified, see if width of image exists
225
    * if not, make it, save it, and return it.
226
    * @return String HTML of resized or full image.
227
    */
228
  function _getImageByName(){
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...
229
    //only proceed if we actually have the file in question
230
    if(!$this->_isOutsideSource() && !file_exists($this->_getFullPath())) return false;
231
232
    //resize if we have resize on, a width, and if it doesn't already exist.
233
    if($this->options['autoResize'] && $this->options['width'] > 0 && !file_exists($this->_getResizeNameOrPath($this->_getFullPath()))){
234
      $this->_resizeImage();
235
    }
236
    return $this->_htmlImage();
237
  }
238
239
  /**
240
    * @return String of the resizedpath of a filename or path.
241
    * @access protected
242
    */
243
  function _getResizeNameOrPath($file_name_or_path){
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...
244
    $file_name = basename($file_name_or_path);
245
    $path = substr($file_name_or_path, 0, strlen($file_name_or_path) - strlen($file_name));
246
    $temp_path = substr($file_name,0,strlen($file_name) - strlen($this->_getExt())) . "x" . $this->options['width'] . $this->_getExt();
247
    $full_path = (strlen($this->options['resizedDir']) > 0) ? $path . $this->options['resizedDir'] . '/' . $temp_path : $path . $temp_path;
248
    return $full_path;
249
  }
250
251
  /**
252
    * _resizeImage actually resizes the passed in image.
253
    * @access protected
254
    * @return null
255
    */
256
  function _resizeImage(){
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...
257
    $this->newImage = new RResizeImage($this->_getFullPath());
0 ignored issues
show
Documentation introduced by
The property newImage does not exist on object<FileUploadHelper>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
258
    if($this->newImage->imgWidth > $this->options['width']){
0 ignored issues
show
Documentation introduced by
The property newImage does not exist on object<FileUploadHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
259
      $this->newImage->resize_limitwh($this->options['width'], 0, $this->_getResizeNameOrPath($this->_getFullPath()));
0 ignored issues
show
Documentation introduced by
The property newImage does not exist on object<FileUploadHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
0 is of type integer, but the function expects a object<Numnber>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
260
    }
261
    else {
262
      //$this->autoResize = false;
263
    }
264
  }
265
266
  /**
267
    * _htmlImage returns the atual HTML of the resized/full image asked for
268
    * @access protected
269
    * @return String HTML image asked for
270
    */
271
  function _htmlImage(){
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...
272
    if(!$this->_isOutsideSource() && $this->options['autoResize'] && $this->options['width'] > 0){
273
      if(isset($this->newImage) && $this->newImage->imgWidth && $this->newImage->imgWidth <= $this->options['width']){
0 ignored issues
show
Documentation introduced by
The property newImage does not exist on object<FileUploadHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
274
        $image = $this->_getImagePath();
275
      }
276
      else {
277
        $image = $this->_getResizeNameOrPath($this->_getImagePath());
278
      }
279
    }
280
    else {
281
      $image = $this->_getImagePath();
282
    }
283
284
    $options = $this->options; //copy
285
    //unset the default options
286
    unset($options['resizedDir'], $options['uploadDir'], $options['imagePathOnly'], $options['autoResize'], $options['resizeThumbOnly']);
287
    //unset width only if we're not an outsourced image, we have resize turned on, or we don't have a width to begin with.
288
    if(!$this->_isOutsideSource() && ($this->options['resizeThumbOnly'] || !$options['width'])) unset($options['width']);
289
290
    //return the impage path or image html
291
    if($this->options['imagePathOnly']){
292
      return $image;
293
    }
294
    else {
295
      return $this->Html->image($image, $options);
0 ignored issues
show
Documentation introduced by
The property Html does not exist on object<FileUploadHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
296
    }
297
  }
298
299
  /**
300
    * _isOutsideSource searches the fileName string for :// to determine if the image source is inside or outside our server
301
    */
302
  function _isOutsideSource(){
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...
303
    return !!strpos($this->fileName, '://');
304
  }
305
}
306
307
308
309
310
	/**
311
	 * Image Resizer.
312
	 * @author : Harish Chauhan
313
	 * @copyright : Freeware
314
	 * About :This PHP script will resize the given image and can show on the fly or save as image file.
315
	 *
316
	 */
317
	//define("HAR_AUTO_NAME",1);
318
	Class RResizeImage	{
319
		var $imgFile="";
320
		var $imgWidth=0;
321
		var $imgHeight=0;
322
		var $imgType="";
323
		var $imgAttr="";
324
		var $type=NULL;
325
		var $_img=NULL;
326
		var $_error="";
327
328
		/**
329
		 * Constructor
330
		 *
331
		 * @param [String $imgFile] Image File Name
0 ignored issues
show
Documentation introduced by
The doc-type String">[String could not be parsed: Unknown type name "[" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Documentation introduced by
There is no parameter named $imgFile]. Did you maybe mean $imgFile?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
332
		 * @return RESIZEIMAGE (Class Object)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
333
		 */
334
		function __construct($imgFile=""){
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...
335
			if (!function_exists("imagecreate")){
336
				$this->_error="Error: GD Library is not available.";
337
				return false;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
338
			}
339
340
			$this->type=Array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG', 4 => 'SWF', 5 => 'PSD', 6 => 'BMP', 7 => 'TIFF', 8 => 'TIFF', 9 => 'JPC', 10 => 'JP2', 11 => 'JPX', 12 => 'JB2', 13 => 'SWC', 14 => 'IFF', 15 => 'WBMP', 16 => 'XBM');
341
			if(!empty($imgFile)){
342
				$this->setImage($imgFile);
343
      }
344
		}
345
346
		/**
347
		 * Error occured while resizing the image.
348
		 *
349
		 * @return String
350
		 */
351
		function error(){
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...
352
			return $this->_error;
353
		}
354
355
		/**
356
		 * Set image file name
357
		 *
358
		 * @param String $imgFile
359
		 * @return void
360
		 */
361
		function setImage($imgFile){
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...
362
			$this->imgFile=$imgFile;
363
			return $this->_createImage();
364
		}
365
366
    /**
367
		 * @return void
368
		 */
369
		function close(){
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...
370
			return @imagedestroy($this->_img);
371
		}
372
373
		/**
374
		 * Resize a image to given width and height and keep it's current width and height ratio
375
		 *
376
		 * @param Number $imgwidth
377
		 * @param Numnber $imgheight
378
		 * @param String $newfile
379
		 */
380
		function resize_limitwh($imgwidth,$imgheight,$newfile=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...
381
			$image_per = 100;
382
			list($width, $height, $type, $attr) = @getimagesize($this->imgFile);
0 ignored issues
show
Unused Code introduced by
The assignment to $type is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $attr is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
383
			if($width > $imgwidth && $imgwidth > 0){
384
				$image_per = (double)(($imgwidth * 100) / $width);
385
      }
386
387
			if(floor(($height * $image_per)/100)>$imgheight && $imgheight > 0){
388
				$image_per = (double)(($imgheight * 100) / $height);
389
      }
390
391
			$this->resize_percentage($image_per,$newfile);
392
		}
393
394
    /**
395
		 * Resize an image to given percentage.
396
		 *
397
		 * @param Number $percent
398
		 * @param String $newfile
399
		 * @return Boolean
400
		 */
401
		function resize_percentage($percent=100,$newfile=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...
402
			$newWidth=($this->imgWidth*$percent)/100;
403
			$newHeight=($this->imgHeight*$percent)/100;
404
			return $this->resize($newWidth,$newHeight,$newfile);
405
		}
406
407
    /**
408
		 * Resize an image to given X and Y percentage.
409
		 *
410
		 * @param Number $xpercent
411
		 * @param Number $ypercent
412
		 * @param String $newfile
413
		 * @return Boolean
414
		 */
415
		function resize_xypercentage($xpercent=100,$ypercent=100,$newfile=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...
416
			$newWidth=($this->imgWidth*$xpercent)/100;
417
			$newHeight=($this->imgHeight*$ypercent)/100;
418
			return $this->resize($newWidth,$newHeight,$newfile);
419
		}
420
421
		/**
422
		 * Resize an image to given width and height
423
		 *
424
		 * @param Number $width
425
		 * @param Number $height
426
		 * @param String $newfile
427
		 * @return Boolean
428
		 */
429
		function resize($width,$height,$newfile=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...
430
			if(empty($this->imgFile)){
431
				$this->_error="File name is not initialised.";
432
				return false;
433
			}
434
			if($this->imgWidth<=0 || $this->imgHeight<=0){
435
				$this->_error="Could not resize given image";
436
				return false;
437
			}
438
			if($width<=0)	$width=$this->imgWidth;
439
			if($height<=0) $height=$this->imgHeight;
440
441
			return $this->_resize($width,$height,$newfile);
442
		}
443
444
		/**
445
		 * Get the image attributes
446
		 * @access Private
447
		 *
448
		 */
449
		function _getImageInfo()
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...
450
		{
451
			@list($this->imgWidth,$this->imgHeight,$type,$this->imgAttr)=@getimagesize($this->imgFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
452
			$this->imgType=$this->type[$type];
453
		}
454
455
		/**
456
		 * Create the image resource
457
		 * @access Private
458
		 * @return Boolean
459
		 */
460
		function _createImage(){
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...
461
			$this->_getImageInfo();
462
			if($this->imgType=='GIF'){
463
				$this->_img=@imagecreatefromgif($this->imgFile);
464
			}
465
			elseif($this->imgType=='JPG'){
466
				$this->_img=@imagecreatefromjpeg($this->imgFile);
467
			}
468
			elseif($this->imgType=='PNG'){
469
				$this->_img=@imagecreatefrompng($this->imgFile);
470
			}
471
472
			if(!$this->_img || !@is_resource($this->_img)){
473
				$this->_error="Error loading ".$this->imgFile;
474
				return false;
475
			}
476
			return true;
477
		}
478
479
		/**
480
		 * Function is used to resize the image
481
		 *
482
		 * @access Private
483
		 * @param Number $width
484
		 * @param Number $height
485
		 * @param String $newfile
486
		 * @return Boolean
487
		 */
488
		function _resize($width,$height,$newfile=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...
489
			if (!function_exists("imagecreate")){
490
				$this->_error="Error: GD Library is not available.";
491
				return false;
492
			}
493
494
			$newimg=@imagecreatetruecolor($width,$height);
495
			//imagecolortransparent( $newimg, imagecolorat( $newimg, 0, 0 ) );
496
497
			if($this->imgType=='GIF' || $this->imgType=='PNG')	{
498
				/** Code to keep transparency of image **/
499
				$colorcount = imagecolorstotal($this->_img);
500
				if ($colorcount == 0) $colorcount = 256;
501
				imagetruecolortopalette($newimg,true,$colorcount);
502
				imagepalettecopy($newimg,$this->_img);
503
				$transparentcolor = imagecolortransparent($this->_img);
504
				imagefill($newimg,0,0,$transparentcolor);
505
				imagecolortransparent($newimg,$transparentcolor);
506
			}
507
508
			@imagecopyresampled ( $newimg, $this->_img, 0,0,0,0, $width, $height, $this->imgWidth,$this->imgHeight);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
509
510
			if($newfile===1)	{
511
				if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches)){
512
			   		$newfile=@substr_replace($this->imgFile,"_har",-@strlen($matches[0]),0);
513
        }
514
			}
515
			elseif(!empty($newfile)){
516
				if(!@preg_match("/\..*+$/",@basename($newfile))){
517
					if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches)){
518
					   $newfile=$newfile.$matches[0];
519
          }
520
				}
521
			}
522
523
			if($this->imgType=='GIF'){
524
				if(!empty($newfile)){
525
          @imagegif($newimg,$newfile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
526
        }
527
				else {
528
					@header("Content-type: image/gif");
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
529
					@imagegif($newimg);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
530
				}
531
			}
532 View Code Duplication
			elseif($this->imgType=='JPG'){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
533
				if(!empty($newfile)){
534
					@imagejpeg($newimg,$newfile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
535
        }
536
				else	{
537
					@header("Content-type: image/jpeg");
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
538
					@imagejpeg($newimg);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
539
				}
540
			}
541 View Code Duplication
			elseif($this->imgType=='PNG'){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
542
				if(!empty($newfile)){
543
					@imagepng($newimg,$newfile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
544
        }
545
				else{
546
					@header("Content-type: image/png");
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
547
					@imagepng($newimg);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
548
				}
549
			}
550
			@imagedestroy($newimg);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
551
		}
552
	}
553
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...