Ver Mensaje Individual
  #11 (permalink)  
Antiguo 10/09/2003, 00:09
Avatar de davidMS
davidMS
 
Fecha de Ingreso: abril-2003
Ubicación: Chepe
Mensajes: 202
Antigüedad: 21 años
Puntos: 0
esta es la clase:

Código PHP:
<?PHP


require("hft_image_errors.php"); // para editar los errores del lenguajes

    
    
class hft_image {
        var 
$image_original;
        var 
$file_original;
        var 
$image_original_width;
        var 
$image_original_height;
        var 
$image_original_type_code;
        var 
$image_original_type_abbr;
        var 
$image_original_html_sizes;

        var 
$image_resized;
        var 
$file_resized;
        var 
$image_resized_width;
        var 
$image_resized_height;
        var 
$image_resized_type_code;
        var 
$image_resized_type_abbr;
        var 
$image_resized_html_sizes;

        
//some settings
        
var $jpeg_quality;
        var 
$use_gd2;

        
        function 
hft_image($file_original){
            
//constructor of the class
            //it takes given file and creates image out of it
            
global $ERR;
            
$this->clear(); // clear all.

            
if(file_exists($file_original))    {
                
$this->file_original $file_original;
                
$this->image_original $this->imagecreatefromfile($file_original);
                if(!
$this->image_original){
                    
$this->error($ERR["IMAGE_NOT_CREATED_FROM_FILE"]." file=$file_original"); 
                    return 
false;
                }


            } else {
                
$this->error($ERR["FILE_DOESNOT_EXSIT"]." file=$file_original");
            }
        }

        function 
clear() {
            

                
$this->image_original            =    0;
                
$this->file_original            =    "";
                
$this->image_original_width        =    0;
                
$this->image_original_height    =    0;
                
$this->image_original_type_code    =    0;
                
$this->image_original_type_abbr    =    "";
                
$this->image_original_html_sizes=    "";

                
$this->image_resized            =    0;
                
$this->file_resized                =    "";
                
$this->image_resized_width        =    0;
                
$this->image_resized_height        =    0;
                
$this->image_resized_type_code    =    -1;
                
$this->image_resized_type_abbr    =    "";
                
$this->image_resized_html_sizes    =    "";

                
$this->set_parameters();

        }

        function 
set_parameters($jpeg_quality="85"$use_gd2=true) {

            
$this->jpeg_quality=$jpeg_quality;
            
$this->use_gd2=$use_gd2;
        }

        function 
error($msg){
            
            echo(
"<hr color='red'><font color='red'><b>$msg</b></font><br> file=<b>".__FILE__."</b><hr color='red'>");
        }
    

        function 
imagecreatefromfile($img_file){
            global 
$ERR;
            
$img=0;
            
$img_sz =  getimagesize$img_file );     ## returns array with some properties like dimensions and type;
            ####### Now create original image from uploaded file. Be carefull! GIF is often not supported, as far as I remember from GD 1.6
            
switch( $img_sz[2] ){
                case 
1:
                    
$img $this->_imagecheckandcreate("ImageCreateFromGif"$img_file);
                    
$img_type "GIF";
                break;
                case 
2
                    
$img $this->_imagecheckandcreate("ImageCreateFromJpeg"$img_file);
                    
$img_type "JPG";
                break;
                case 
3
                    
$img $this->_imagecheckandcreate("ImageCreateFromPng"$img_file);
                    
$img_type "PNG";
                break;
                
// would be nice if this function will be finally supported
                
case 4
                    
$img $this->_imagecheckandcreate("ImageCreateFromSwf"$img_file);
                    
$img_type "SWF";
                break;
                default:
                    
$img 0;
                    
$img_type "UNKNOWN";
                    
$this->error($ERR["IMG_NOT_SUPPORTED"]." $img_file");
                    break;
                
            }
//case
        
            
if($img){
                
$this->image_original_width=$img_sz[0];
                
$this->image_original_height=$img_sz[1];
                
$this->image_original_type_code=$img_sz[2];
                
$this->image_original_type_abbr=$img_type;
                
$this->image_original_html_sizes=$img_sz[3];

            }else {
                
$this->clear();

            }

            return 
$img;        
        }


        function 
_imagecheckandcreate($function$img_file) {
            global 
$ERR;
            if(
function_exists($function)) {
                
$img $function($img_file); 
            }else{
                
$img false;
                
$this->error($ERR["FUNCTION_DOESNOT_EXIST"]." ".$function);
            }

            return 
$img;
            
        }

        function 
resize($desired_width$desired_height$mode="-"){
            global 
$ERR;
            if(
$desired_width == "*" && $desired_height == "*"){
                
$this->image_resized $this->image_original;
                Return 
true;
            }
            switch(
$mode) {
                case 
"-":
                case 
'+':
                    if(
$desired_width != "*"$mult_x $desired_width $this->image_original_width;        
                    if(
$desired_height != "*"$mult_y $desired_height $this->image_original_height;                
                    
$ratio $this->image_original_width $this->image_original_height;

                    if(
$desired_width == "*"){
                        
$new_height $desired_height;
                        
$new_width $ratio $desired_height;
                    }elseif(
$desired_height == "*"){
                        
$new_height $desired_width $ratio;
                        
$new_width =  $desired_width;

                    }else{
                        if(
$mode=="-"){
                                if( 
$this->image_original_height $mult_x $desired_height ){
                                
                                    
$new_width $desired_width;
                                    
$new_height $this->image_original_height $mult_x;
                                }else{
                                    
                                    
$new_width $this->image_original_width $mult_y;
                                    
$new_height $desired_height;
                                }

                        }else{
                            
                            if( 
$this->image_original_height $mult_x $desired_height ){
                                
                                
$new_width $desired_width;
                                
$new_height $this->image_original_height $mult_x;
                            }else{
                                
                                
$new_width $this->image_original_width $mult_y;
                                
$new_height $desired_height;
                            }

                        }
                    }
                break;

                case 
'0':
                    
                    if(
$desired_width == "*"$desired_width $this->image_original_width;        
                    if(
$desired_height == "*"$desired_height $this->image_original_height;    
                    
$new_width $desired_width;
                    
$new_height $desired_height;
                    
                break;
                default: 
                    
$this->error($ERR["UNKNOWN_RESIZE_MODE"]."  $mode");
                break;
            }
            
            
            if( 
$this->use_gd2 ){
                if( 
function_exists("imagecreatetruecolor")){
                
                    
$this->image_resized imagecreatetruecolor($new_width$new_height) or $this->error($ERR["GD2_NOT_CREATED"]);
                }else {
                    
$this->error($ERR["GD2_UNAVALABLE"]." ImageCreateTruecolor()");
                }
            } else {
            
            
                
$this->image_resized imagecreate($new_width$new_height) or $this->error($ERR["IMG_NOT_CREATED"]);
            }

            
//Resize
            
if( $this->use_gd2 ){

                if( 
function_exists("imagecopyresampled")){
                    
$res imagecopyresampled($this->image_resized
                                              
$this->image_original
                                              
00,  //dest coord
                                              
00,            //source coord
                                              
$new_width$new_height//dest sizes
                                              
$this->image_original_width$this->image_original_height // src sizes
                                            
) or $this->error($ERR["GD2_NOT_RESIZED"]); 

                }else {
                    
$this->error($ERR["GD2_UNAVALABLE"]." ImageCopyResampled()");
                }
            } else {
            
                
$res imagecopyresized($this->image_resized
                                          
$this->image_original
                                          
00,  //dest coord
                                          
00,            //source coord
                                          
$new_width$new_height//dest sizes
                                          
$this->image_original_width$this->image_original_height // src sizes
                                        
) or $this->error($ERR["IMG_NOT_RESIZED"]); 
            }

        }
        
        function 
output_original($destination_file$image_type="JPG") { 
          
            return 
_output_image($destination_file$image_type$this->image_original); 
        } 
        
        function 
output_resized($destination_file$image_type="JPG") { 
          
            
$res $this->_output_image($destination_file$image_type$this->image_resized); 
            if(
trim($destination_file)){ 
                
$sz=getimagesize($destination_file); 
                
$this->file_resized $destination_file
                
$this->image_resized_width $sz[0]; 
                
$this->image_resized_height $sz[1]; 
                
$this->image_resized_type_code=$sz[2]; 
                
$this->image_resized_html_sizes=$sz[3]; 
                  
                switch(
$this->image_resized_html_sizes){ 
                    case 
0
                        
$this->image_resized_type_abbr "GIF"
                    break; 
                    case 
1
                        
$this->image_resized_type_abbr "JPG"
                    break; 
                    case 
2
                        
$this->image_resized_type_abbr "PNG"
                    break; 
                    case 
3
                        
$this->image_resized_type_abbr "SWF"
                    break; 
                    default: 
                        
$this->image_resized_type_abbr "UNKNOWN"
                    break; 
                } 
                 
            } 
            return 
$res
        } 

        function 
_output_image($destination_file$image_type$image){ 
          
            global 
$ERR
            
$destination_file trim($destination_file); 
            
$res false
            if(
$image){ 
                switch(
$image_type) { 
                    case 
'JPEG'
                    case 
'JPG'
                     
                        
$res ImageJpeg($image$destination_file$this->jpeg_quality); 
                    break; 
                    case 
'PNG'
                        
$res Imagepng($image$destination_file); 
                    break; 
                    default: 
                        
$this->error($ERR["UNKNOWN_OUTPUT_FORMAT"]." $image_type"); 
                    break; 
             
                } 
            }else{ 
                
$this->error($ERR["NO_IMAGE_FOR_OUTPUT"]); 
            } 
            if(!
$res$this->error($ERR["UNABLE_TO_OUTPUT"]." $destination_file"); 
            return 
$res
        } 



?>