Ver Mensaje Individual
  #2 (permalink)  
Antiguo 30/06/2009, 15:59
easy
 
Fecha de Ingreso: diciembre-2008
Mensajes: 454
Antigüedad: 17 años, 10 meses
Puntos: 2
Respuesta: Tratamiento de Imagenes, como hacer que se vea de dos maneras

Aqui esta GDImageManipulation.class.php:
Código PHP:
Class GDImageManipulation
{
    
/*
     * Variable declarations
     */

    
private $image_file         = ""; // Filename and path for disc-input
    
private $image_object       = ""; // Input image object
    
private $image_width        = ""; // Input image width
    
private $image_height       = ""; // Input image height

    /*
     * Function declaration
     */

    
public function set_image_asFile($filename)
    {
        
$this->image_file = $filename;
        
$handle = fopen($filename, "r");
        
$this->image_object = imagecreatefromstring(fread($handle, filesize($filename)));
        
fclose($handle);

        
$this->image_width = imagesx($this->image_object);
        
$this->image_height = imagesy($this->image_object);
        return 
true;
    } 
// function set_image_file

  
    
public function set_image_asString($string)
    {
        
$this->image_object = imagecreatefromstring($string);
        
$this->image_width = imagesx($this->image_object);
        
$this->image_height = imagesy($this->image_object);
        return 
true;    
    } 
// function set_image_string


    
public function set_image_asObject($object)
    {
        
$this->image_object = $object;
        
$this->image_width = imagesx($this->image_object);
        
$this->image_height = imagesy($this->image_object);
        return 
true;                
    } 
// function set_image_string   

    
    
public function get_png()
    {
        if (
$this->image_object)
        {
            return 
imagepng($this->image_object);
        } else {
        return 
false;
        }
    } 
// function get_png


    
public function get_gd2()
    {
        if (
$this->image_object)
        {
            return 
imagegd2($this->image_object);
        } else {
        return 
false;
        }
    } 
// function get_gd2


    
public function get_gd()
    {
        if (
$this->image_object)
        {
            return 
imagegd($this->image_object);
        } else {
        return 
false;
        }
    } 
// function get_gd


    
public function get_gif()
    {
        if (
$this->image_object)
        {
            return 
imagegif($this->image_object);
        } else {
        return 
false;
        }
    } 
// function get_gif


    
public function get_jpeg()
    {
        if (
$this->image_object)
        {
            return 
imagejpeg($this->image_object);
        } else {
        return 
false;
        }
    } 
// function get_jpg


    
public function get_inlineTag_png($alt)
    {
        if (
$this->image_object)
        {
            
ob_start();
            
imagepng($this->image_object);
            
$raw = ob_get_clean();
            
ob_end_clean();
            
$data = chunk_split(base64_encode($raw));
            
//$data = str_replace(array('+','/','='),array('-','_',),$data);
            
$tag  = '<img src="data:image/png;base64,'.$data.'"';
            
$tag .= ' width="'.imagesx($this->image_object).'"';
            
$tag .= ' height="'.imagesy($this->image_object).'"';
            
$tag .= ' alt="'.$alt.'" />';
            
            return 
$tag;
        } else {
        return 
false;
        }
    }


    public function 
get_inlineTag_gif($alt)
    {
        if (
$this->image_object)
        {
            
ob_start();
            
imagegif($this->image_object);
            
$raw = ob_get_clean();
            
ob_end_clean();
            
$data = chunk_split(base64_encode($raw));
            
//$data = str_replace(array('+','/','='),array('-','_',),$data);
            
$tag  = '<img src="data:image/gif;base64,'.$data.'"';
            
$tag .= ' width="'.imagesx($this->image_object).'"';
            
$tag .= ' height="'.imagesy($this->image_object).'"';
            
$tag .= ' alt="'.$alt.'" />';
            
            return 
$tag;
        } else {
        return 
false;
        }
    }

    public function 
get_inlineTag_jpeg($alt)
    {
        if (
$this->image_object)
        {
            
ob_start();
            
imagejpeg($this->image_object);
            
$raw = ob_get_clean();
            
ob_end_clean();
            
$data = chunk_split(base64_encode($raw));
            
//$data = str_replace(array('+','/','='),array('-','_',),$data);
            
$tag  = '<img src="data:image/jpeg;base64,'.$data.'"';
            
$tag .= ' width="'.imagesx($this->image_object).'"';
            
$tag .= ' height="'.imagesy($this->image_object).'"';
            
$tag .= ' alt="'.$alt.'" />';
            
            return 
$tag;
        } else {
        return 
false;
        }
    }

    public function 
fit_width($width, $enlarge)
    {
        if ( 
$this->image_width > $width or ($enlarge and $this->image_width < $width))
        {
            
$nwidth = $width;
            
$nheight = intval(($width / $this->image_width) * $this->image_height); 

            
$scaledImage = ImageCreateTrueColor($nwidth, $nheight);
            
ImageCopyResampled($scaledImage, $this->image_object, 0, 0, 0, 0, 
                               
$nwidth, $nheight, $this->image_width, $this->image_height);

            
$this->set_image_asObject($scaledImage);
        }
        return 
true;
    } 
// function fit_width


    
public function fit_height($height, $enlarge)
    {
        if ( 
$this->image_height > $height or ($enlarge and $this->image_height < $height))
        {
            
$nheight = $height;
            
$nwidth = intval(($height / $this->image_height) * $this->image_width); 

            
$scaledImage = ImageCreateTrueColor($nwidth, $nheight);
            
ImageCopyResampled($scaledImage, $this->image_object, 0, 0, 0, 0, 
                               
$nwidth, $nheight, $this->image_width, $this->image_height);
        
            
$this->set_image_asObject($scaledImage);
        }
        return 
true;    
    } 
// function fit_height


    
public function resize($factor)
    {
        
$nheight = intval($this->image_height * $factor);
        
$nwidth  = intval($this->image_width * $factor);
        
$scaledImage = ImageCreateTrueColor($nwidth, $nheight);
        
ImageCopyResampled($scaledImage, $this->image_object, 0, 0, 0, 0, 
                           
$nwidth, $nheight, $this->image_width, $this->image_height);
        
$this->set_image_asObject($scaledImage);
        return 
true;
    } 
// function resize


    
public function flip_vertical()
    {
        
$dest = imagecreatetruecolor(imagesx($this->image_object), imagesy($this->image_object));
        
ImageCopyResampled($dest, $this->image_object, 0, 0, 0, 
                            (
imagesy($dest)-1), imagesx($dest), imagesy($dest), imagesx($dest), 
                            (
0-imagesy($dest)));
        
$this->set_image_asObject($dest);
        return 
true;
    }


    public function 
flip_horizontal()
    {
        
$dest = imagecreatetruecolor(imagesx($this->image_object), imagesy($this->image_object));
        
ImageCopyResampled($dest, $this->image_object, 0, 0, 
                            (
imagesx($dest)-1), 0, imagesx($dest), 
                            
imagesy($dest), (0-imagesx($dest)), imagesy($dest));
        
$this->set_image_asObject($dest);
        return 
true;
    }


    public function 
rotate($angle, $r=255, $g=255, $b=255, $ignore_transparent=-1)
    {
        if (
$this->image_object) {
            
$bgcolor = imagecolorallocate($this->image_object, $r, $g, $b);
            
$this->image_object = imagerotate($this->image_object, $angle, $bgcolor, $ignoretransparent);
            
$bgcolor = imagecolorallocate($this->image_object, $r, $g, $b);
            return 
true;
        } else {
            return 
false;
        }
    
    }


    public function 
convert_sepia($dither = true)
    {
        
// Credits: derek at idreams dot co dot uk
        // Source: http://de.php.net/manual/en/function.imagecolorsforindex.php#79511       

        
if (!($t = imagecolorstotal($this->image_object))) {
            
$t = 256;
            
imagetruecolortopalette($this->image_object, $dither, $t);
        }

        
$total = imagecolorstotal( $this->image_object );

        for ( 
$i = 0; $i < $total; $i++ ) {
            
$index = imagecolorsforindex( $this->image_object, $i );
                
$red = ( $index["red"] * 0.393 + $index["green"] * 0.769 + $index["blue"] * 0.189 );
                
$green = ( $index["red"] * 0.349 + $index["green"] * 0.686 + $index["blue"] * 0.168 ); 
                
$blue = ( $index["red"] * 0.272 + $index["green"] * 0.534 + $index["blue"] * 0.131 ); 

        if (
$red > 255) { $red = 255; }
        if (
$green > 255) { $green = 255; }
        if (
$blue > 255) { $blue = 255; }
        
imagecolorset( $this->image_object, $i, $red, $green, $blue );
        }    
    }

//continua en la siguiente cita