Ver Mensaje Individual
  #47 (permalink)  
Antiguo 12/09/2015, 06:37
Avatar de IsaBelM
IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 15 años, 10 meses
Puntos: 1012
Respuesta: [APORTE] efectos sin jquery

subirArchivosResize.php

Código:
<?php

class editarImg {

	private $_imagen;
	private $_ancho;
	private $_alto;
	public $_tiposPermitidos = array(
		IMAGETYPE_JPEG => 'image/jpeg; charset=binary',
		IMAGETYPE_GIF => 'image/gif; charset=binary',
		IMAGETYPE_PNG => 'image/png; charset=binary'
	);
	public $_tipo;
	//private $_mime;
	public $_nombre;
	private $_tmp_nombre;
	private $_tamanoImg;
	private $_tamanoMax = 170000;
	private $_dir = 'SubirArchivos/';
	public $_guardarEn;
	private $_archivo;
	public $_resp = array();
	private $_escalaTipo;
	private $_escalaValor;
	private $_escala = 100;
	private $_error = 0;



	public function tomaInfo($fileName, $escala) {

		list($ancho, $alto, $tipoNOusado, $str) = getimagesize($fileName['tmp_name']);

		$this->_ancho = $ancho;
		$this->_alto = $alto;
		$this->_archivo = $fileName;
		$this->_tmp_nombre = $this->_archivo['tmp_name'];
		$finfo = new finfo();
		$this->_tipo = $finfo->file($this->_tmp_nombre, FILEINFO_MIME);
/*
$finfo = finfo_open();
$this->_tipo = finfo_file($finfo, $fileName['tmp_name'], FILEINFO_MIME);
finfo_close($finfo);
*/
		$this->_nombre = $this->_archivo['name'];
		$this->_tamanoImg = $this->_archivo['size'];
		$this->_error = $this->_archivo['error'];
		$this->_escalaTipo = $escala['tipo'];
		$this->_escalaValor = $escala['valor'];

		if ($this->_tipo == $this->_tiposPermitidos[IMAGETYPE_JPEG]) {

			$this->_imagen = imagecreatefromjpeg($this->_tmp_nombre);

		} elseif ($this->_tipo == $this->_tiposPermitidos[IMAGETYPE_GIF]) {

			$this->_imagen = imagecreatefromgif($this->_tmp_nombre);

		} elseif ($this->_tipo == $this->_tiposPermitidos[IMAGETYPE_PNG]) {

			$this->_imagen = imagecreatefrompng($this->_tmp_nombre);

		}

		$this->guardar();
	}




	private function guardar() {

		if (!empty($this->_archivo) && $this->_error == UPLOAD_ERR_OK) {

			if (($respuesta = $this->validarExtension()) === true) {

				if (($respuesta = $this->validarTamano()) === true) {

					$nombreGenerado = $this->aleatorio();
					$this->_guardarEn = $this->_dir . '' . $nombreGenerado . '' .$this->_nombre;
					
					move_uploaded_file($this->_tmp_nombre, $this->_guardarEn);

					$this->_escala = implode(", ", $this->_escalaValor);

					if ($this->_escalaTipo == 'relativa') {

						$this->escalaRelativa($this->_escala);

					} elseif ($this->_escalaTipo == 'absoluta') {

						$this->escalaAbsoluta($this->_escala);
					}

					array_push($this->_resp, 'ok');

				} else {

					array_push($this->_resp, $respuesta);
				}

			} else {

				array_push($this->_resp, $respuesta);
			}
		}

	}



	private function aleatorio() {

		return md5(mt_rand() * time()); // rand()
	}
	


	public function obtenerTipo() {

		return in_array($this->_tipo, $this->_tiposPermitidos) ? $this->_tipo : null;
	}


	private function validarExtension() {

		//return in_array($this->_tipo, $this->_tiposPermitidos) ? true : 'El tipo de archivo no está permitido. Se admiten ( ' . implode(', ', $this->_tiposPermitidos) . ' )';
		return in_array($this->_tipo, $this->_tiposPermitidos) ? true : 'ko1';
	}



	private function validarTamano() {

		//return ($this->_tamanoImg <= $this->_tamanoMax) ? true : 'El tamaño del archivo supera el máximo permitido';
		return ($this->_tamanoImg <= $this->_tamanoMax) ? true : 'ko2';
	}


	private function escalaRelativa($escala) {

		$ancho = $this->_ancho * $escala / 100;
		$alto = $this->_alto * $escala / 100;
		$this->redimensionar($ancho, $alto);
	}
 

	private function escalaAbsoluta($escala) {

		$valores = explode(",", $escala);
		$ancho = $valores[0];
		$alto = $valores[1];
		$this->redimensionar($ancho, $alto);
	}


	private function redimensionar($ancho, $alto) {
		
		$nuevaImagen = imagecreatetruecolor($ancho, $alto);

		imagecopyresampled($nuevaImagen, $this->_imagen, 0, 0, 0, 0, $ancho, $alto, $this->_ancho, $this->_alto);
		imagejpeg($nuevaImagen, $this->_guardarEn, 100);
		imagedestroy($nuevaImagen);
	}

};

?>
__________________
if(ViolenciaDeGénero) {alert('MUJER ASESINADA');}