解决方案 »

  1.   


    斑竹英明,就是用了GD函数了,做了等比缩放这些操作,您能帮忙指点指点吗?代码如下
    function resizeImg($from,$w=100,$h=100,$newfile){
      $info = getimagesize($from);
      switch ($info[2]){
      case 1:
      $im = imagecreatefromgif($from);
     break;
     case 2:
      $im = imagecreatefromjpeg($from);
      break;
     case 3:
      $im = imagecreatefrompng($from);
      break;
     default:
        //exit('不支持的图像格式');
     break;
     }
     $temp = pathinfo($from);
     $name = $temp["basename"];//文件名
     $dir = $temp["dirname"];//文件所在的文件夹
     $extension = $temp["extension"];//文件扩展名
     $width = $info[0];//获取图片宽度
     $height = $info[1];//获取图片高度
     $per1 = round($width/$height,2);//计算原图长宽比
     $per2 = round($w/$h,2);//计算缩略图长宽比
     //计算缩放比例
     if($per1>$per2||$per1==$per2)
     {
     //原图长宽比大于或者等于缩略图长宽比,则按照宽度优先
     $per=$w/$width;
     }
     if($per1<$per2)
     {
     //原图长宽比小于缩略图长宽比,则按照高度优先
     $per=$h/$height;
     }
     $temp_w = intval($width*$per);//计算原图缩放后的宽度
     $temp_h = intval($height*$per);//计算原图缩放后的高度
     $dst_im = imagecreatetruecolor($temp_w, $temp_h);
     //调整大小
     imagecopyresized($dst_im, $im, 0, 0, 0, 0, $temp_w, $temp_h, $width, $height);
     //输出缩小后的图像
     //exit($newfile);
     imagejpeg($dst_im,$newfile,100);
     imagedestroy($dst_im);
     imagedestroy($im);
    }还有一段代码如下$file = $_FILES["photo"];

    $filename=$file["tmp_name"]; 
    $size=$file['size']; 
    $type=$file['type'];
        $max_size=5*1024*1024;
    //echo $size;

    if($type!="image/jpg"&&$type!="image/jpeg"&&$type!="image/png"&&$type!="image/gif")
    {
       echo"<script>alert('不支持的图像格式')</script>";
    exit;
    }
     
    if($size>$max_size)
    {
       echo"<script>alert('图片尺寸超出上传限制')</script>";
    exit;
    }
     
    $udid=guid();

    $target_file="../photos/".$udid.".jpg";
    copy($filename,$target_file);

    /*
    $info=getimagesize($target_file);
        print_r($info);
    */ resizeImg($target_file,$nw,$nh,$target_file);

    $ifn = $target_file;   //-----------原图像----
    $ofn = $target_file;  //---剪切后保存的图像名----
    $ext = strtoupper(end(explode('.',$ifn))); 
    if(is_file($ifn) && ($ext == "JPG" || $ext == "JPEG")){ 
    $source = imagecreatefromjpeg($ifn); 
    }elseif(is_file($ifn) && $ext == "PNG"){ 
    $source = imagecreatefromPNG($ifn); 
    }elseif(is_file($ifn) && $ext == "GIF"){ 
    $source = imagecreatefromGIF($ifn); 

    $sourceWidth  = imagesx($source); 
    $sourceHeight = imagesy($source); 
    $thumbWidth = $_POST['w']; 
    $thumbHeight = $_POST['h']; 
    $thumb = imagecreatetruecolor($thumbWidth, $thumbHeight); 
    $x1 = $_POST['x1']; 
    $y1 = $_POST['y1']; 
    $x2 = $_POST['x2']; 
    $y2 = $_POST['y2']; 
    imagecopyresampled($thumb, $source,0,0,$x1,$y1,$thumbWidth,$thumbHeight,$thumbWidth,$thumbHeight); 
    imagejpeg($thumb, $ofn);

    resizeImg($target_file,100,100,$target_file);
    您能帮忙看看问题在哪吗?因为对GD函数不熟悉,希望您能指导指导,谢谢
      

  2.   

    尝试启用抗锯齿功能(imageantialias)不过你对位图做了缩放,丢失精度是必然的