Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/11/2008, 14:00
Avatar de pato12
pato12
 
Fecha de Ingreso: septiembre-2007
Ubicación: Salta
Mensajes: 1.620
Antigüedad: 16 años, 7 meses
Puntos: 101
[APORTE] EasyThumbnail 2.0

Hola,
Cree la nueva version del EasyThumbnail.
Agrege public y private a peticion del publico.
Nuevas funciones:
  • Capas de crear bordes a las imagenes
  • Editar imagenes GIF y PNG con trasparencia
  • Crear una imagen desde 0 con el formato elejido
  • Editar imagen desde posision X1 Y1 a la X2 Y2
  • Insertar lineas
  • Funcion que te dice los errores producidos
  • Insertar imagen (codigo HTML)
  • Modernización del código
Se lo usa distinto.
Codigo:
Código PHP:
<?php
/******************************************************************************\
** *****************************************************************************
**                               EasyThumbnail                                **
** *****************************************************************************
** Nombre      : EasyThumbnail                                                **
**                                                                            **
** Creador     : Pato12 <de Forosdelweb.com>                                  **
**                                                                            **
** Description : Sirve para redimensionar una imagen, para crear Thumbnail    **
**               y para escribir una imagen fácilmente.                       **
**                                                                            **
** Contacto    : MP de forosdelweb.com al usuario Pato12                      **
**                                                                            **
** Version     : 2.0 (BETA)                                                   **
**                                                                            **
** Web del                                                                    **
** creador     : www.halfmusic.com.ar                                         **
********************************************************************************
********************************************************************************
*   Este scriopt PHP es libre de usar siempre cuando no borren estas lineas
*   y respeten la licencia GPL :
*   http://opensource.org/licenses/gpl-license.php GNU Public License
\******************************************************************************/
    
class EasyThumbnail{
    private 
$imagen;
    private 
$image;
    private 
$height;
    private 
$width;
    private 
$error=array();
    private 
$calidad;
    private 
$srcw;
    private 
$srch;
    private 
$ext;
    private 
$et;
    private 
$x=array(0,0);
    private 
$y=array(0,0);
    public function 
__construct($e,$w 60,$h 60,$u "#FFFFFF"){
    
$this->et=$e;
        if(
$e=="edit"){
            
$str=$w;
            if(!
file_exists($str))
                
$this->error('El archivo '.$str.' no existe.');
                
$this->detecExt($str);
    switch(
$this->ext){
        case 
"JPG":
            
$this->ext="JPEG";
            
$this->imagen ImageCreateFromJPEG ($str);
                break;
        case 
"JPEG":
            
$this->ext="JPEG";
            
$this->imagen ImageCreateFromJPEG ($str);
                break;
        case 
"PNG":
            
$this->ext="PNG";
            
$this->imagen ImageCreateFromPNG ($str);
            
imageAlphaBlending($this->imagenfalse); 
            
imageSaveAlpha($this->imagentrue); 
                break;
        case 
"GIF":
            
$this->ext="GIF";
            
$this->imagenImageCreateFromGIF ($str);
                break;
        default:
            
$this->error("Extencion del archivo a editar no valida.");
                break;
        }
          
$this->srcw=@imagesx($this->imagen);
          
$this->srch=@imagesy($this->imagen);
        
$this->calidad=75;
        
$this->automatico(75);
    }elseif(
$e=="new"){
    
$this->width($w);
    
$this->height($h);
    
$this->image ImageCreateTrueColor($this->width,$this->height);
          list(
$c1,$c2,$c3)=$this->detecColor($u);
            
$c=@imagecolorallocate($this->image$c1,$c2,$c3);
             @
imagefill($this->image,0,0,$c);
        }
            }
    public function 
recortar($x1 0,$y1 0,$x2 0,$y2 0){
        
$this->x[1]=$x1;
        
$this->x[2]=$x2;
        
$this->y[1]=$y1;
        
$this->y[2]=$y2;
            }
    private function 
detecExt($str,$t false){
        if(!
$t){
            
$this->ext=ereg_replace(".*\.(.*)$","\\1",$str);
            
$this->ext=strtoupper($this->ext);
        }else{
            
$e=ereg_replace(".*\.(.*)$","\\1",$str);
            
$e=strtoupper($e);
                return 
$e;
        }
            }
    public function 
height($str=50){
    if(
$str<50)
        
$str=@floor($this->srch*$this->width/$this->srcw);
            
$this->height $str;
    }
    public function 
width($str=50)    {
    if(
$str<50)
        
$str=@floor($this->srcw*$this->height/$this->srch);
            
$this->width $str;
    }
    public function 
calidad($str=75){
        
$this->calidad=$str;
    }
    public function 
automatico($str=50)    {
    if (
$this->srcw<$this->srch) {
        
$this->height=$str;
            
$this->width=@floor($this->srcw*$this->height/$this->srch);
    }else{
    
$this->width=$str;
        
$this->height=@floor($this->srch*$this->width/$this->srcw);
    }
      if (
$this->width>$this->srcw && $this->height>$this->srch) {
    
$this->width=$this->srcw;
        
$this->height=$this->srch;
    }
    }
    public function 
text($t,$x=0,$y=0,$u="#000000",$f=NULL,$f2=20,$a=0){
    list(
$c1,$c2,$c3)=$this->detecColor($u);
        if(
$this->et=="edit")
            
$c=@imagecolorallocate($this->imagen,$c1,$c2,$c3);
        elseif(
$this->et=="new")
            
$c=@imagecolorallocate($this->image,$c1,$c2,$c3);
        if(
$this->et=="edit"){
            if(
$f==NULL)
                @
imagestring($this->imagen,$f2,$x,$y,$t,$c);
                    else
                        @
imagettftext($this->imagen$f2$a$x$y$c$f$t);
        }elseif(
$this->et=="new"){
            if(
$f==NULL)
                @
imagestring($this->image,$f2,$x,$y,$t,$c);
                    else
                        @
imagettftext($this->image$f2$a$x$y$c$f$t);
                            }
                                }
    public function 
borde($px 1,$u){
    
$px=($px-1);
        for(
$i=0;$i<=$px;$i++){
            
$this->linea(0,$i$this->width,$i,$u);
            
$this->linea($i,$this->height,$i,0,$u);
            
$this->linea(0,($this->height-$i)-1,$this->width,($this->height-$i)-1,$u);
            
$this->linea(($this->width-$i)-1,0, ($this->width-$i)-1$this->height$u);
        }
            }
    public function 
linea($x1,$y1,$x2,$y2,$u "#000000"){
    list(
$c1,$c2,$c3)=$this->detecColor($u);
        if(
$this->et=="edit"){
            
$c=@imagecolorallocate($this->imagen,$c1,$c2,$c3);
            @
imageline($this->imagen,$x1,$y1,$x2,$y2,$c);
        }elseif(
$this->et=="new"){
            
$c=@imagecolorallocate($this->image,$c1,$c2,$c3);
            @
imageline($this->image,$x1,$y1,$x2,$y2,$c);
        }
            }
    private function 
detecColor($u){
    if(
strchr($u,"#"))
                return @
sscanf($u,'#%2x%2x%2x');
            else
                return @
sscanf($u,'%2x%2x%2x');
    }
    private function 
error($t){
        
$this->error[]="<li>".$t."</li>";
    }
    public function 
errores(){
        if(!empty(
$this->error))
            return 
"Errores detectados:<ul>".implode("\n",$this->error)."</ul>";
    }
    public function 
crear($dir$fun NULL)    {
    if(
$this->et=="edit"){
        if(
$dir=="")
            return 
false;
            if(
$this->width=="" && $this->height=="")
                
$this->error('No se a selecionado un tamaño.');
                
$this->image = @ImageCreateTrueColor($this->width,$this->height);
                if(
$this->ext=="GIF"){
                    
$color=imagecolortransparent($this->imagen);
                        if(
$color!=-1){
                            
$trnprt_color = @imagecolorsforindex($this->imagen$color);
                             
$trnprt_indx = @imagecolorallocatealpha($this->image$trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue'], $trnprt_color['alpha']);
                             @
imagefill($this->image00$trnprt_indx);
                              @
imagecolortransparent($this->image$trnprt_indx);
                                        }
                                            }
                   @
imagecopyresampled ($this->image$this->imagen$this->x[1], $this->y[1], $this->x[2], $this->y[2],$this->width ,$this->height ,$this->srcw $this->srch);
    switch(
$this->detecExt($dir,true)){
        case 
"JPG":
            
$creat=@imageJPEG($this->image,$dir,$this->calidad);
                break;
        case 
"JPEG":
            
$creat=@imageJPEG($this->image,$dir,$this->calidad);
                break;
        case 
"PNG":
            
$creat=@imagePNG($this->image,$dir);
                break;
        case 
"GIF":
            
$creat=@imageGIF($this->image,$dir);
                break;
        default:
            
$this->error("Extencion del archivo a crear no valida.");
                break;
                }
    }elseif(
$this->et=="new"){
    
$this->detecExt($dir);
    switch(
$this->ext){
        case 
"JPG":
            
$creat=@imageJPEG($this->image,$dir,$this->calidad);
                break;
        case 
"JPEG":
        
$creat=    @imageJPEG($this->image,$dir,$this->calidad);
            break;
        case 
"PNG":
            
$creat=@imagePNG($this->image,$dir);
                break;
        case 
"GIF":
            
$creat=@imageGIF($this->image,$dir);
                break;
        default:
            
$this->error("Extencion del archivo a crear no valida.");
                break;
            }
                }
    if(
$creat)
        @
imagedestroy($this->image);
     else
        
$this->error("No se pudo crear el archivo \"".$dir."\", porque no se a podido abrir el archivo base.");
        if(
$fun=="ver" && $creat)
        echo 
"<img src=\"".$dir."\" width=\"".$this->width."\" height=\"".$this->height."\" />";
                    }
                        }
?>
Modo de uso:
Para edita una imagen:
Código PHP:
$t= new EasyThumbnail("edit",'logo.jpg'); 
Para crear una imagen:
Código PHP:
$t= new EasyThumbnail("new",width,height,color de fondo HTML); 
Para escribir, dar tamaño y calidad es igual.

Despues para ver los errores que se produsieron:
Código PHP:
echo $t->errores(); 
Crear borde:
Código PHP:
$t->borde(GROSOR,COLOR HTML); 
Para recortar (solo al editar):
Código PHP:
$t->recortar(X1,Y1,X2,Y2); 
Para crear una linea:
Código PHP:
$t->linea(X1,Y2,X2,Y2,COLOR HTML); 
y para crear:
Código PHP:
$t->crear(NOMBRE DE LA IMAGEN A CREAR); 
y para crear y verla:
Código PHP:
$t->crear(NOMBRE DE LA IMAGEN A CREAR,"ver"); 
Gracias
Salu2
__________________
Half Music - www.halfmusic.com

Última edición por pato12; 25/11/2008 a las 16:41 Razón: Saque el archivo BMP