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); 
        }    

    ?>
  • 相关阅读:
    Nginx平滑升级
    svn部署-linux
    svn服务备份与还原
    vmware exsi安装部署
    redis主从复制读写分离
    redis配置文件详解
    zabbix与agent端通信加密
    部署owa预览服务
    zabbix-3.4邮件报警
    centos7--zabbix3.4微信报警
  • 原文地址:https://www.cnblogs.com/Athrun/p/1786153.html
Copyright © 2011-2022 走看看