php 图片等比例处理图片的时候有个问题一直不明白  就是 原图片有个宽高比 目标图片也有一个宽高比 为什么 原图的宽高比 大于目标图片的宽高比 要高度优先
$ratio   =   ($this->width)/($this->height); //实际图象的比例
$resize_ratio   =   ($this->resize_width)/($this->resize_height);//改变后的图象的比例  
if($ratio>$resize_ratio){
高度优先 //就是不明白 这里为什么是高度优先
}else{
宽度优先 //不明白为什么是宽度优先
} private $width;//实际宽度  
private $height;//实际高度  
private $resize_width;//改变后的宽度    
private $resize_height;//改变后的高度  

解决方案 »

  1.   

    举个例子来说明一下,假设原图是 50px /50px的一张图片。 则:
    $ratio = ($this->width)/($this->height);  // 值为1
    $resize_ratio = ($this->resize_width)/($this->resize_height); 
    若果 高度拉伸 了。$resize_ratio<1  = $ratio  就是高度优先。反之则宽度优先。
      

  2.   

    若果 高度拉伸 了。$resize_ratio<1 = $ratio 就是高度优先。反之则宽度优先。
     为什么高度拉伸了 就要高度优先
      

  3.   

    <?   
      /***************************************/   
      /*功     能:利用PHP的GD库生成高质量的缩略图*/   
      /*运行环境:PHP5.01/GD2*/   
      /*类说明:可以选择是/否裁图。   
        如果裁图则生成的图的尺寸与您输入的一样。   
        原则:尽可能多保持原图完整   
        如果不裁图,则按照原图比例生成新图   
        原则:根据比例以输入的长或者宽为基准*/   
      /*参   数:$img:源图片地址   
        wid:新图的宽度   
        $hei:新图的高度   
        $c:是否裁图,1为是,0为否*/   
        
      /***************************************/   
    class Image{
    private $type;//图片类型   
    private $width;//实际宽度  
    private $height;//实际高度  
    private $resize_width;//改变后的宽度    
    private $resize_height;//改变后的高度  
    private $cut;//是否裁图 
    private $srcimg;//源图象     
    private $dstimg; //目标图象地址 
    private $im;//临时建的图象 
    private static $_handle ;
    function resizeimage($img,   $wid,   $hei,$c){
    $this->srcimg = $img;   
    $this->resize_width = $wid;   
    $this->resize_height = $hei;   
    $this->cut = $c;   
    $this->type = substr(strrchr($this->srcimg,"."),1);//图片的类型     
    $this->initi_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);//改变后的图象的比例   
    $ratio   =   ($this->width)/($this->height); //实际图象的比例   
    if(($this->cut)=="1"){ //裁图      
    if($ratio>=$resize_ratio){   //高度优先   
    $newimg   =   imagecreatetruecolor($this->resize_width,$this->resize_height);   
    imagecopyresampled($newimg,   $this->im,   0,   0,   0,   0,   $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->im,   0,   0,   0,   0,   $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->im,   0,   0,   0,   0,   $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->im,   0,   0,   0,   0,   ($this->resize_height)*$ratio,   $this->resize_height,   $this->width,   $this->height);   
    ImageJpeg   ($newimg,$this->dstimg);   
    }   
    }   
    }   
    //初始化图象   
    function   initi_img()   {   
    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);   
    }   
    }
    /**
     * Enter description here...
     *
     * @return Image
     */
    public static function getInstance(){
    if(is_object(self::$_handle)==false){
    self::$_handle = new self();
    }
    return self::$_handle;
    }
    }   
      ?>
      

  4.   

    吃饱喝足了
    你看看这个测试例$url = 'http://avatar.profile.csdn.net/9/D/6/2_dream1206.jpg';
    $fn = 'dream1206.jpg';
    $s = file_get_contents($url);
    file_put_contents($fn, $s);//缓存在本地,一是为了防止网络问题造成图片错误,二是程序也只接受本地文件list($w, $h) = getimagesize($fn);//获取原始图片的宽和高$code = file_get_contents('images.php'); //因为后面要修改程序代码,所以要把你的类定义读进来if(1) //改变一下条件看看 1 强制高度优先
      $code = str_replace(array('$ratio>=$resize_ratio', '$ratio<$resize_ratio'), array(1, 0), $code);
    else //0 强制宽度优先
      $code = str_replace(array('$ratio>=$resize_ratio', '$ratio<$resize_ratio'), array(0, 1), $code);eval("?>$code"); //加载被修改后的类$p = new image;
    $p->resizeimage($fn, $h, $w, 1); //注意是“裁图”方式分别用 1 和 0 运行一下,就能理解了
      

  5.   

    本帖最后由 xuzuning 于 2012-04-07 15:10:20 编辑
      

  6.   

    ����JFIF��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ��C      $.' ",#(7),01444'9=82<.342��C   2!!22222222222222222222222222222222222222222222222222��dE"��  ���}!1AQa"q2亼�#B绷R佯$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz儎厗噲墛挀敃枟槞殺¥ウЖ┆渤吹斗腹郝媚牌侨墒矣哉肿刭卺忏溴骁栝犟蝮趱鲼���  ���w!1AQaq"2�B憽绷 #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz們剠唶垑姃摂晼棙櫄ⅲぅΗī炒刀犯购旅呐魄壬室釉罩棕仝忏溴骁栝牝篝貊鼬�� ?膼誑l绉j跉葬F:璕氂溿祡��*s覜�晑﹩�爷淟铉m8�嵉q 悭+A荍9萬['礍阺拉W躲洲認HF~拄y訳s ;�-  聋礔{7=创x��>柛酕k萖ǔH艤!硱�'覚鯲偆-啓c佺尦QxO/��蕣煯裄m\�袨茷l镐脎米曓痳)梈G�娬bb萾鋛撔�Yq僝r囟 �d旊V,r焋c佝TWL鯡Hv��+3觘尔� V{l灂吼嗃襞B仲< 鶗Z钎飕a媌CV��!NJ况a�锍c�貶j6匚� (U虯%烐�n磈^#阳�躙!�鞸Y隁n�雓祋岉閵�I崒X-][ {樌d*侣焬H`}*A(鵻AG睕.慺屠镋h�*=瑡鍙b猸?N贷B�漪T�鶺)﹟Hf弉焔禦>蠓�XW驌窊佖誝nI 廉i脀s'Y�x踋柤灻<~禃w鈃^鋺%衄鹒壁\�&爄 0悃J踪S韇}�$�%矺V3牖[vsLw丶qNN+DN鏏g痡鶽到�/韦徠汈�棠瀯�?,�-鲎鑈鈺舃�RrG�頥適菁r�?�;�骐p1EG堡W;哉駯臘.襛X醅噯@豐簺鰗
      

  7.   

    高度优先或宽度优先都会有图片超出的情况,正确的算法应该是计算出 height1/height2 width1/width2 两个比例,取比值小的那个就可以绝对保证不会超出。