zoukankan      html  css  js  c++  java
  • PHP图片操作类

    1,生成缩图

    代码
    <?php
    ///生成縮圖
    //
    /$resizeimage=new resizeimage($filesaved,$dstimg,250,250);
    class resizeimage 

        
    var $type
        
    var $width
        
    var $height
        
    var $resize_width
        
    var $resize_height
        
    var $srcimg
        
    var $dstimg
        
    var $im;
        
    var $desiredRatio;

        
    function resizeimage($srcimg,$dstimg, $wid, $hei
        { 
            
    $this->srcimg = $srcimg
            
    $this->dstimg = $dstimg
            
    //圖片的類型 
            $this->type = substr(strrchr($this->srcimg,"."),1); 
            
    //初始化圖像 
            if($this->type=="jpg"
                
    $this->im = imagecreatefromjpeg($this->srcimg); 
            
    if($this->type=="gif"
                
    $this->im = imagecreatefromgif($this->srcimg); 
            
    if($this->type=="png"
                
    $this->im = imagecreatefrompng($this->srcimg);
                
            
    //目標圖像寬和高
            $this->width = imagesx($this->im); 
            
    $this->height = imagesy($this->im);
            
    //計算縮圖的寬和高
            //若需要生產的縮圖的寬和高都大於源圖的寬高時都不需要縮小

            if( ($this->width<=$wid&& ($this->height<=$hei) ){
                
    $this->resize_width=$this->width;
                
    $this->resize_height=$this->height;
            }
    else{
                
    //若寬的縮小比例小於高的縮小比例時,則成生的縮圖的寬為$wid
                if( ($wid/$this->width) < ($hei/$this->height) ){
                    
    $this->desiredRatio=$wid/$this->width;
                    
    $this->resize_width=$wid;
                    
    $this->resize_height=(int)($this->height*$this->desiredRatio);
                
    //若寬的縮小比例大於高的縮小比例時,則生成的縮圖的高為$hei
                }else{
                    
    $this->desiredRatio=$hei/$this->height;
                    
    $this->resize_height=$hei;
                    
    $this->resize_width=(int)($this->width*$this->desiredRatio);
                }
            }
            
    //生成圖像
            $this->newimg(); 
            
    //销毁一图像
            ImageDestroy ($this->im); 
        } 
        
    function newimg() 
        { 
            
    $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); 
            imagecopyresampled(
    $newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, $this->height) ; 
            ImageJpeg (
    $newimg,$this->dstimg); 
        }    

    ?>
  • 相关阅读:
    linux 总结一下git reset的各个选项吧
    深入C++的new
    Android中binderDied()以及"Unknown binder error code" 出现的原因说明
    C/C++语言void及void指针深层探索
    Android 不通过USB数据线调试的方法
    Android Browser Gallery3D无法两指手势缩放
    Android eMMC Booting
    android基础知识13:AndroidManifest.xml文件解析
    Package sunjava6jdk is not available Ubuntu 10.04 LTS 安装sunjava6jdk ,出现错误
    SQL Server 2008中Service Broker基础应用(上)
  • 原文地址:https://www.cnblogs.com/Athrun/p/1786153.html
Copyright © 2011-2022 走看看