如题.图片尺寸长大于800px时.如何调用GD2函数.将图片按比例缩小.并且可以被HTML<img>调用?

解决方案 »

  1.   

    //缩放图片
    function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100)
    {
       
     
    list($width, $height, $type, $attr) = getimagesize($srcImage);
    switch ($type) {
    case 1: $img = imagecreatefromgif($srcImage); break;
    case 2: $img = imagecreatefromjpeg($srcImage); break;
    case 3: $img = imagecreatefrompng($srcImage); break;
    }
    $scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例

    if($scale < 1) {
    $newWidth = floor($scale*$width);
    $newHeight = floor($scale*$height);
    $newImg = imagecreatetruecolor($newWidth, $newHeight);
    imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
    $newName = "";
    switch($type) {
    case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality))
    return "$newName.gif"; break;
    case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
    return "$newName.jpg"; break;
    case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality))
    return "$newName.png"; break;
    default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
    return "$newName.jpg"; break;
    }
    imagedestroy($newImg);
    }
    imagedestroy($img);
    return false;
    }
    //直接压制指定大小,不进行等比处理
    function resize_2($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100)
    {
     
    list($width, $height, $type, $attr) = getimagesize($srcImage);
    switch ($type) {
    case 1: $img = imagecreatefromgif($srcImage); break;
    case 2: $img = imagecreatefromjpeg($srcImage); break;
    case 3: $img = imagecreatefrompng($srcImage); break;
    }
    $scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例


    $newWidth =  $maxWidth;
    $newHeight = $maxHeight;
    $newImg = imagecreatetruecolor($newWidth, $newHeight);
    imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
    $newName = "";
    switch($type) {
    case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality))
    return "$newName.gif"; break;
    case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
    return "$newName.jpg"; break;
    case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality))
    return "$newName.png"; break;
    default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
    return "$newName.jpg"; break;
            imagedestroy($newImg);
    }
    imagedestroy($img);
    return false;
    }//裁剪图片
    function cutImg($o_file,$x1,$x2,$y1,$y2){
    //设置文件参数
    //$o_file="./uploadfiles/200902/13/1234515154_QcA.jpg";//原文件文件路径
      $file=basename($o_file);//文件
    $ext=end(explode(".", $file));//扩展名
    $filename=basename($file,$ext);//文件名 $filelen=strlen($file);
    $path=substr($o_file,0,strlen(($o_file))-$filelen);//文件夹 $newfile=$path."edit_".$file;//新文件名
    $newthufile=$path."thumb_".$file;//新文件名 header('Content-type: image/jpeg');
    list($width, $height) = getimagesize($o_file); $new_width = $x2-$x1;
    $new_height = $y2-$y1;

    $image_n = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($o_file); imagecopyresampled($image_n, $image, 0, 0, $x1, $y1, $new_width, $new_height, $new_width, $new_height);
    //输出文件
    imagejpeg($image_n, $newfile, 100);
    $newfileName = $newfile;
    return $newfile;
    }利用上面的函数将指定大小的图片按指定路径生成出来。再进行调用。
      

  2.   

    http://hi.baidu.com/bit5566/blog/item/80083502742486054afb51ea.htmlhttp://www.google.com.hk/search?hl=zh-CN&newwindow=1&safe=strict&client=aff-cs-360se&hs=CxX&q=php+%E7%BC%A9%E6%94%BE%E5%9B%BE%E7%89%87%E5%87%BD%E6%95%B0&meta=&aq=f&aqi=&aql=&oq=&gs_rfai=
      

  3.   


    queryphp框架自带无损缩放,无压扁剪切(就是缩放到指定大小,而图像不会被压扁或拉伸了),可以定动剪切头像。http://code.google.com/p/queryphp/downloads/list