登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

linlove的小屋

无论你有多喜欢对方,爱情里的主动必须是男人。如果这个男人不主动,我宁愿错过。。。

 
 
 

日志

 
 

PHP缩略图类,可生成BMP格式  

2011-03-02 11:39:30|  分类: php函数库 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
<?
class 
resizeimage  
 
   
//图片类型  
   
var $type
 
   
//实际宽度  
   
var $width
 
   
//实际高度  
   
var $height
 
   
//改变后的宽度  
   
var $resize_width
 
   
//改变后的高度  
   
var $resize_height
 
   
//是否裁图  
   
var $cut
 
   
//源图象  
   
var $srcimg
 
   
//目标图象地址  
   
var $dstimg
 
   
//临时建的图象  
   
var $im
 
   
//生成的文件名后缀 
   
var $extstr
; 

   function 
resizeimage($img$wid$hei,$extstr,$c=0
 
   {  
       
$this->srcimg $img
 
       
$this->resize_width $wid
 
       
$this->resize_height $hei
 
       
$this->cut $c
 
       
$this->extstr $extstr
; 
       
//图片的类型  
       
$this->type substr(strrchr($this->srcimg,"."),1
);  
       
//初始化图象  
       
$this->initi_img
();  
       
//目标图象地址  
       
$this -> dst_img
();  
       
$this->width = @imagesx($this->im
);  
       
$this->height = @imagesy($this->im
);  
       
//生成图象  
       
$this->newimg
();  
       @
ImageDestroy ($this->im
);  
   }  
   function 
newimg
()  
   {  
       
//改变后的图象的比例  
       
$resize_ratio = ($this->resize_width)/($this->resize_height
);  
       
//实际图象的比例  
       
if($this->height>0
) 
       
$ratio = ($this->width)/($this->height
);  
       if((
$this->cut)=="1"
 
       
//裁图  
       
 
           if(
$ratio>=$resize_ratio
 
           
//高度优先  
           
 
               
$newimg = @imagecreatetruecolor($this->resize_width,$this->resize_height
);  
               @
imagecopyresampled($newimg$this->im0000$this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height
);  
               @
ImageJpeg ($newimg,$this->dstimg
);  
           }  
           if(
$ratio<$resize_ratio
 
           
//宽度优先  
           
 
               
$newimg = @imagecreatetruecolor($this->resize_width,$this->resize_height
);  
               @
imagecopyresampled($newimg$this->im0000$this->resize_width$this->resize_height$this->width, (($this->width)/$resize_ratio
));  
               @
ImageJpeg ($newimg,$this->dstimg
);  
           }  
       }  
       else  
       
//不裁图  
       
 
           if(
$ratio>=$resize_ratio
 
           {  
               
$newimg = @imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio
);  
               @
imagecopyresampled($newimg$this->im0000$this->resize_width, ($this->resize_width)/$ratio$this->width$this->height
);  
               
ImageJpeg ($newimg,$this->dstimg
);  
           }  
           if(
$ratio<$resize_ratio
 
           {  
               
$newimg = @imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height
);  
               @
imagecopyresampled($newimg$this->im0000, ($this->resize_height)*$ratio$this->resize_height$this->width$this->height
);  
               @
ImageJpeg ($newimg,$this->dstimg
);  
           }  
       }  
   }  
   
//初始化图象  
   
function initi_img
()  
   {  
          
$type=strtolower($this->type);
//转换成小写,否则不写扩展名生成不了。 
        
       
if($type=="jpg" || $type=="jpeg" || $type=="jpe"
 
       {  
           
$this->im = @imagecreatefromjpeg($this->srcimg
);  
       }  
       if(
$type=="gif"
 
       {  
           
$this->im = @imagecreatefromgif($this->srcimg
);  
       }  
       if(
$type=="png"
 
       {  
           
$this->im = @imagecreatefrompng($this->srcimg
);  
       } 
       if(
$type=="bmp"
) 
       { 
           
$this->im $this->imagecreatefrombmp($this->srcimg
); 
       } 
   }  
    
    
    function 
imagecreatefrombmp($p_sFile
){ 
        
$file fopen($p_sFile,"rb"
); 
        
$read fread($file,10
); 
        while(!
feof($file)&&($read<>""
)) 
        
$read .= fread($file,1024
); 
        
$temp unpack("H*",$read
); 
        
$hex =  $temp[1
]; 
        
$header substr($hex,0,108
); 
        if (
substr($header,0,4)=="424d"
){ 
            
$header_parts str_split($header,2
); 
            
$width hexdec($header_parts[19].$header_parts[18
]); 
            
$height hexdec($header_parts[23].$header_parts[22
]); 
            unset(
$header_parts
); 
        } 
        
$x 0
; 
        
$y 1
; 
        
$image imagecreatetruecolor($width,$height
); 
        
$body substr($hex,108
); 
        
$body_size = (strlen($body)/2
); 
        
$header_size = ($width*$height
); 
        
$usePadding = ($body_size>($header_size*3)+4
); 
        for (
$i=0;$i<$body_size;$i+=3
){ 
            if (
$x>=$width
){ 
                if (
$usePadding
) 
                
$i    +=    $width%4
; 
                
$x    =    0
; 
                
$y
++; 
                if (
$y>$height
) 
                break; 
            } 
            
$i_pos    =    $i*2
; 
            
$r        =    hexdec($body[$i_pos+4].$body[$i_pos+5
]); 
            
$g        =    hexdec($body[$i_pos+2].$body[$i_pos+3
]); 
            
$b        =    hexdec($body[$i_pos].$body[$i_pos+1
]); 
            
$color    =    imagecolorallocate($image,$r,$g,$b
); 
            
imagesetpixel($image,$x,$height-$y,$color
); 
            
$x
++; 
        } 
        unset(
$body
); 
        return 
$image
; 
    } 
                
    
    
   
//图象目标地址  
   
function dst_img
()  
   {  
       
$full_length  strlen($this->srcimg
);  
       
$type_length  strlen($this->type
);  
       
$name_length  $full_length-$type_length
 
       
$name         substr($this->srcimg,0,$name_length-1
);  
       
$this->dstimg $name.$this->extstr.'.'.$this->type
 
   } 
    
   static function 
get_url($img,$extstr
){ 
       
$imgs explode('.',$img
); 
       
$ext end($imgs
); 
       
$full_length  strlen($img
);  
       
$type_length  strlen($ext
);  
       
$name_length  $full_length-$type_length
 
       
$name         substr($img,0,$name_length-1
);  
       return 
$name.$extstr.'.'.$ext
 
   } 

?>
  评论这张
 
阅读(591)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018