图片添加水印和生成缩略图
<?php //图片添加水印效果 class imgGD{ protected $im; protected $water; protected $image; protected $img_width; //源图片宽度 protected $img_height; //源图片高度 protected $img_type; //源类型 protected $water_width; //水印图片宽度 protected $water_height; //水印图片高度 protected $water_type; //水印图片类型 protected $x; //水印显示的x轴坐标 protected $y; //水印显示的y轴坐标 protected $pct; //水印的透明度 //等到文件的基本信息 public function getExt($img){ $info = getimagesize($img); return $info; } //根据文件的类型,创建一个对应类型的图像 public function createImage($type,$image){ switch ($type){ case 1: $img = imagecreatefromgif($image); break; case 2: $img = imagecreatefromjpeg($image); break; case 3: $img = imagecreatefrompng($image); break; case 6: $img = imagecreatefromwbmp($image); break; default: break; } return $img; } //保存图片 public function saveImage($type,$image,$dir){ switch ($type){ case 1: imagegif($image,$dir); break; case 2: imagejpeg($image,$dir); break; case 3: imagepng($image,$dir); break; case 6: imagewbmp($image,$dir); break; default: break; } } //设置水印显示的位置 //0:居中 //1:左上角 //2:右上角 //3:左下角 //4:右下角 public function water_position($position){ switch ($position){ case 0: $this->x = round(($this->img_width - $this->water_width)/2); $this->y = round(($this->img_height - $this->water_height)/2); break; case 1: $this->x = 20; $this->y = 20; break; case 2: $this->x = 20; $this->y = $this->img_height - $this->water_height - 20; break; case 3: $this->x = $this->img_width - $this->water_width - 20; $this->y = 20; break; case 4: $this->x = $this->img_width - $this->water_width - 20; $this->y = $this->img_height - $this->water_height - 20; break; default: $this->x = 20; $this->y = 20; break; } } //取得源图片的目录,并在该目录下新建缩略图存放的文件夹 public function mk_dir($name){ $path = substr($this->image,0,strripos($this->image,'/')) .'/'. $name; // ./small //exit; $file_name = $name . '_' .substr($this->image,(strripos($this->image,'/')+1)); //small_1.jpg if(!is_dir($path)){ if(!mkdir($path,0777,true)){ echo '失败'; return false; } } $path = $path . '/' . $file_name; return $path; } //保存small_img public function save_small($size,$name){ //求缩放比例 if($this->img_width >= $this->img_height){ $width = $size; $height = ($this->img_height/$this->img_width)*$size; }else{ $width = ($this->img_width/$this->img_height)*$size; $height = $size; } //求坐标 $x = ($size - $width)/2; $y = ($size - $height)/2; $small = imagecreatetruecolor($size,$size); $bg = imagecolorallocate($small,255,255,255); imagefill($small,0,0,$bg); imagecopyresampled($small,$this->im,$x,$y,0,0,$width,$height,$this->img_width,$this->img_height); //计算出源文件存放的目录的存放路径 $small_img = $this->mk_dir($name); //header('content-type: image/jpeg'); $this->saveImage($this->img_type,$small,$small_img); return $small_img; } //添加水印 public function watermark($image,$water,$position=1,$pct=35){ //水印的透明度 $this->pct = $pct; //获得图片 $this->image = $image; //取得源图片的基本信息 $info = $this->getExt($this->image); $this->img_width = $info[0]; $this->img_height = $info[1]; $this->img_type = $info[2]; //取得水印图片的基本信息 $info = $this->getExt($water); $this->water_width = $info[0]; $this->water_height = $info[1]; $this->water_type = $info[2]; //计算水印显示的位置 $this->water_position($position); //分别创建一个对应类型的图像 $this->im = $this->createImage($this->img_type,$this->image); $this->water = $this->createImage($this->water_type,$water); //把水印图片粘到源图片上去,使用imagecopymerge imagecopymerge($this->im,$this->water,$this->x,$this->y,0,0,$this->water_width,$this->water_height,$this->pct); //保存源图片 //header('content-type: image/jpeg'); $this->saveImage($this->img_type,$this->im,$this->image); $list = array(); //小缩略图 if($thumb = $this->save_small(150,'goods_thumb')){ $list['goods_thumb'] = str_replace(ROOT,'',$thumb); } //中缩略图 if($small = $this->save_small(200,'goods_img')){ $list['goods_img'] = str_replace(ROOT,'',$small); } return $list; } }