<?php
class Redimensiona{
	
	public function Redimensionar($imagem, $max_x, $max_y, $pasta, $tipo = "image/jpeg", $name){
		
		$novo_x = (int) $max_x;
		$novo_y = (int) $max_y;
		if ($tipo == "image/jpeg" || $tipo == "image/pjpeg"){
			$img = imagecreatefromjpeg($imagem);
			
		}else if ($tipo == "image/gif"){
			$img = imagecreatefromgif($imagem);
			$transparent_index = ImageColorTransparent($img);
			if($transparent_index!=(-1)) $transparent_color = ImageColorsForIndex($img,$transparent_index);

		}else if ($tipo == "image/png" || $tipo == "image/x-png"){
			$img = imagecreatefrompng($imagem);
			// $transparent_index = ImageColorTransparent($img);
			// if($transparent_index!=(-1)) $transparent_color = ImageColorsForIndex($img,$transparent_index);

		}

		if(isset($img)){

			$original_x   = imagesx($img);
			$original_y   = imagesy($img);

			// verifica se a largura ou altura da imagem é maior que o valor
	     	// máximo permitido
		    if ( ( $original_x > $novo_x ) || ( $original_y > $novo_y ) ){
			// verifica o que é maior na imagem, largura ou altura?
			    if ( $original_x > $original_y ) {	
					$novo_y	= 	( $novo_x * $original_y ) / $original_x;
				}elseif($original_y > $original_x){
					$novo_x	= 	( $novo_y * $original_x ) / $original_y;
				}else{
					if($novo_x > $novo_y){
						$novo_x	= 	( $novo_y * $original_x ) / $original_y;
					}else{
						$novo_y	= 	( $novo_x * $original_y ) / $original_x;
					}
				}
			}else{
				
				$novo_x = $original_x;
				$novo_y = $original_y;
			}

			while($novo_x > $max_x || $novo_y > $max_y){
				$novo_x *= 0.9;
				$novo_y *= 0.9;
			}

				$nova = imagecreatetruecolor($novo_x, $novo_y);

				if($tipo == "image/png" || $tipo == "image/x-png"){
					// Fundo transparente (PNG)
					imagealphablending($nova, false);
			        imagesavealpha($nova  , true );
		    		$transparent = imagecolorallocatealpha($nova, 255, 255, 255, 127);
                	imagefilledrectangle($nova, 0, 0, $novo_x, $novo_y, $transparent);
		    	}

		    	if(!empty($transparent_color)){
				    $transparent_new = ImageColorAllocate( $nova, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue'] );
				    $transparent_new_index = ImageColorTransparent( $nova, $transparent_new );
				    ImageFill( $nova, 0,0, $transparent_new_index ); /* don't forget to fill the new image with the transparent color */
				}

				imagecopyresampled($nova, $img, 0, 0, 0, 0, $novo_x, $novo_y, $original_x, $original_y);
				
				if ($tipo == "image/jpeg" || $tipo == "image/pjpeg"){
					$local="$pasta/$name";
					imagejpeg($nova, $local);
				}else if ($tipo == "image/gif"){
					$local="$pasta/$name";
					imagegif($nova, $local);
				}else if ($tipo == "image/png" || $tipo == "image/x-png"){
					$local="$pasta/$name";
					imagepng($nova, $local);
				}		
				
				imagedestroy($img);
				imagedestroy($nova);

			return $name;
		}	
	
	}

	public function GerarThumbs($imagem, $max_x, $max_y, $pasta, $tipo = "image/jpeg", $name){
		
		$novo_x = (int) $max_x;
		$novo_y = (int) $max_y;
		if ($tipo == "image/jpeg" || $tipo == "image/pjpeg"){
			$img = imagecreatefromjpeg($imagem);
			
		}else if ($tipo == "image/gif"){
			$img = imagecreatefromgif($imagem);
			$transparent_index = ImageColorTransparent($img);
			if($transparent_index!=(-1)) $transparent_color = ImageColorsForIndex($img,$transparent_index);

		}else if ($tipo == "image/png" || $tipo == "image/x-png"){
			$img = imagecreatefrompng($imagem);
			// $transparent_index = ImageColorTransparent($img);
			// if($transparent_index!=(-1)) $transparent_color = ImageColorsForIndex($img,$transparent_index);

		}

		if(isset($img)){

			$original_x   = imagesx($img);
			$original_y   = imagesy($img);

		    if ( ( $original_x > $novo_x ) || ( $original_y > $novo_y ) ){
			    if ( $original_x < $original_y ) {	
					$novo_y	= 	( $novo_x * $original_y ) / $original_x;
				}elseif($original_y < $original_x){
					$novo_x	= 	( $novo_y * $original_x ) / $original_y;
				}else{
					if($novo_x < $novo_y){
						$novo_x	= 	( $novo_y * $original_x ) / $original_y;
					}else{
						$novo_y	= 	( $novo_x * $original_y ) / $original_x;
					}
				}
			}else{
				
				$novo_x = $original_x;
				$novo_y = $original_y;
			}

			while($novo_x < $max_x || $novo_y < $max_y){
				$novo_x *= 1.1;
				$novo_y *= 1.1;
			}

				$redm = imagecreatetruecolor($novo_x, $novo_y);

				if($tipo == "image/png" || $tipo == "image/x-png"){
					// Fundo transparente (PNG)
					imagealphablending($redm, false);
			        imagesavealpha($redm  , true );
		    		$transparent = imagecolorallocatealpha($redm, 255, 255, 255, 127);
                	imagefilledrectangle($redm, 0, 0, $novo_x, $novo_y, $transparent);
		    	}

		    	if(!empty($transparent_color)){
				    $transparent_new = ImageColorAllocate( $redm, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue'] );
				    $transparent_new_index = ImageColorTransparent( $redm, $transparent_new );
				    ImageFill( $redm, 0,0, $transparent_new_index ); /* don't forget to fill the new image with the transparent color */
				}

				imagecopyresampled($redm, $img, 0, 0, 0, 0, $novo_x, $novo_y, $original_x, $original_y);

				$nova = imagecreatetruecolor($max_x, $max_y);

				if($tipo == "image/png" || $tipo == "image/x-png"){
					// Fundo transparente (PNG)
					imagealphablending($nova, false);
			        imagesavealpha($nova  , true );
		    		$transparent = imagecolorallocatealpha($nova, 255, 255, 255, 127);
                	imagefilledrectangle($nova, 0, 0, $novo_x, $novo_y, $transparent);
		    	}

		    	if(!empty($transparent_color)){
				    $transparent_new = ImageColorAllocate( $nova, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue'] );
				    $transparent_new_index = ImageColorTransparent( $nova, $transparent_new );
				    ImageFill( $nova, 0,0, $transparent_new_index ); /* don't forget to fill the new image with the transparent color */
				}

				imagecopy($nova, $redm, 0, 0 , 0 , 0, $max_x, $max_y);

				if ($tipo == "image/jpeg" || $tipo == "image/pjpeg"){
					$local="$pasta/$name";
					imagejpeg($nova, $local);
				}else if ($tipo == "image/gif"){
					$local="$pasta/$name";
					imagegif($nova, $local);
				}else if ($tipo == "image/png" || $tipo == "image/x-png"){
					$local="$pasta/$name";
					imagepng($nova, $local);
				}		

				// unlink("imagens-site/temp/".$name);		

				imagedestroy($redm);
				imagedestroy($img);
				imagedestroy($nova);

				

			return $name;
		}	
	
	}




	// TRATAR NOME DE ARQUIVOS DE UPLOAD //
	public function tratar_arquivo_upload($string){
	   // pegando a extensao do arquivo
	   $partes 		= explode(".", $string);
	   $extensao 		= $partes[count($partes)-1];	
	   // somente o nome do arquivo
	   $nome			= preg_replace('/\.[^.]*$/', '', $string);	
	   // removendo simbolos, acentos etc
	   $a = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýýþÿRr?';
	   $b = 'aaaaaaaceeeeiiiidnoooooouuuuybsaaaaaaaceeeeiiiidnoooooouuuuyybyRr-';
	   $nome = strtr($nome, utf8_decode($a), $b);
	   $nome = str_replace(".","-",$nome);
	   $nome = preg_replace( "/[^0-9a-zA-Z\.]+/",'-',$nome);
	   return utf8_decode(strtolower($nome.".".$extensao));
	}
}
?>