您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php 图片处理类_PHP教程

2024/4/6 0:57:30发布24次查看
path=rtrim($path, /)./; 
        } 
        /* 对图片进行缩放
         *
         * 参数$name: 是需要处理的图片名称
         * 参数$width:是缩放后的宽度
         * 参数$height:是缩放后的高度
         * 参数$qz: 是新图片的名称前缀
         * 返回值:就是缩放后的图片名称,失败则返回false
         *
         */ 
        function thumb($name, $width, $height, $qz=th_){ 
            //获取图片信息  
            $imginfo=$this->getinfo($name); //图片的宽度,高度,类型  
            //获取图片资源, 各种类型的图片都可以创建资源 jpg, gif, png  
            $srcimg=$this->getimg($name, $imginfo); 
            //获取计算图片等比例之后的大小, $size[width], $size[height]  
            $size=$this->getnewsize($name, $width, $height, $imginfo); 
            //获取新的图片资源, 处理一下gif透明背景  
            $newimg=$this->kidofimage($srcimg, $size, $imginfo); 
            //另存为一个新的图片,返回新的缩放后的图片名称      
            return $this->createnewimage($newimg, $qz.$name, $imginfo);   
        }
private function createnewimage($newimg, $newname, $imginfo){ 
            switch($imginfo[type]){ 
                case 1://gif  
                    $result=imagegif($newimg, $this->path.$newname); 
                    break; 
                case 2://jpg  
                    $result=imagejpeg($newimg, $this->path.$newname); 
                    break; 
                case 3://png  
                    $return=imagepng($newimg, $this->path.$newname); 
                    break; 
            } 
            imagedestroy($newimg); 
            return $newname; 
        }
private function kidofimage($srcimg, $size, $imginfo){ 
            $newimg=imagecreatetruecolor($size[width], $size[height]);
$otsc=imagecolortransparent($srcimg);
if($otsc >=0 && $otsc                 $tran=imagecolorsforindex($srcimg, $otsc);
$newt=imagecolorallocate($newimg, $tran[red], $tran[green], $tran[blue]);
imagefill($newimg, 0, 0, $newt);
imagecolortransparent($newimg, $newt); 
            }
imagecopyresized($newimg, $srcimg, 0, 0, 0, 0, $size[width], $size[height], $imginfo[width], $imginfo[height]);
imagedestroy($srcimg);
return $newimg; 
        }
private function getnewsize($name, $width, $height, $imginfo){ 
            $size[width]=$imginfo[width]; 
            $size[height]=$imginfo[height];
//缩放的宽度如果比原图小才重新设置宽度  
            if($width                 $size[width]=$width; 
            } 
            //缩放的高度如果比原图小才重新设置高度  
            if($height                 $size[height]=$height; 
            }
//图片等比例缩放的算法  
            if($imginfo[width]*$size[width] > $imginfo[height] * $size[height]){ 
                $size[height]=round($imginfo[height]*$size[width]/$imginfo[width]); 
            }else{ 
                $size[width]=round($imginfo[width]*$size[height]/$imginfo[height]); 
            }
return $size;
}
private function getinfo($name){ 
            $data=getimagesize($this->path.$name);
$imageinfo[width]=$data[0]; 
            $imageinfo[height]=$data[1]; 
            $imageinfo[type]=$data[2];
return $imageinfo; 
        }
private function getimg($name, $imginfo){ 
            $srcpic=$this->path.$name;
switch($imginfo[type]){ 
                case 1: //gif  
                    $img=imagecreatefromgif($srcpic); 
                    break; 
                case 2: //jpg  
                    $img=imagecreatefromjpeg($srcpic); 
                    break; 
                case 3: //png  
                    $img=imagecreatefrompng($srcpic); 
                    break; 
                default: 
                    return false;
}
return $img; 
        } 
        /* 功能:为图片加水印图片
         * 参数$groundname: 背景图片,即需要加水印的图片
         * 参数$watername: 水钱图片
         * 参数#aterpost:水印位置, 10种状态, 
         *  0为随机位置
         *
         *  1. 为顶端居左  2. 为顶端居中  3 为顶端居右
         *  4  为中部居左  5. 为中部居中  6 为中部居右
         *  7 . 为底端居左 8. 为底端居中, 9. 为底端居右
         *
         * 参数$qz : 是加水印后的图片名称前缀
         * 返回值:就是处理后图片的名称
         *
         */ 
        function watermark($groundname, $watername, $waterpos=0, $qz=wa_){
if(file_exists($this->path.$groundname) && file_exists($this->path.$watername)){ 
                $groundinfo=$this->getinfo($groundname); 
                $waterinfo=$this->getinfo($watername); 
                //水印的位置  
                if(!$pos=$this->position($groundinfo, $waterinfo, $waterpos)){ 
                    echo 水印不应该比背景图片小!; 
                    return; 
                }
$groundimg=$this->getimg($groundname, $groundinfo); 
                $waterimg=$this->getimg($watername, $waterinfo);
$groundimg=$this->copyimage($groundimg, $waterimg, $pos, $waterinfo);
return $this->createnewimage($groundimg, $qz.$groundname, $groundinfo); 
            }else{ 
                echo 图片或水印图片不存在; 
                return false; 
            } 
        }
private function copyimage($groundimg, $waterimg, $pos, $waterinfo){ 
            imagecopy($groundimg, $waterimg, $pos[posx], $pos[posy], 0, 0, $waterinfo[width], $waterinfo[height]); 
            imagedestroy($waterimg);
return $groundimg; 
        }
private function position($groundinfo, $waterinfo, $waterpos){ 
            //需要背景比水印图片大  
            if(($groundinfo[width]                return false; 
            }
switch($waterpos){ 
                case 1: 
                    $posx=0; 
                    $posy=0; 
                    break; 
                case 2: 
                    $posx=($groundinfo[width]-$waterinfo[width])/2; 
                    $posy=0; 
                    break; 
                case 3: 
                    $posx=$groundinfo[width]-$waterinfo[width]; 
                    $posy=0; 
                    break; 
                case 4: 
                    $posx=0; 
                    $posy=($groundinfo[height]-$waterinfo[height]) /2; 
                    break; 
                case 5: 
                    $posx=($groundinfo[width]-$waterinfo[width])/2; 
                    $posy=($groundinfo[height]-$waterinfo[height]) /2; 
                    break; 
                case 6: 
                    $posx=$groundinfo[width]-$waterinfo[width]; 
                    $posy=($groundinfo[height]-$waterinfo[height]) /2; 
                    break; 
                case 7: 
                    $posx=0; 
                    $posy=$groundinfo[height]-$waterinfo[height]; 
                    break; 
                case 8: 
                    $posx=($groundinfo[width]-$waterinfo[width])/2; 
                    $posy=$groundinfo[height]-$waterinfo[height]; 
                    break; 
                case 9: 
                    $posx=$groundinfo[width]-$waterinfo[width]; 
                    $posy=$groundinfo[height]-$waterinfo[height]; 
                    break; 
                case 0: 
                default: 
                    $posx=rand(0, ($groundinfo[width]-$waterinfo[width])); 
                    $posy=rand(0, ($groundinfo[height]-$waterinfo[height])); 
                    break; 
            }
return array(posx=>$posx, posy=>$posy); 
        }

path=rtrim($path, /)./;
  }
  /* 对图片进行缩放
   *
   * 参数$name: 是需要处理的图片名称
   * 参数$width:是缩放后的宽度
   * 参数$height:是缩放后的高度
   * 参数$qz: 是新图片的名称前缀
   * 返回值:就是缩放后的图片名称,失败则返回false
   *
   */
  function thumb($name, $width, $height, $qz=th_){
   //获取图片信息
   $imginfo=$this->getinfo($name); //图片的宽度,高度,类型
   //获取图片资源, 各种类型的图片都可以创建资源 jpg, gif, png
   $srcimg=$this->getimg($name, $imginfo);
   //获取计算图片等比例之后的大小, $size[width], $size[height]
   $size=$this->getnewsize($name, $width, $height, $imginfo);
   //获取新的图片资源, 处理一下gif透明背景
   $newimg=$this->kidofimage($srcimg, $size, $imginfo);
   //另存为一个新的图片,返回新的缩放后的图片名称 
   return $this->createnewimage($newimg, $qz.$name, $imginfo); 
  }
  private function createnewimage($newimg, $newname, $imginfo){
   switch($imginfo[type]){
    case 1://gif
     $result=imagegif($newimg, $this->path.$newname);
     break;
    case 2://jpg
     $result=imagejpeg($newimg, $this->path.$newname);
     break;
    case 3://png
     $return=imagepng($newimg, $this->path.$newname);
     break;
   }
   imagedestroy($newimg);
   return $newname;
  }
  private function kidofimage($srcimg, $size, $imginfo){
   $newimg=imagecreatetruecolor($size[width], $size[height]);
$otsc=imagecolortransparent($srcimg);
   if($otsc >=0 && $otsc     $tran=imagecolorsforindex($srcimg, $otsc);
    $newt=imagecolorallocate($newimg, $tran[red], $tran[green], $tran[blue]);
    imagefill($newimg, 0, 0, $newt);
    imagecolortransparent($newimg, $newt);
   }
   imagecopyresized($newimg, $srcimg, 0, 0, 0, 0, $size[width], $size[height], $imginfo[width], $imginfo[height]);
   imagedestroy($srcimg);
   return $newimg;
  }
  private function getnewsize($name, $width, $height, $imginfo){
   $size[width]=$imginfo[width];
   $size[height]=$imginfo[height];
   //缩放的宽度如果比原图小才重新设置宽度
   if($width     $size[width]=$width;
   }
   //缩放的高度如果比原图小才重新设置高度
   if($height     $size[height]=$height;
   }
   //图片等比例缩放的算法
   if($imginfo[width]*$size[width] > $imginfo[height] * $size[height]){
    $size[height]=round($imginfo[height]*$size[width]/$imginfo[width]);
   }else{
    $size[width]=round($imginfo[width]*$size[height]/$imginfo[height]);
   }
return $size;
  }
  private function getinfo($name){
   $data=getimagesize($this->path.$name);
   $imageinfo[width]=$data[0];
   $imageinfo[height]=$data[1];
   $imageinfo[type]=$data[2];
   return $imageinfo;
  }
  private function getimg($name, $imginfo){
   $srcpic=$this->path.$name;
   switch($imginfo[type]){
    case 1: //gif
     $img=imagecreatefromgif($srcpic);
     break;
    case 2: //jpg
     $img=imagecreatefromjpeg($srcpic);
     break;
    case 3: //png
     $img=imagecreatefrompng($srcpic);
     break;
    default:
     return false;
}
   return $img;
  }
  /* 功能:为图片加水印图片
   * 参数$groundname: 背景图片,即需要加水印的图片
   * 参数$watername: 水钱图片
   * 参数#aterpost:水印位置, 10种状态,
   *  0为随机位置
   *
   *  1. 为顶端居左  2. 为顶端居中  3 为顶端居右
   *  4  为中部居左  5. 为中部居中  6 为中部居右
   *  7 . 为底端居左 8. 为底端居中, 9. 为底端居右
   *
   * 参数$qz : 是加水印后的图片名称前缀
   * 返回值:就是处理后图片的名称
   *
   */
  function watermark($groundname, $watername, $waterpos=0, $qz=wa_){
if(file_exists($this->path.$groundname) && file_exists($this->path.$watername)){
    $groundinfo=$this->getinfo($groundname);
    $waterinfo=$this->getinfo($watername);
    //水印的位置
    if(!$pos=$this->position($groundinfo, $waterinfo, $waterpos)){
     echo 水印不应该比背景图片小!;
     return;
    }
    $groundimg=$this->getimg($groundname, $groundinfo);
    $waterimg=$this->getimg($watername, $waterinfo);
    $groundimg=$this->copyimage($groundimg, $waterimg, $pos, $waterinfo);
    return $this->createnewimage($groundimg, $qz.$groundname, $groundinfo);
   }else{
    echo 图片或水印图片不存在;
    return false;
   }
  }
  private function copyimage($groundimg, $waterimg, $pos, $waterinfo){
   imagecopy($groundimg, $waterimg, $pos[posx], $pos[posy], 0, 0, $waterinfo[width], $waterinfo[height]);
   imagedestroy($waterimg);
   return $groundimg;
  }
private function position($groundinfo, $waterinfo, $waterpos){
   //需要背景比水印图片大
   if(($groundinfo[width]    return false;
   }
   switch($waterpos){
    case 1:
     $posx=0;
     $posy=0;
     break;
    case 2:
     $posx=($groundinfo[width]-$waterinfo[width])/2;
     $posy=0;
     break;
    case 3:
     $posx=$groundinfo[width]-$waterinfo[width];
     $posy=0;
     break;
    case 4:
     $posx=0;
     $posy=($groundinfo[height]-$waterinfo[height]) /2;
     break;
    case 5:
     $posx=($groundinfo[width]-$waterinfo[width])/2;
     $posy=($groundinfo[height]-$waterinfo[height]) /2;
     break;
    case 6:
     $posx=$groundinfo[width]-$waterinfo[width];
     $posy=($groundinfo[height]-$waterinfo[height]) /2;
     break;
    case 7:
     $posx=0;
     $posy=$groundinfo[height]-$waterinfo[height];
     break;
    case 8:
     $posx=($groundinfo[width]-$waterinfo[width])/2;
     $posy=$groundinfo[height]-$waterinfo[height];
     break;
    case 9:
     $posx=$groundinfo[width]-$waterinfo[width];
     $posy=$groundinfo[height]-$waterinfo[height];
     break;
    case 0:
    default:
     $posx=rand(0, ($groundinfo[width]-$waterinfo[width]));
     $posy=rand(0, ($groundinfo[height]-$waterinfo[height]));
     break;
   }
   return array(posx=>$posx, posy=>$posy);
  }
 }
 摘自 chaojie2009的专栏
http://www.bkjia.com/phpjc/478376.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/478376.htmltecharticle?php class image { private $path; //构造方法用来对图片所在位置进行初使化 function __construct($path=./){ $this-path=rtrim($path, /)./; } /* 对图片进行缩放...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product