All pastes #446534 Raw Edit

Stuff

public text v1 · immutable
·published 2007-04-18 15:31 UTC
rendered paste body
class generarGIFAnimado {
	var $caracteres;
	var $stringParaRealizar;
	
	var $scroll;
	var $velocidadscroll;
	
	var $tamMinCaracter;
	var $tamMaxCaracter;
	
	var $minAnguloRotacion;
	var $maxAnguloRotacion;
	
	var $tipografias;
	

	var $x;
	var $y;
	

	var $r;
	var $g;
	var $b;
	
	var $gifCreado;
	var $directorioTipografias;
	var $directorioFrames;
	var $directorioCortoFrames;
	
	
	var $arrayCoordenadasVariables;
	
	var $arrayCoordenadasIniciales;
	
	var $arrayCoordenadasVariablesPasadas;
	
	
	var $arrayCoordenadasSignos;
	
	
	/* Constructor de la clase */
	
	function generarGIFAnimado($config) {
		/* Guardo los valores en las variables */
		if( is_array($config)) {
		  $valid = get_class_vars(get_class($this));
			foreach($config as $k=>$v) {
				if(array_key_exists($k,$valid)) { $this->$k = $v; }
			}
		}
		
		/* Compruebo Tamaños de  caracter */
		
		if (( $this->tamMinCaracter > $this->tamMaxCaracter ) || ( !is_numeric($this->tamMinCaracter)  ) || ( !is_numeric($this->tamMaxCaracter) ))  {
				$this->tamMinCaracter = 12;
				$this->tamMaxCaracter = 12;
		}

		/* Compruebo angulo de rotacion */
		if ( !is_numeric($this->minAnguloRotacion) ) { $this->minAnguloRotacion = 0; }
		if ( !is_numeric($this->maxAnguloRotacion) ) { $this->maxAnguloRotacion = 0; }
		
		if ( $this->maxAnguloRotacion <= $this->minAnguloRotacion ) {
			$this->minAnguloRotacion=0;
			$this->maxAnguloRotacion=0;
		}
		
		
		
		
		/* Directorios */
		if( !is_dir($this->directorioFrames) )  { $this->directorioFrames='/home/bond/public_html/frames/'; }
		if( !is_dir($this->directorioTipografias) )  { $this->directorioTipografias='/home/bond/public_html/tipografias/'; }
		
		if( strlen($this->directorioCortoFrames) == 0 )  { $this->directorioCortoFrames='/frames/'; }
		
		
		
		 /* Compruebo tipografias */
		if (is_array($this->tipografias)) {
				$temp = array();
				foreach($this->tipografias as $k=>$v) {
					$dir=$this->directorioTipografias.$v;
				
					
					
					if( is_readable($dir))  {
						 $temp[] = $v;
					 }
					
					
					
				}
				$this->tipografias = $temp;
				
			}
		if ( count($this->tipografias) < 1) { die("error jaja"); }
		
		
		
		
		
		/* Compruebo el string a realizar */
		$this->caracteres=strlen($this->stringParaRealizar);
		if ( $this->caracteres == 0 ) { 
		    $this->stringParaRealizar="error";
		    $this->caracteres=5;
		 }
		
		/* Anchura y altura de la imagen */
		
		if ( $this->tamMaxCaracter == $this->tamMinCaracter ) { $proporcion = 2; } 
		else { $proporcion=1.5; }
		$this->x = round( ($this->caracteres) * round( (($this->tamMaxCaracter + $this->tamMinCaracter) / $proporcion) ) + ( ($this->caracteres + 1) * 5));
		$this->y = round((2.4 * $this->tamMaxCaracter));
		
		
		/* scroll */
	   if( strlen($this->scroll) == 0 )  { $this->scroll='no'; }
		
	   if (( !is_numeric($this->velocidadscroll) ) || ( $this->velocidadscroll >= $this->y ))  { $this->velocidadscroll=round($this->y / 3); }
		 
		
       $this->crearImagen();
		
      }
      
      /* normal frame */
      function crearFrame($angulo,$nombreImagen) {
	   $im=@imagecreatetruecolor($this->x,$this->y);
       $black=@imagecolorallocate($im,0,0,0);
       @imagefill($im,0,0,$black);
       $xInicial=5;
       $yInicial=round($this->y / 2);
       
       for ( $i = 0; $i < $this->caracteres;$i ++ ) {
	   $tamLetra=mt_rand($this->tamMinCaracter,$this->tamMaxCaracter);
	   $tipoLetra=$this->cambiar_tipografia();
	   
	   $this->random_color(0,127);
	   $color=@imagecolorallocate($im,$this->r,$this->g,$this->b);
	   
	   @imagettftext($im,$tamLetra,$angulo,$xInicial,$yInicial,$color,$tipoLetra,substr($this->stringParaRealizar,$i,1));
	   $xInicial=$xInicial + $tamLetra + 5;
       }  
       $tcount=sprintf("%04d",$angulo);
       
       
       $nombreImagen=$nombreImagen."framenagulo".$tcount.".gif";
       @imagegif($im,$nombreImagen);
       @imagedestroy($im);
       
      }
      

           /* scroll frame */
      
      function crearFrameScroll($nombreImagen) {
	
       $terminado="no";
       $cantidadFines=0;
       $numeroImagen=0;
       
       
       
       while ( $terminado == "no" ) {
       
	       
	       
	   $im=@imagecreatetruecolor($this->x,$this->y);
       $black=@imagecolorallocate($im,0,0,0);
       @imagefill($im,0,0,$black);
       $xInicial=5;
	       
       
	      /* caracter while */
       for ( $i = 0; $i < $this->caracteres;$i ++ ) {
	   $tamLetra=mt_rand($this->tamMinCaracter,$this->tamMaxCaracter);
	   $tipoLetra=$this->cambiar_tipografia();
	   $this->random_color(0,127);
	   $color=@imagecolorallocate($im,$this->r,$this->g,$this->b);
	   
	   
	   $velocidad=$this->velocidadscroll;
	   $tipoIncremento=$this->arrayCoordenadasSignos[$i];
	 
	
	   $coordenadaInicial=round($this->arrayCoordenadasIniciales[$i]);
	   $coordenadaVariable=round($this->arrayCoordenadasVariables[$i]);
       $cantidadPasadas=$this->arrayCoordenadasVariablesPasadas[$i];
	   
	   
	   /* Letter prom high */
	    $promedioTamanio=round((( $this->tamMaxCaracter + $this->tamMinCaracter) / 2));
	
	    
	  
	    
       /* Abajo */
	    
	    	
	  if  (( $tipoIncremento == "+" ) && ( $cantidadPasadas > 0 )) {
		if ( $coordenadaVariable >= abs($coordenadaInicial) )   { $cantidadFines++; }
	
	  }

	 /* arriba */
	
    if (( $tipoIncremento == "-" ) && ( $cantidadPasadas > 0 )) { 
	   if ( $coordenadaVariable <= abs($coordenadaInicial) )   { $cantidadFines++; }
		
	  }
	    
	    
	  
	
	  // Limite inferior cuando tipo voy para arriba //
	   if  (( $coordenadaVariable <=  0 ) && ( $tipoIncremento == "-" )) { 
		    $this->arrayCoordenadasVariables[$i]=$this->y + $promedioTamanio;  
		    
		    $this->arrayCoordenadasVariablesPasadas[$i]=$this->arrayCoordenadasVariablesPasadas[$i] + 1;
		    
	   }
	   
	   // Limite superior cuando voy para abajo //
	  if  (( $coordenadaVariable >=  ($this->y + $promedioTamanio) ) && ( $tipoIncremento == "+" )) { 
		   $this->arrayCoordenadasVariables[$i]=0;  
		   $this->arrayCoordenadasVariablesPasadas[$i]=$this->arrayCoordenadasVariablesPasadas[$i] + 1;
		   
	   }
	   
	/* Dibujo texto 
	   @imagettftext($im,$tamLetra,0,$xInicial,$this->arrayCoordenadasVariables[$i],$color,$tipoLetra,substr($this->stringParaRealizar,$i,1));
	   $xInicial=$xInicial + $tamLetra + 5;
      
	   
	   // Muevo coordenadas
	   if ( $tipoIncremento == "-" ) {
	   $this->arrayCoordenadasVariables[$i]= $this->arrayCoordenadasVariables[$i] - $velocidad;
       }
      else {
	    $this->arrayCoordenadasVariables[$i]= $this->arrayCoordenadasVariables[$i] + $velocidad;
       }
	   

	    }
	     For */
       
        $numeroImagen++;
        $tcount=sprintf("%08d",$numeroImagen);
        $nombreReal=$nombreImagen."framenagulo".$tcount.".gif";
        @imagegif($im,$nombreReal);
        @imagedestroy($im);
       if ( $cantidadFines >= $this->caracteres ) {   $terminado="si";  }
      
	   
       }  
       /* While */
       
      }
  
      function crearImagen() {
	   $tiempo=time();
	   $nombreImagen=$this->directorioFrames;
       $nombreImagen=$nombreImagen.$tiempo."/";
       if ( !file_exists($nombreImagen) ) {  mkdir($nombreImagen,0777);  }
	     
	  
    if ( $this->scroll == "no" ) {
	   
         for ( $i = $this->minAnguloRotacion; $i <= $this->maxAnguloRotacion;$i=$i + 25 ) {
	         $this->crearFrame($i,$nombreImagen);
         
       }
     }
       
     
     if ( $this->scroll == "si" ) { 
	      $temp1=array();
	      $temp2=array();
	      $temp3=array();
	      $temp4=array();
	     for ( $i = 0; $i < $this->caracteres;$i ++ ) {
	
	          $valor=round($this->y - ($i * 10));
		      $temp1[]=$valor;
		      if ( $tipoIncremento == "-" ) { 
			   $cuenta=$valor - $this->velocidadscroll;
		       $tipoIncremento="+";
		      }
	         else { 
		     $tipoIncremento="-"; 
	         $cuenta=$valor + $this->velocidadscroll;
	         
	         }
		      $temp2[]=round($cuenta);
		      $temp3[]=$tipoIncremento; 
		      $temp4[]=0;
		      
	      }
		  $this->arrayCoordenadasVariables=$temp1;
		  $this->arrayCoordenadasIniciales=$temp2;
		  $this->arrayCoordenadasSignos=$temp3;
		  $this->arrayCoordenadasVariablesPasadas=$temp4;
		  
		  
	      $this->crearFrameScroll($nombreImagen);

     }
     
     
       
       
      $imagenFinal=$nombreImagen."result.gif";
      $str="convert -delay 10 -loop 0 ".$nombreImagen."framenagulo*.gif ".$imagenFinal;
      exec($str);
       
        
     
         
      $nombreImagen=$this->directorioCortoFrames.$tiempo."/result.gif";
      $this->gifCreado=$nombreImagen;
       
      }
      
	
	
	
	function random_color($min,$max) {
			
			$this->r = mt_rand($min,$max) + 127;
			
			$this->g = mt_rand($min,$max) + 127 ;
			
			$this->b = mt_rand($min,$max) + 127;
			$this->r=255;
			$this->g=255;
			$this->b=255;
    }
    
    
    

    
	function obtenerGIF() {
		
		 return $this->gifCreado; 
	
	
	}
	function obtenerString() {
		return $this->stringParaRealizar." ".$this->caracteres;
		 
	
	
	}
	
	
		
	

	
	
	
	
	function cambiar_tipografia()  {
		if(is_array($this->tipografias)) {	
		 srand((float)microtime() * 10000000);
		$clave = array_rand($this->tipografias);
         $tipografia=$this->tipografias[$clave];	
		}
		return $this->directorioTipografias.$tipografia;
	}

	
	
		
  
		
		
	
	
}