ImageUpload::_saveFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.
9
 *
10
 * @category   Kumbia
11
 * @package    Upload
12
 * @subpackage Adapters
13
 *
14
 * @copyright  Copyright (c) 2005 - 2019 KumbiaPHP Team (http://www.kumbiaphp.com)
15
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
16
 */
17
18
/**
19
 * Clase para guardar imagen subida
20
 *
21
 * @category   Kumbia
22
 * @package    Upload
23
 * @subpackage Adapters
24
 */
25
class ImageUpload extends Upload
26
{
27
    /**
28
     * Información de la imagen
29
     *
30
     * @var array|boolean
31
     */
32
    protected $_imgInfo;
33
    /**
34
     * Ancho mínimo de la imagen
35
     *
36
     * @var int
37
     */
38
    protected $_minWidth = NULL;
39
    /**
40
     * Ancho máximo de la imagen
41
     *
42
     * @var int
43
     */
44
    protected $_maxWidth = NULL;
45
    /**
46
     * Alto mínimo de la imagen
47
     *
48
     * @var int
49
     */
50
    protected $_minHeight = NULL;
51
    /**
52
     * Alto máximo de la imagen
53
     *
54
     * @var int
55
     */
56
    protected $_maxHeight = NULL;
57
58
    /**
59
     * Constructor
60
     *
61
     * @param string $name nombre de archivo por metodo POST
62
     */
63
    public function __construct($name)
64
    {
65
        parent::__construct($name);
66
67
        $this->_imgInfo = getimagesize($_FILES[$name]['tmp_name']);
68
69
        // Ruta donde se guardara el archivo
70
        $this->_path = dirname($_SERVER['SCRIPT_FILENAME']) . '/img/upload';
71
    }
72
73
    /**
74
     * Asigna la ruta al directorio de destino para la imagen
75
     *
76
     * @param string $path ruta al directorio de destino (Ej: /home/usuario/data)
77
     */
78
    public function setPath($path)
79
    {
80
        $this->_path = $path;
81
    }
82
83
    /**
84
     * Asigna el ancho mínimo de la imagen
85
     *
86
     * @param int $value
87
     */
88
    public function setMinWidth($value)
89
    {
90
        $this->_minWidth = $value;
91
    }
92
93
    /**
94
     * Asigna el ancho máximo de la imagen
95
     *
96
     * @param int $value
97
     */
98
    public function setMaxWidth($value)
99
    {
100
        $this->_maxWidth = $value;
101
    }
102
103
    /**
104
     * Asigna el alto mínimo de la imagen
105
     *
106
     * @param int $value
107
     */
108
    public function setMinHeight($value)
109
    {
110
        $this->_minHeight = $value;
111
    }
112
113
    /**
114
     * Asigna el alto máximo de la imagen
115
     *
116
     * @param int $value
117
     */
118
    public function setMaxHeight($value)
119
    {
120
        $this->_maxHeight = $value;
121
    }
122
123
    /**
124
     * Valida el archivo antes de guardar
125
     *
126
     * @return boolean
127
     */
128
    protected function _validates()
129
    {
130
        // Verifica que se pueda escribir en el directorio
131
        if (!is_writable($this->_path)) {
132
            Flash::error('Error: no se puede escribir en el directorio');
133
            return FALSE;
134
        }
135
136
137
        $image = $this->_imgInfo;
138
        // Verifica que sea un archivo de imagen
139
        if (!$image){
140
            Flash::error('Error: el archivo debe ser una imagen');
141
            return FALSE;
142
        }
143
144
        // Verifica ancho minimo de la imagen
145 View Code Duplication
        if ($this->_minWidth !== NULL) {
0 ignored issues
show
Duplication introduced by joanhey
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...
146
            if ($image[0] < $this->_minWidth) {
147
                Flash::error("Error: el ancho de la imagen debe ser superior o igual a {$this->_minWidth}px");
148
                return FALSE;
149
            }
150
        }
151
152
        // Verifica ancho maximo de la imagen
153 View Code Duplication
        if ($this->_maxWidth !== NULL) {
0 ignored issues
show
Duplication introduced by joanhey
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...
154
            if ($image[0] > $this->_maxWidth) {
155
                Flash::error("Error: el ancho de la imagen debe ser inferior o igual a {$this->_maxWidth}px");
156
                return FALSE;
157
            }
158
        }
159
160
        // Verifica alto minimo de la imagen
161 View Code Duplication
        if ($this->_minHeight !== NULL) {
0 ignored issues
show
Duplication introduced by joanhey
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...
162
            if ($image[1] < $this->_minHeight) {
163
                Flash::error("Error: el alto de la imagen debe ser superior o igual a {$this->_minHeight}px");
164
                return FALSE;
165
            }
166
        }
167
168
        // Verifica alto maximo de la imagen
169 View Code Duplication
        if ($this->_maxHeight !== NULL) {
0 ignored issues
show
Duplication introduced by joanhey
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...
170
            if ($image[1] > $this->_maxHeight) {
171
                Flash::error("Error: el alto de la imagen debe ser inferior o igual a {$this->_maxHeight}px");
172
                return FALSE;
173
            }
174
        }
175
176
        // Validaciones
177
        return parent::_validates();
178
    }
179
180
    /**
181
     * Valida que el tipo de archivo
182
     *
183
     * @return boolean
184
     */
185
    protected function _validatesTypes()
186
    {
187
        // Verifica que sea un archivo de imagen
188
        if (!$this->_imgInfo) return FALSE;
189
190
        foreach ($this->_types as $type) {
191
            if ($this->_imgInfo['mime'] == "image/$type") return TRUE;
192
        }
193
194
        return FALSE;
195
    }
196
197
    /**
198
     * Guardar el archivo en el servidor
199
     *
200
     * @param string $name nombre con el que se guardará el archivo
201
     * @return boolean
202
     */
203
    protected function _saveFile($name)
204
    {
205
        return move_uploaded_file($_FILES[$this->_name]['tmp_name'], "$this->_path/$name");
206
    }
207
208
}
209