一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php图像操作类

时间:2022-06-24 21:12:14 编辑:袖梨 来源:一聚教程网

 代码如下 复制代码

class image{
        public $filename;
        private $info;
        private $im;
  

//构造
        public function __construct($filename){
                $this->filename = $filename;
                $this->info    = @getimagesize($filename);
    if($this->info[2]>3){echo "只支持gif、jpeg、png 格式。";exit;}
  
        }
       
// 载入
        public function imgload(){
   switch($this->info[2]){
    case 1:
    $this->im=@imagecreatefromgif($this->filename);
    break; 
    case 2:
    $this->im=@imagecreatefromjpeg($this->filename);
    break; 
    case 3:
    $this->im=@imagecreatefrompng($this->filename);
    break; 
   }
        }

// 保存
        public function imagesave($img,$savename,$inf){
   switch($inf){
    case 1:
    imagegif($img,$savename);;
    break; 
    case 2:
    imagejpeg($img,$savename);;
    break; 
    case 3:
    imagepng($img,$savename);;
    break; 
   }
        }  

  
//滤镜
 public function filter($arg=1,$savename=''){
  $this->imgload();
  
  if($savename=='')$savename='f_'.$this->filename;
     $ok=false;
     switch($arg){
   case 1:
   imagefilter($this->im,img_filter_negate);//反色
   $ok=true;
   break;
   case 2:
   imagefilter($this->im,img_filter_grayscale); //黑白
   $ok=true;
   break;
   case 3:
   imagefilter($this->im,img_filter_emboss);//浮雕
   $ok=true;
   break;
   case 4:
   imagefilter($this->im,img_filter_gaussian_blur); //高斯模糊
   $ok=true;
   break;
   case 5:
   imagefilter($this->im,img_filter_brightness,50); //亮度50
   $ok=true;
   break;
   case 6:
   imagefilter($this->im,img_filter_contrast,-50); //对比度-50
   $ok=true;
   break;  
  }
  if($ok){  
   $this->imagesave($this->im,$savename,$this->info[2]);  //写文件
   imagedestroy($this->im);
   return 1;
  }else{
   imagedestroy($this->im);
   return 0;}
 }
 

 

 

 

 代码如下 复制代码


//缩小
 public function trunsmall($arg=2,$savename=''){
  $this->imgload();
 
  if($savename=='')$savename='s_'.$this->filename;
     $imgwidth=$this->info[0]; //原图宽
  $imgheight=$this->info[1]; //原图高
  $arg2= sqrt($arg); //缩小度 等比例 
  $imgwidth2=$imgwidth/$arg2;  //新图宽
  $imgheight2=$imgheight/$arg2; //新图高
  
     if($arg>1 && $arg<10){
   $img = imagecreatetruecolor($imgwidth2, $imgheight2);
   imagecopyresized($img,$this->im,0,0,0,0,$imgwidth2,$imgheight2,$imgwidth,$imgheight);
   $this->imagesave($img,$savename,$this->info[2]);  //写文件
   imagedestroy($img);
   imagedestroy($this->im);
   return 1;      
  }else{
   imagedestroy($this->im);
   return 0;}
 }
 
//文字水印
public function printstr($str,$savename=''){
  $this->imgload();

  if($savename=='')$savename='p_'.$this->filename;
    
  $imgwidth=$this->info[0]; //原图宽
  $imgheight=$this->info[1]; //原图高

  $x=$imgwidth/8;  //x
  $y=$imgheight/2; //y
  
  $white = imagecolorallocate($this->im, 255, 255, 255);
  $grey1 = imagecolorallocate($this->im, 150, 150, 150);
  $grey2 = imagecolorallocate($this->im, 140, 140, 140);
  $font = 'c:/windows/fonts/simkai.ttf';
  if(is_file($font)){
   $str = iconv("gb2312","utf-8",$str);
   imagettftext($this->im, 14, 10, $x, $y, $grey1, $font, $str);
   imagettftext($this->im, 14, 10, $x+1, $y+1, $grey2, $font, $str);   
   $this->imagesave($this->im,$savename,$this->info[2]);  //写文件
   imagedestroy($this->im);
   return 1;
  }else{
   imagedestroy($this->im);
   return 0;}
   
}


//小图章水印
public function imageadd($image,$savename=''){
  $this->imgload();

  if($savename=='')$savename='i_'.$this->filename;
  if(!$image){return 0;exit;}
    
  $imgwidth=$this->info[0]; //原图宽
  $imgheight=$this->info[1]; //原图高
 
  $imageinfo = getimagesize($image); //小图章
  if($imageinfo[2]>3){return 0;exit;}
  
  switch($imageinfo[2]){
   case 1:
   $img=@imagecreatefromgif($image);
   break; 
   case 2:
   $img=@imagecreatefromjpeg($image);
   break; 
   case 3:
   $img=@imagecreatefrompng($image);
   break; 
  }  
 
  $imgwidth2=$imageinfo[0]; //小图宽
  $imgheight2=$imageinfo[1]; //小图高
  
  if($imgwidth2>$imgwidth/2 || $imgheight2>$imgheight/2){echo "小图章太大";exit;}
  
  if(imagecopy ( $this->im, $img, ($imgwidth-$imgwidth2)/8, ($imgheight-$imgheight2)/8, 0, 0, $imgwidth2, $imgheight2 )){
   $this->imagesave($this->im,$savename,$this->info[2]);  //写文件
   imagedestroy($img);
   imagedestroy($this->im); 
   return 1;
  }else{ 
   imagedestroy($img); 
   imagedestroy($this->im);     
   return 0;
  }   
}
 

 

 

       

 

 代码如下 复制代码

// 图像信息打印
        public function __tostring(){
   $infostr='';
   $infostr="图像名称:".$this->filename."
";
   $infostr.="图像宽度:".$this->info[0]."
";
   $infostr.="图像高度:".$this->info[1]."
";
   $infostr.="图像类型:".$this->info['mime']."
";  
   return $infostr;
        }

}

include('image_class.php');
$image=new image('001.jpg');

echo $image->trunsmall(4);
echo "


";

echo $image->filter(3);
echo "


";

echo $image->printstr("www.111com.net");
echo "


";

echo $image->imageadd("x.png");
echo "


";

echo $image;
echo "


";

热门栏目