用php生成縮略圖很簡單,不過質量很差,請問高手有什麼方法來解決這麼問題嗎?有代碼說明更好

解决方案 »

  1.   

    PHP100 视频有专门讲解生成縮略圖的 , 去学习一下就知道了
      

  2.   

    <?php
    /*---------------------------------------
     * Code Sundj.
     * --------------------------------------
     * $image = new image();
     * $image->init('./image.jpg');
     * $image->cut('./1.jpg', 120, 120, 1);
     * $image->cut('./2.jpg', 120, 120, 2);
     * $image->cut('./3.jpg', 120, 120, 3);
     * $image->cut('./4.jpg', 120, 120, 4);
     *  dimensions
     ----------------------------------------*/
    class image
    {
    private $import;
    private $export;
    private $create;

    private $quality = 100;
    private $ext;

    private $image;
    private $image_info = array();

    private $thumb;
    private $thumb_info = array();

    private $ratio = array();

    private $e = array();

    public function init($source)
    {
    if(!function_exists('gd_info'))

    define('GD2ERROR', true);
    $this->e[] = 'GD lib error';
    return false;
    }

    $info = getimagesize($source);
    $this->image_info['x'] = 0;
    $this->image_info['y'] = 0;
    $this->image_info['w'] = $info[0];
    $this->image_info['h'] = $info[1]; switch($info[2]) //info[2] MIME code
    {
    case IMAGETYPE_JPEG:
    $this->import = 'imagecreatefromjpeg';
    $this->export = 'imagejpeg';
    $this->quality = 100;
    $this->ext = '.jpg';
    break;
    case IMAGETYPE_GIF:
    $this->import = 'imagecreatefromgif';
    $this->export = 'imagegif';
    $this->quality = 100;
    $this->ext = '.gif';
    break;
    case IMAGETYPE_PNG:
    $this->import = 'imagecreatefrompng';
    $this->export = 'imagepng';
    $this->quality = 9;
    $this->ext = '.png';
    break;
    default:
    $this->e[] = 'Image mime unaccept';
    return false;
    }
    $this->create = function_exists('imagecreatetruecolor') ? 'imagecreatetruecolor' : 'imagecreate'; $import = $this->import;
       $this->image = $import($source);
    }

    public function cut($new, $w = 16, $h = 16, $mode = 1)
    {
            $this->ratio['thumb'] = $w / $h;
            $this->ratio['image'] = $this->image_info['w'] / $this->image_info['h'];
            $this->ratio['width'] = $w / $this->image_info['w'];
            $this->ratio['height'] = $h / $this->image_info['h'];
            
            $export = $this->export;
            
    switch($mode)
    {
    case 2:
    $this->transform_2($w, $h);
    break;
    case 3:
    $this->transform_3($w, $h);
    break;
    case 4:
    $this->transform_4($w, $h);
    break;
    default:
    $this->transform_1($w, $h);
    break;
    }        imagefill($this->thumb, 0, 0, imagecolorallocate($this->thumb, 255, 255, 255));
    imagecopyresized($this->thumb, $this->image, $this->thumb_info['x'], $this->thumb_info['y'], $this->image_info['x'], $this->image_info['y'], $this->thumb_info['w'], $this->thumb_info['h'], $this->image_info['w'], $this->image_info['h']);

    $export($this->thumb, $new, $this->quality);
    }

    private function transform_1($w, $h)
    {
    if($this->ratio['width'] > 1 && $this->ratio['height'] > 1)
    {
    $this->thumb_info['w'] = $this->image_info['w'];
    $this->thumb_info['h'] = $this->image_info['h'];
    }
    elseif($this->ratio['width'] > 1 && $this->ratio['height'] <= 1)
    {
    $this->thumb_info['w'] = $this->image_info['w'];
    $this->thumb_info['h'] = $this->image_info['h'];
    }
    elseif($this->ratio['width'] <= 1 && $this->ratio['height'] > 1)
    {
    $this->thumb_info['w'] = $this->image_info['w'];
    $this->thumb_info['h'] = $this->image_info['h'];
    }
    elseif($this->ratio['width'] <= 1 && $this->ratio['height'] <= 1)
    {
    if($this->ratio['image'] > 1)
    {
    $this->thumb_info['w'] = $h * $this->ratio['image'];
    $this->thumb_info['h'] = $h;
    }
    else
    {
    $this->thumb_info['w'] = $w;
    $this->thumb_info['h'] = $w / $this->ratio['image'];
    }
    }
    else
    {
    //
    }

    $this->thumb_info['x'] = ($w - $this->thumb_info['w']) / 2;
    $this->thumb_info['y'] = ($h - $this->thumb_info['h']) / 2;
    $this->image_info['x'] = 0;
    $this->image_info['y'] = 0;

    $create = $this->create;
    $this->thumb = $create($w, $h);
    }

    private function transform_2($w, $h)
    {
    if($w > $this->image_info['w'] && $h > $this->image_info['h'])
    {
    $w = $this->image_info['w'];
    $h = $this->image_info['h'];
    }
    elseif($w <= $this->image_info['w'] && $h <= $this->image_info['h'])
    {
    if($this->ratio['width'] <= $this->ratio['height'])
    {
    $w = $this->image_info['w'] * $this->ratio['width'];
    $h = $this->image_info['h'] * $this->ratio['width'];
    }
    else
    {
    $w = $this->image_info['w'] * $this->ratio['height'];
    $h = $this->image_info['h'] * $this->ratio['height'];
    }
    }
    elseif($w <= $this->image_info['w'] && $h > $this->image_info['h'])
    {
    $w = $this->image_info['w'] * $this->ratio['width'];
    $h = $this->image_info['h'] * $this->ratio['width'];
    }
    elseif($w > $this->image_info['w'] && $h <= $this->image_info['h'])
    {
    $w = $this->image_info['w'] * $this->ratio['height'];
    $h = $this->image_info['h'] * $this->ratio['height'];
    }
    else
    {
    //
    }
        
        $this->thumb_info['w'] = $w;
        $this->thumb_info['h'] = $h;
        
    $this->thumb_info['x'] = 0;
    $this->thumb_info['y'] = 0;
    $this->image_info['x'] = 0;
    $this->image_info['y'] = 0;

    $create = $this->create;
    $this->thumb = $create($w, $h);
    }

    private function transform_3($w, $h)
    {
    if($this->ratio['width'] > 1 && $this->ratio['height'] > 1)
    {
    $this->thumb_info['w'] = $this->image_info['w'];
    $this->thumb_info['h'] = $this->image_info['h'];
    }
    elseif($this->ratio['width'] > 1 && $this->ratio['height'] <= 1)
    {
    $this->thumb_info['w'] = $h * $this->ratio['image'];
    $this->thumb_info['h'] = $h;
    }
    elseif($this->ratio['width'] <= 1 && $this->ratio['height'] > 1)
    {
    $this->thumb_info['w'] = $w;
    $this->thumb_info['h'] = $w / $this->ratio['image'];
    }
    elseif($this->ratio['width'] <= 1 && $this->ratio['height'] <= 1)
    {
    if($this->ratio['thumb'] > $this->ratio['image'])
    {
    $this->thumb_info['w'] = $h * $this->ratio['image'];
    $this->thumb_info['h'] = $h;
    }
    else
    {
    $this->thumb_info['w'] = $w;
    $this->thumb_info['h'] = $w / $this->ratio['image'];
    }
    }
    else
    {
    //
    }

    $this->thumb_info['x'] = ($w - $this->thumb_info['w']) / 2;
    $this->thumb_info['y'] = ($h - $this->thumb_info['h']) / 2;
    $this->image_info['x'] = 0;
    $this->image_info['y'] = 0; $create = $this->create;
    $this->thumb = $create($w, $h);
    }

    private function transform_4($w, $h)
    {
        $this->thumb_info['w'] = $w;
        $this->thumb_info['h'] = $h;
        
    if($this->ratio['image'] > $this->ratio['thumb'])
    {
    $this->image_info['x'] = ($this->image_info['w'] - $this->image_info['h'] * $this->ratio['thumb']) / 2;
    $this->image_info['y'] = 0;
    $this->image_info['w'] = $this->image_info['h'] * $this->ratio['thumb'];
    }
    else
    {
    $this->image_info['x'] = 0;
    $this->image_info['y'] = ($this->image_info['h'] - $this->image_info['w'] / $this->ratio['thumb']) / 2;
    $this->image_info['h'] = $this->image_info['w'] / $this->ratio['thumb'];
    }

    $this->thumb_info['x'] = 0;
    $this->thumb_info['y'] = 0;
    $create = $this->create;
    $this->thumb = $create($w, $h);
    }
    }
    ?>
      

  3.   

    $image->init($source);
    $image->cut($thumb, $w, $h, $mode);
      

  4.   

    网上现成的,没你这个这个麻烦吧:
    /*
    $source_img = "../base_img/index.jpg";
    $target_dir='../base_img/';
    $new_width=500;
    $new_height=500;
    $if_cut=0;
    $target_name='HanroadClass';
    $img=new HanroadClass;
    $img->HrResize($source_img,$target_dir,$target_name,$new_width,$new_height,$if_cut); 
    */
    class HanroadClass 

        /**//********************** 
        利用PHP的GD库生成缩略图。 
        支持图片格式:jpg,gif,png
        $source_img:  源图象完整路径
        $target_dir:  目标图象目录
        $target_name: 目标图象名称
        $new_width:   目标图象宽
        $new_height:  目标图象高
        $if_cut:      是否裁图
            1(裁图): 裁图则按设置的大小生成目标图象
            0(不裁): 不裁则按比例生成目标图象
        **********************/ 
        function HrResize($source_img,$target_dir,$target_name,$new_width,$new_height,$if_cut) { 
            //图片类型 
            $img_type = strtolower(substr(strrchr($source_img,"."),1)); 
     
            //图象的完整目标路径 
            $tar_url = $target_dir."/".$target_name.".".$img_type; 
     
            //初始化图象 
            if($img_type=="jpg")$temp_img = imagecreatefromjpeg($source_img); 
            if($img_type=="gif")$temp_img = imagecreatefromgif($source_img);
            if($img_type=="png")$temp_img = imagecreatefrompng($source_img);

     
            //原始图象的宽和高 
            $old_width  = imagesx($temp_img); 
            $old_height = imagesy($temp_img); 
     
            //改变前后的图象的比例 
            $new_ratio = $new_width/$new_height; 
            $old_ratio = $old_width/$old_height; 
     
            //生成新图象的参数 
            //情况一:裁图 则按设置的大小生成目标图象 
            if($if_cut=="1"){ 
                $new_width  = $new_width; 
                $new_height = $new_height; 
                //高度优先 
                if($old_ratio>=$new_ratio){ 
                    $old_width  = $old_height*$new_ratio; 
                    $old_height = $old_height; 
                }else{//宽度优先 
                    $old_width  = $old_width; 
                    $old_height = $old_width/$new_ratio; 
                } 
            }else{//情况二:不裁图 则按比例生成目标图象 
                $old_width  = $old_width; 
                $old_height = $old_height; 
                //高度优先 
                if($old_ratio>=$new_ratio) { 
                    $new_width  = $new_width; 
                    $new_height = $new_width/$old_ratio; 
                }else{//宽度优先 
                    $new_width  = $new_height*$old_ratio; 
                    $new_height = $new_height; 
                } 
            } 
            //生成新图片 
    $new_img = imagecreatetruecolor($new_width,$new_height);
            if($img_type=="gif"){
    imagealphablending($new_img, false);
    imagesavealpha($new_img, true);
    }
    if($img_type=="png"){
    imagesavealpha($temp_img,true);
        imagealphablending($new_img,false);
          imagesavealpha($new_img,true);
    }
           imagecopyresampled($new_img,$temp_img,0,0,0,0,$new_width,$new_height,$old_width,$old_height);

            if($img_type=="jpg")imagejpeg($new_img,$tar_url);
            if($img_type=="gif"){
    $bgcolor=imagecolorallocate($new_img,0,0,0);
    $bgcolor=imagecolortransparent($new_img,$bgcolor);
    $bgcolor=imagecolorallocatealpha($new_img, 0, 0, 0,127);
    imagefill($new_img, 0, 0, $bgcolor);
    imagegif($new_img,$tar_url);
    }
            if($img_type=="png")imagepng($new_img,$tar_url);
    imagedestroy($source_img);
    imagedestroy($new_img);
        } 

      

  5.   


    <?php 
    require_once("../../config/class.GD.Zoom.php");
    $file=New MakeMiniature();
    $file->setParam("../../".$tmp_pic,"../../".$tmp_zoom_pic);
    $file->createNewImg($file->im,320,320,false);
    ?>  class.GD.Zoom.php<?php 
    class MakeMiniature{
    public $srcFile;
    public $dstFile;
    public $fileType;
    public $im;
    public $imgType = array("jpg","gif","png","bmp");

    public function findType($fileName){
    if($type=strrchr($fileName,".")){
    $type=substr($type,1);
    if(!strstr($type,".")){
    $var=$type;
    }else{
    echo "file type error!";
    }
    }else{
    echo "file type error!";
    } for($i=0;$i<=count($this->imgType);$i++){
    if(Strcmp($this->imgType[$i],$var)==0){
    $this->fileType=$var;
    return true;
    }else{
    return false;
    }
    }
    }

    function loadImg($fileType){
    $type=$this->isNull($fileType);
    switch($type){
    case "jpg":
    $im=ImageCreateFromjpeg($this->srcFile);
    break;
    case "gif":
    $im=ImageCreateFromGIF($this->srcFile);
    break;
    case "png":
    $im=imagecreatefrompng($this->srcFile);
    break;
    case "bmp":
    $im=imagecreatefromwbmp($this->srcFile);
    break;
    default:
    $im=0;
    echo "not you input file type!";
    break;
    }
    $this->im=$im;
    return $im;
    }

    function isNull($var){
    if(!isset($var)||empty($var)){
    echo "var is Null!";
    exit(0);
    }
    return $var;
    }

    function setParam($srcFile,$dstFile){
    $this->srcFile=$this->isNull($srcFile);
    $this->dstFile=$this->isNull($dstFile);

    if(!$this->findType($srcFile)){
    echo "file type error!";
    }

    if(!$this->loadImg($this->fileType)){
    echo "open ".$this->srcFile."error!";
    }
    }

    function getImgWidth($im){
    $im=$this->isNull($im);
    $width=imagesx($im);
    return $width;
    }

    function getImgHeight($im){
    $im=$this->isNull($im);
    $height=imagesy($im);
    return $height;
    }

    function createImg($im,$scale,$page){
    $im=$this->isNull($im);
    $scale=$this->isNull($scale);
    $srcW=$this->getImgWidth($im);
    $srcH=$this->getImgHeight($im);
    $detW=round($srcW*$scale/100);
    $detH=round($srcH*$scale/100);
    $om=imagecreatetruecolor($detW,$detH);
    //ImageCopyResized($om,$im,0,0,0,0,$detW,$detH,$srcW,$srcH);
    imagecopyresampled($om,$im,0,0,0,0,$detW,$detH,$srcW,$srcH);
    $this->showImg($om,$this->fileType,$page);
    }

    function createNewImg($im,$width,$height,$page){
    $im=$this->isNull($im);
    $srcW=$this->getImgWidth($im);
    $srcH=$this->getImgHeight($im);
    $detW=$this->isNull($width);
    $detH=$this->isNull($height);
    $om=imagecreatetruecolor($detW,$detH);
    //ImageCopyResized($om,$im,0,0,0,0,$detW,$detH,$srcW,$srcH);
    imagecopyresampled($om,$im,0,0,0,0,$detW,$detH,$srcW,$srcH);
    $this->showImg($om,$this->fileType,$page);
    }

    function inputError($boolean){
    if(!$boolean){
    echo "img input error!";
    }
    }

    function showImg($om,$type,$page){
    $om=$this->isNull($om);
    $type=$this->isNull($type);

    switch($type){
    case "jpg":
    if($page)
    {
    $suc=imagejpeg($om);
    $this->inputError($suc);
    }else{
    $suc=imagejpeg($om,$this->dstFile);
    $this->inputError($suc);
    }
    break;
    case "gif":
    if($page){
    $suc=imagegif($om);
    $this->inputError($suc);
    }else{
    $suc=imagegif($om,$this->dstFile);
    $this->inputError($suc);
    }
    break;
    case "png":
    if($page){
    $suc=imagepng($om);
    $this->inputError($suc);
    }else{
    $suc=imagepng($om,$this->dstFile);
    $this->inputError($suc);
    }
    break;
    case "bmp":
    if($page){
    $suc=imagewbmp($om);
    $this->inputError($suc);
    }else{
    $suc=imagewbmp($om,$this->dstFile);
    $this->inputError($suc);
    }
    break;
    default:
    echo "not you input file type!";
    break;
    }
    }
    }
    ?> 
      

  6.   

    可能造成图片质量差的函数是
    imagecopyresized在GD2中应使用
    imagecopyresampled 
      

  7.   

    对,imagecopyresampled(
    用这个重取样的函数,这个会做抗锯齿什么的处理吧
      

  8.   

    <?php
    // The file 本例将把一幅图像按最宽或最高像素来按比例缩放显示。
    $filename = '10.jpg';// Set a maximum height and width
    $width = !$_GET["w"]?200:$_GET["w"];
    $height = !$_GET["h"]?200:$_GET["h"];// Content type
    header('Content-type: image/jpeg');// Get new dimensions
    list($width_orig, $height_orig) = getimagesize($filename);if ($width && ($width_orig/$height_orig < $width/$height)) {
        $width = ($height / $height_orig) * $width_orig;
    } else {
        $height = ($width / $width_orig) * $height_orig;
    }// Resample
    $image_p = imagecreatetruecolor($width, $height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);// Output 90为图片质量
    imagejpeg($image_p, null,90);
    //保存
    imagejpeg($image_p, "new\index_10.jpg", 90);
    ?>