PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/core/libs/upload/adapters/image_upload.php

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