我实现了一图片缩放功能,可缩小后的图片很难看,请问怎么办?

解决方案 »

  1.   

    <?
    /**
     * 等比例调整图片大小
     * 
     * @param  $ 原始路径 $path_from
     * @param  $ 存放路径 $path_to
     * @param  $ 原有文件名 $name
     * @param  $ 新文件名 $outname
     * @param  $ 新宽度 $width
     * @param  $ 新高度 $height
     * @return string 新文件名
     */
    function resizeimg( $path_from, $path_to, $name , $outname, $width, $height ) {
        if ( !$path_from or !$path_to or !$name or !$outname or !$width or !$height ) {
            return false;
        } 
        // 新文件名
        $outputfile = $path_to . $outname;
        if ( is_file ( $outputfile ) ) {
            return false;
        } 
        // 指定jpg图片压缩率
        $compress = "90"; 
        // 水印开关
        $water = 0; //0 无水印 1 文字水印 2 图片水印 3 所有水印           
        // $waterposition=1;     //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
        $waterstring = ""; //水印字符串
        $font = 'simsun.ttc'; //文字字体
        $size = 10; //文字水印大小
        $waterimg = ""; //水印图片
        $alpha = 40;    $file_orig = $name; 
        // 获得原始文件名和分辨率
        $pic_url_orig = $path_from . $file_orig;
        list( $width_orig, $height_orig ) = getimagesize( $pic_url_orig ); 
        // 计算新分辨率
        if ( $width_orig > $width ) {
            $height = ( $width * $height_orig ) / $width_orig;
        } else {
            $width = $width_orig;
            $height = $height_orig;
        } 
        // 缩放
        $image_p = imagecreatetruecolor( $width, $height );
        $type = getimagesize ( $pic_url_orig );
        switch ( $type[2] ) {
            case 1:
                $simage = imagecreatefromgif( $pic_url_orig );
                break;
            case 2:
                $simage = imagecreatefromjpeg( $pic_url_orig );
                break;
            case 3:
                $simage = imagecreatefrompng( $pic_url_orig );
                break;
            case 6:
                $simage = imagecreatefromwbmp( $pic_url_orig );
                break;
            default:
                $simage = imagecreatefromjpeg( $pic_url_orig );
        } 
        imagecopyresampled( $image_p, $simage, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig );    if ( $water == 1 or $water == 3 ) {
            // 指定颜色
            $black = imagecolorallocate( $image_p, 0, 0, 0 );
            $grey = imagecolorallocate( $image_p, 192, 192, 192 );
            $white = imagecolorexactalpha ( $image_p, 255, 255, 255, 30 );
            $waterstring = $lang[waterstring];
            $waterimg = $lang[waterimg]; 
            // 填充背景色块 左上为原点 左上 至 右下
            imagefilledrectangle( $image_p, 4, $height - $size - 7, 4 + $size * strlen( $waterstring ) / 1.5 , $height - 3, $white ); 
            // 加水印字符串
            // 添加阴影 左上为原点 字符的基线左下角坐标
            // imagettftext( $image_p, $size, 0, 6, $height-4, $grey, $font, iconv("gb2312","utf-8", $waterstring ) );
            imagettftext( $image_p, $size, 0, 6, $height-4, $grey, $font, mb_convert_encoding( $waterstring, "UTF-8", "GB2312" ) );
            imagettftext( $image_p, $size, 0, 5, $height-5, $black, $font, mb_convert_encoding( $waterstring, "UTF-8", "GB2312" ) );
        } 
        if ( $water == 2 or $water == 3 ) {
            $typewater = getimagesize ( $waterimg ); 
            // 加水印图片
            switch ( $typewater[2] ) {
                case 1:
                    $simage1 = imagecreatefromgif( $waterimg );
                    break;
                case 2:
                    $simage1 = imagecreatefromjpeg( $waterimg );
                    break;
                case 3:
                    $simage1 = imagecreatefrompng( $waterimg );
                    break;
                case 6:
                    $simage1 = imagecreatefromwbmp( $waterimg );
                    break;
                default:
                    $simage1 = imagecreatefromjpeg( $waterimg );
            } 
            imagecopymerge( $image_p, $simage1, 0, 0, 0, 0, $typewater[0], $typewater[1] , $alpha );
            imagedestroy( $simage1 );
        } 
        // 输出
        switch ( $type[2] ) {
            case 1:
                imagegif( $image_p, $outputfile );
                break;
            case 2:
                imagejpeg( $image_p, $outputfile , $compress );
                break;
            case 3:
                imagepng( $image_p, $outputfile );
                break;
            case 6:
                imagewbmp( $image_p, $outputfile );
                break;
            default:
                imagejpeg( $image_p, $outputfile , $compress );
        } 
        return $outname;
    }
    $path=str_replace('test.php', '', str_replace('\\', '/', __FILE__));
    $aa=rand(1000,9999).".jpg";
    resizeimg($path."img/",$path,"d.jpg",$aa,105,80);
    ?>
      

  2.   

    <?php
    //==================================================== 
    // FileName:GDImage.inc.php 
    // Summary: 图片处理程序 
    // Author: ice_berg16(寻梦的稻草人) 
    // CreateTime: 2004-10-12 
    // LastModifed:2004-10-12 
    // copyright (c)2004 [email protected] 
    //==================================================== class GDImage 

    var $sourcePath; //图片存储路径 
    var $galleryPath; //图片缩略图存储路径 
    var $toFile = false; //是否生成文件 
    var $fontName; //使用的TTF字体名称 
    var $maxWidth = 500; //图片最大宽度 
    var $maxHeight = 600; //图片最大高度 //========================================== 
    // 函数: GDImage($sourcePath ,$galleryPath, $fontPath) 
    // 功能: constructor 
    // 参数: $sourcePath 图片源路径(包括最后一个"/") 
    // 参数: $galleryPath 生成图片的路径 
    // 参数: $fontPath 字体路径 
    //========================================== 
    function GDImage($sourcePath, $galleryPath, $fontPath) 

    $this->sourcePath = $sourcePath; 
    $this->galleryPath = $galleryPath; 
    $this->fontName = $fontPath . "4.ttf"; 
    } //========================================== 
    // 函数: makeThumb($sourFile,$width=128,$height=128) 
    // 功能: 生成缩略图(输出到浏览器) 
    // 参数: $sourFile 图片源文件 
    // 参数: $width 生成缩略图的宽度 
    // 参数: $height 生成缩略图的高度 
    // 返回: 0 失败 成功时返回生成的图片路径 
    //========================================== 
    function makeThumb($sourFile,$width=128,$height=128) 

    $imageInfo = $this->getInfo($sourFile); 
    $sourFile = $this->sourcePath . $sourFile; 
    $newName = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . "_thumb.jpg"; 
    switch ($imageInfo["type"]) 

    case 1: //gif 
    $img = imagecreatefromgif($sourFile); 
    break; 
    case 2: //jpg 
    $img = imagecreatefromjpeg($sourFile); 
    break; 
    case 3: //png 
    $img = imagecreatefrompng($sourFile); 
    break; 
    default: 
    return 0; 
    break; 

    if (!$img) 
    return 0; $width = ($width > $imageInfo["width"]) ? $imageInfo["width"] : $width; 
    $height = ($height > $imageInfo["height"]) ? $imageInfo["height"] : $height; 
    $srcW = $imageInfo["width"]; 
    $srcH = $imageInfo["height"]; 
    if ($srcW * $width > $srcH * $height) 
    $height = round($srcH * $width / $srcW); 
    else 
    $width = round($srcW * $height / $srcH); 
    //* 
    if (function_exists("imagecreatetruecolor")) //GD2.0.1 

    $new = imagecreatetruecolor($width, $height); 
    ImageCopyResampled($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]); 

    else 

    $new = imagecreate($width, $height); 
    ImageCopyResized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]); 

    //*/ 
    if ($this->toFile) 

    if (file_exists($this->galleryPath . $newName)) 
    unlink($this->galleryPath . $newName); 
    ImageJPEG($new, $this->galleryPath . $newName); 
    return $this->galleryPath . $newName; 

    else 

    ImageJPEG($new); 

    ImageDestroy($new); 
    ImageDestroy($img); } 
    //========================================== 
    // 函数: waterMark($sourFile, $text) 
    // 功能: 给图片加水印 
    // 参数: $sourFile 图片文件名 
    // 参数: $text 文本数组(包含二个字符串) 
    // 返回: 1 成功 成功时返回生成的图片路径 
    //========================================== 
    function waterMark($sourFile, $text) 

    $imageInfo = $this->getInfo($sourFile); 
    $sourFile = $this->sourcePath . $sourFile; 
    $newName = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . "_.jpg"; 
    switch ($imageInfo["type"]) 

    case 1: //gif 
    $img = imagecreatefromgif($sourFile); 
    break; 
    case 2: //jpg 
    $img = imagecreatefromjpeg($sourFile); 
    break; 
    case 3: //png 
    $img = imagecreatefrompng($sourFile); 
    break; 
    default: 
    return 0; 
    break; 

    if (!$img) 
    return 0; $width = ($this->maxWidth > $imageInfo["width"]) ? $imageInfo["width"] : $this->maxWidth; 
    $height = ($this->maxHeight > $imageInfo["height"]) ? $imageInfo["height"] : $this->maxHeight; 
    $srcW = $imageInfo["width"]; 
    $srcH = $imageInfo["height"]; 
    if ($srcW * $width > $srcH * $height) 
    $height = round($srcH * $width / $srcW); 
    else 
    $width = round($srcW * $height / $srcH); 
    //* 
    if (function_exists("imagecreatetruecolor")) //GD2.0.1 

    $new = imagecreatetruecolor($width, $height); 
    ImageCopyResampled($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]); 

    else 

    $new = imagecreate($width, $height); 
    ImageCopyResized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]); 

    $white = imageColorAllocate($new, 255, 255, 255); 
    $black = imageColorAllocate($new, 0, 0, 0); 
    $alpha = imageColorAllocateAlpha($new, 230, 230, 230, 40); 
    //$rectW = max(strlen($text[0]),strlen($text[1]))*7; 
    ImageFilledRectangle($new, 0, $height-26, $width, $height, $alpha); 
    ImageFilledRectangle($new, 13, $height-20, 15, $height-7, $black); 
    ImageTTFText($new, 6.8, 0, 20, $height-14, $black, $this->fontName, $text[0]); 
    ImageTTFText($new, 6.8, 0, 20, $height-6, $black, $this->fontName, $text[1]); 
    //*/ 
    if ($this->toFile) 

    if (file_exists($this->galleryPath . $newName)) 
    unlink($this->galleryPath . $newName); 
    ImageJPEG($new, $this->galleryPath . $newName); 
    return $this->galleryPath . $newName; 

    else 

    ImageJPEG($new); 

    ImageDestroy($new); 
    ImageDestroy($img); } 
    //========================================== 
    // 函数: displayThumb($file) 
    // 功能: 显示指定图片的缩略图 
    // 参数: $file 文件名 
    // 返回: 0 图片不存在 
    //========================================== 
    function displayThumb($file) 

    $thumbName = substr($file, 0, strrpos($file, ".")) . "_thumb.jpg"; 
    $file = $this->galleryPath . $thumbName; 
    if (!file_exists($file)) 
    return 0; 
    $html = "<img src='$file' >"; 
    echo $html; 

    function insertimg($file){
    $thumbName = substr($file, 0, strrpos($file, ".")) . "_thumb.jpg"; 
    $file = $this->galleryPath . $thumbName; 
    if (!file_exists($file)) 
    return 0; 
    return $file; 

    }
    //========================================== 
    // 函数: displayMark($file) 
    // 功能: 显示指定图片的水印图 
    // 参数: $file 文件名 
    // 返回: 0 图片不存在 
    //========================================== 
    function displayMark($file) 

    $Name = substr($file, 0, strrpos($file, ".")) . "_.jpg"; 
    $file = $this->galleryPath . $Name; 
    if (!file_exists($file)) 
    return 0; 
    $html = "<img src='$file' >"; 
    echo $html; 

    //========================================== 
    // 函数: getInfo($file) 
    // 功能: 返回图像信息 
    // 参数: $file 文件路径 
    // 返回: 图片信息数组 
    //========================================== 
    function getInfo($file) 

    $file = $this->sourcePath . $file; 
    $data = getimagesize($file); 
    $imageInfo["width"] = $data[0]; 
    $imageInfo["height"]= $data[1]; 
    $imageInfo["type"] = $data[2]; 
    $imageInfo["name"] = basename($file); 
    return $imageInfo; 
    } } ?> 
      

  3.   

    可能是你选用的GD库的函数问题如果是高版本的GD,可以使用