<?php
class Image{
    private 
$src;
    private 
$mime;
    private 
$outputType;
    private 
$img;
    private 
$width;
    private 
$height;
    
    private 
$quality 100;
    
    public function 
Image($src){
        
$this->loadImage($src);
    }
    
    public function 
loadImage($src){
        
// Creating image from src
        
$src_img false;
        if (
file_exists($src)){
            
$t getimagesize($src);
            
$this->mime $t["mime"];
            
$this->outputType $t["mime"];
            
$this->width $t[0];
            
$this->height $t[1];
            
$this->src $src;
            
            switch(
$this->mime) {
                case 
"image/jpeg":
                    
$src_img imagecreatefromjpeg($src);
                    break;
                case 
"image/gif":
                    
$src_img imagecreatefromgif($src);    
                    break;
                case 
"image/png":
                    
$src_img imagecreatefrompng($src);                    
                    
imagealphablending$src_imgfalse );
                    
imagesavealpha$src_imgtrue );
                    break;
                default:
                    throw new 
Exception("Formato de imágen no válido ".$this->mime);
                    break;
            }        
        }
        else{
            throw new 
Exception("El archivo fuente no existe ".$src);
        }
        
        if (!
$src_img){
            throw new 
Exception("Error al generar la imágen ".$src);
        }
        else{
            
$this->img $src_img;
        }
    }
    
    public function 
resize($width ""$height ""){
        
// Resize image
        
        
if (!$this->img) throw Exception("You need to load the image First");
        
$x $this->width;
        
$y $this->height;
        if (
$y>$height && $height != ""){
            
$ratio $y $height;
            
$y $height;
            
$x $x/$ratio;
        }
        if (
$x>$width && $width != ""){
            
$ratio $x $width;
            
$x $width;
            
$y $y/$ratio;
        }
        
$x floor($x);
        
$y floor($y);
        
        
$dst_img = @imagecreatetruecolor($x$y);

        
imagealphablending$dst_imgfalse );
        
imagesavealpha$dst_imgtrue );

        if(!
$dst_img) {
            
$dst_img imagecreate($x$y);
        }
        if (!
$dst_img){
            throw new 
Exception("Error al reescalar la imagen");
        }
        
        
# gif transparency
        
if ($this->outputType == "image/gif"){
            
$transindex imagecolortransparent($this->img);
            if(
$transindex >= 0) {
                
$transcol imagecolorsforindex($this->img$transindex);
                
$transindex imagecolorallocatealpha($dst_img$transcol['red'], $transcol['green'], $transcol['blue'], 127);
                
imagefill($dst_img00$transindex);
            }
        }
        
imagecopyresampled($dst_img$this->img0000$x$y$this->width$this->height);
        
        
# restore transparency
        
if ($this->outputType == "image/gif"){
            
imagecolortransparent($dst_img$transindex);
            for(
$_y=0$_y<$y; ++$_y)
                for(
$_x=0$_x<$x; ++$_x)
                    if(((
imagecolorat($dst_img$_x$_y)>>24) & 0x7F) >= 100imagesetpixel($dst_img$_x$_y$transindex);
        }
        
        
imagedestroy($this->img);
        
$this->img $dst_img;
        
$this->width $x;
        
$this->height $y;
    }
    
    
// Sets the jpg quality
    
public function setQuality($q){
        
$this->quality $q;
    }
    
    
// Updates the output type, only mime types are allowed (image/jpeg, image/gif, image/png)
    
public function setOutput($type){
        
$this->outputType $type;
    }
    
    
// Saves the image into filesystem
    
public function saveImage($saveFile){
        if(
is_writable(dirname($saveFile))){
            switch (
$this->outputType){
                case 
"image/jpeg":
                    
imagejpeg($this->img$saveFile$this->quality);
                    break;
                case 
"image/gif":
                    
imagegif($this->img$saveFile);
                    break;
                case 
"image/png":
                    
imagepng($this->img$saveFile);
                    break;
            }
        }
        else{
            throw new 
Exception("Imposible abrir ".$saveFile." para exritura");
        }
    }
    
    
// Prints the image
    
public function printImage(){
        switch (
$this->outputType){
            case 
"image/jpeg":
                
header("Content-type: ".$this->mime);
                
imagejpeg($this->imgNULL$this->quality);
                break;
            case 
"image/gif":
                
header("Content-type: ".$this->mime);
                
imagegif($this->img);
                break;
            case 
"image/png":
                
header("Content-type: ".$this->mime);
                
imagepng($this->img);
                break;
            default:
                throw new 
Exception("Formato de imágen de salida no válido ".$this->outputType);
                break;
        }
    }
    
    public function 
setWaterMark($waterSrc$left$top$right ""$bottom ""){
        
$wtr imagecreatefrompng($waterSrc);
        
$metrics getimagesize($waterSrc);
        
        
imagealphablending$this->imgtrue );

        if (!
$wtr){
            throw new 
Exception("Error al reescalar la imagen");
        }
        
        if (
$right != ""$left $this->width $metrics[0] - $right;
        if (
$bottom != ""$top $this->height $metrics[1] - $bottom;
        
        
imagecopy($this->img$wtr$left$top00$metrics[0], $metrics[1]);
    }
}
?>