各位大侠:
    问题如下,欢迎各位指点,讨论:    用户上传的不定高宽的相片如何才能在固定高宽的位置按正常比例显示?因为程序没法判断照片的内容的,所以不能武断的裁剪,但是如果不裁剪,网站中显示的相片的位置大小高宽是固定的,有没有相对较好的处理方法呢?    谢谢!!

解决方案 »

  1.   

    在HTML中输出的时候指定<img>的高宽。
      

  2.   

    通过程序处理后的相片是和原图的比例差不度,看起来也没有不协调,
    最关键的就是显示时<img> 设定了高宽 就会改变处理好的相片比例。
    比如:
    原图:500*718
    处理后:120*172
    <img>: 120*75这种情况下页面的显示比例也是不正常。
      

  3.   

    function imagefixed($image,$fixwidth,$fixheight){
        $picsize=getimagesize($image);
        $width=$picsize[0];   
        $height=$picsize[1];
        $return=array(0,0);
        if($width>0&&$height>0){
            if($width<=$fixwidth&&$height<=$fixheight){
                $return[0]=$width;
                $return[1]=$fixheight;                
            }else{
                $scalewidth=$width/$fixwidth;
                $scaleheight=$height/$fixheight; 
                if($scalewidth>=$scaleheight){
                    $return[0]= $fixwidth;
                    $return[1]=intval($height/$scalewidth); 
                }else{
                    $return[1]= $fixheight;
                    $return[0]=intval($width/$scaleheight);                 
                }            
            }        
        }
        return $return;
    }
    都设定高度宽度,上面的函数按比例获取高度,宽度;<img >里强制限定高度,宽度,不过最好还是用缩略图显示
      

  4.   

    最好的方法还是php生成缩小图
    可以用JavaScript<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>比较实用的利用javascript按比例相应缩放图片大小</title>
    <script language="JavaScript" type="text/javascript">
    <!--
    function DrawImage(ImgD,FitWidth,FitHeight){
       var image=new Image();
       image.src=ImgD.src;
       if(image.width>0 && image.height>0){
           if(image.width/image.height>= FitWidth/FitHeight){
               if(image.width>FitWidth){
                   ImgD.width=FitWidth;
                   ImgD.height=(image.height*FitWidth)/image.width;
               }else{
                   ImgD.width=image.width;
                   ImgD.height=image.height;
               }
           } else{
               if(image.height>FitHeight){
                   ImgD.height=FitHeight;
                   ImgD.width=(image.width*FitHeight)/image.height;
               }else{
                   ImgD.width=image.width;
                   ImgD.height=image.height;
               }
           }
       }
    }
    //-->
    </script>
    </head>
    <body>
    以下为原始图大小:
    <p>
      <img src="http://www.qwbm.com/ad/002.jpg" /></p>
    以下为缩小图
    <p><img src="http://www.qwbm.com/ad/002.jpg" onload="DrawImage(this,111,111)" border="0"></a>
    </p>
    以下为放大图(结果,放不大。。呵呵。。)
    <p><img src="http://www.qwbm.com/ad/002.jpg" onload="DrawImage(this,888,887)" border="0"></a>
    </p>
    </body>
    </html>
      

  5.   

    谢谢各位,我在Google Code里面找到了一个好用的类库,代码太长贴不上来,
    名字是ImageCrop,需要的可以去下载,注释如下:
    /**
     * Author : smallchicken
     * Time   : 2009年6月8日16:46:05
     * Last Time: 2010年5月5日 10:24:30
     * mode 1 : 强制裁剪,生成图片严格按照需要,不足放大,超过裁剪,图片始终铺满
     * mode 2 : 和1类似,但不足的时候 不放大 会产生补白,可以用png消除。
     * mode 3 : 只缩放,不裁剪,保留全部图片信息,会产生补白,
     * mode 4 : 只缩放,不裁剪,保留全部图片信息,此时的参数只是限制了生成的图片的最大宽高,不产生补白
     * mode 5 : 生成的图比例严格按照需要的比例,宽和高不超过给定的参数。
     * 默认补白为白色,如果要使补白成透明像素,请使用SaveAlpha()方法代替SaveImage()方法
     *
     * 调用方法:
     *
     * $ic=new ImageCrop('old.jpg','afterCrop.jpg');
     * $ic->Crop(120,80,2);
     * $ic->SaveImage();
     *        //$ic->SaveAlpha();将补白变成透明像素保存
     * $ic->destory();
     *
     *
     */