<?php
    
class imageTransform {
    var $filename;
    var $ct;
    var $src;
    var $dst;
    var $w;
    var $h;
    var $type = 'jpeg';
    var $ct_to_type = array(
            'image/jpeg' => 'jpeg',
            'image/gif' => 'gif',
            'image/png' => 'png'
        );
    
    var $output_format = 'jpeg';
    
    
    function __construct($filename, $ct = "" ) {
        $this->filename = $filename;
        if (!$ct) $ct = mime_content_type($this->filename);
        if (!$ct ) $ct = 'image/jpeg';
        $this->ct = $ct;
        $this->type = $this->ct_to_type[$ct];

        $this->loadImage();
    }
    
    function loadImage() {
        $this->src = $this->imagecreatefrom($this->filename);
        $this->w = imageSX($this->src);
        $this->h = imageSY($this->src);
    }
    
    function imagecreatefrom($filename) {
       $fname = 'imagecreatefrom'.$this->type;
       return $fname($filename);
    }
    
    function resize($w = 0, $h = 0, $zoomout = true) {
        
        if (!$w ) {
            $k = $this->h/$h;
            $w = $this->w/$k;
        }

        if (!$h ) {
            $k = $this->w/$w;
            $h = $this->h/$k;
        }
        

        if (!$this->dst ) {
    	     $this->create($w, $h);
        }
        
        $src_k = $this->w/$this->h;
        $dst_k = $w/$h;
        if ($src_k == $dst_k ) {
            // Если пропорция совпадает, то просто зум   
            $this->zoom($w, $h);
        }
        // Изменяем соотношение сторон, приводим src к dst
        
        if ( $src_k < 1 ) { // Если source вертикальный
            if ( $dst_k > 1 ) { 
                if ($zoomout) { // из | в _
                    $this->resize_w2h($w,$h); 
            } else {
                    $this->resize_w2w($w,$h); 
                }
            } else {
                if ($zoomout ) {
                    $this->resize_w2w($w,$h); // из _ в |   
                } else {
                    $this->resize_w2h($w,$h); 
                }
            }
        } else {
            if ( $dst_k > 1 ) { 
                if ($zoomout ) {
                $this->resize_w2w($w,$h); // из _ в |   
            } else {
                    $this->resize_w2h($w,$h); 
            }
            } else {
                if ($zoomout) { // из | в _
                    $this->resize_w2h($w,$h); 
                } else {
                    $this->resize_w2w($w,$h); 
                }            }
        }
    }
    
    function resize_w2h($w, $h) {
         $k = $this->w / $w;
         $new_h = $this->h/$k;
         $margin_top = ($h-$new_h)/2;
		 imagecopyresampled($this->dst,$this->src,0,$margin_top,0,0,$w,$new_h,$this->w,$this->h); 
    }
    
    function resize_w2w($w, $h) {
         $k = $this->h / $h;
         $new_w = ($this->w/$k);
         $margin_left = ($w - $new_w)/2;
		 imagecopyresampled($this->dst, $this->src, $margin_left, 0, 0, 0, $new_w, $h, $this->w, $this->h); 
    }

    function zoom($w, $h) {
		 imagecopyresampled($this->dst,$this->src,0,0,0,0,$w,$h,$this->w,$this->h); 
    }
    
    function custom($x, $y, $w, $h ) {
           
    }
    
    function create($w = 0, $h = 0) {
    	 if (!$w) $w = $this->w;
         if (!$h) $h = $this->h;
    	 $this->dst = ImageCreateTrueColor($w, $h);
	     if (in_array($this->type, array('png','gif') ) ) {
			 imagesavealpha($this->dst, true);
			 $transparent = imagecolorallocatealpha($this->dst, 255, 255, 255, 127);
		 } else {
			 $transparent = imagecolorallocate($this->dst, 255, 255, 255);
         }
		 imagefill($this->dst, 0, 0, $transparent);
    }
    
    function save($filename) {
        $this->image($this->dst, $filename);
        ImageDestroy($this->dst);
        ImageDestroy($this->src);
    }
    
    function image($link, $filename = '') {
       $fname = 'image'.$this->output_format;
       if ($this->output_format == 'jpeg' ) {
           $fname($link, $filename, 90);
       } else {
           $fname($link, $filename);
       }
    }
    
    
    function addWM($logo, $center = false) {
        if (!$this->dst) {
            $this->create();
            imagecopy($this->dst, $this->src, 0,0,0,0,$this->w,$this->h );
        }
        
        $logoImage = ImageCreateFromPNG($logo);   
        imageAlphaBlending($logoImage, false);
        imageSaveAlpha($logoImage, true);
        $trcolor = ImageColorAllocate($logoImage, 255, 255, 255);
        ImageColorTransparent($logoImage , $trcolor);
        
        if ($center ) {
            $x = imageSX($this->dst)/2- imageSX($logoImage)/2;
            $y = imageSY($this->dst)/2 - imageSY($logoImage)/2;
        } else {
            $x = rand(0, imageSX($this->dst)- imageSX($logoImage) );
            $y = imageSY($this->dst) - ( imageSY($logoImage) * 1.2 );
        }
        imagecopy($this->dst, $logoImage, $x, $y, 0, 0, imageSX($logoImage), imageSY($logoImage));
        ImageDestroy($logoImage);
    }
    
    function flush() {
        Header('Content-Type: ' .  array_search($this->output_format, $this->ct_to_type) );
        
        $this->image($this->dst);
        die();
    }
       
}    
    
?>