php上传图片加水印,图片水印,文字水印 |
时间:2015-01-23 来源:西部数据 作者:西部数据 |
|
这是一款比较完整的在用户上传图片时就自动给图片增加上水印,这款增加水印功能可以增加文字水印与图片水印,实例代码如下:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class image_control
- {
- private $img_path;
- private $is_auto_reduce;
- private $is_appoint;
- private $multiple;
- private $is_water_str;
- private $water_str;
- private $is_watermark;
- private $logo_path;
- private $is_display;
- private $is_create;
-
- function __construct($img_path,$is_auto_reduce,$is_appoint,$multiple,$is_water_str,$water_str,$is_watermark,$logo_path,$is_display,$is_create)
- {
- $this->img_path=$img_path;
- $this->is_auto_reduce=$is_auto_reduce;
- $this->is_appoint=$is_appoint;
- $this->multiple=$multiple;
- $this->is_water_str=$is_water_str;
- $this->water_str=$water_str;
- $this->is_watermark=$is_watermark;
- $this->logo_path=$logo_path;
- $this->is_display=$is_display;
- $this->is_create=$is_create;
- }
-
- function img_control()
- {
-
- $img_info=getimagesize($this->img_path);
-
- switch($img_info[2])
- {
- case 1:
- $img_get=@imagecreatefromgif($this->img_path);
- break;
-
- case 2:
- $img_get=@imagecreatefromjpeg($this->img_path);
- break;
-
- case 3:
- $img_get=@imagecreatefrompng($this->img_path);
- break;
- }
-
-
- if($this->is_water_str==1)
- {
-
- $te=imagecolorallocate($img_get,255,255,255);
- $str=iconv("gbk","utf-8",$this->water_str);
- imagettftext($img_get,16,0,$img_info[0]-200,$img_info[1]-20,$te,'msyh.ttf',$str);
- }
-
-
- if($this->is_watermark==1)
- {
-
- $logo_info=getimagesize($this->logo_path);
-
- switch($logo_info[2])
- {
- case 1:
- $logo=@imagecreatefromgif($this->logo_path);
- break;
-
- case 2:
- $logo=@imagecreatefromjpeg($this->logo_path);
- break;
-
- case 3:
- $logo=@imagecreatefrompng($this->logo_path);
- break;
- }
-
-
-
- imagecopy($img_get,$logo,0,0,0,0,$logo_info[0],$logo_info[1]);
- }
-
-
-
- if($this->is_auto_reduce==1)
- {
- if($img_info[0]>=3000 || $img_info[1]>=3000)
- {
- $new_image_get=imagecreatetruecolor($img_info[0]*0.03,$img_info[1]*0.03);
- imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.03,$img_info[1]*0.03,$img_info[0],$img_info[1]);
- }
- else if($img_info[0]>=2500 || $img_info[1]>=2500)
- {
- $new_image_get=imagecreatetruecolor($img_info[0]*0.04,$img_info[1]*0.04);
- imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.04,$img_info[1]*0.04,$img_info[0],$img_info[1]);
- }
- else if($img_info[0]>=2000 || $img_info[1]>=2000)
- {
- $new_image_get=imagecreatetruecolor($img_info[0]*0.05,$img_info[1]*0.05);
- imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.05,$img_info[1]*0.05,$img_info[0],$img_info[1]);
- }
- else if($img_info[0]>=1500 || $img_info[1]>=1500)
- {
- $new_image_get=imagecreatetruecolor($img_info[0]*0.08,$img_info[1]*0.08);
- imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.08,$img_info[1]*0.08,$img_info[0],$img_info[1]);
- }
- else if($img_info[0]>=1000 || $img_info[1]>=1000)
- {
- $new_image_get=imagecreatetruecolor($img_info[0]*0.1,$img_info[1]*0.1);
- imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.1,$img_info[1]*0.1,$img_info[0],$img_info[1]);
- }
- else if($img_info[0]>=500 || $img_info[1]>=500)
- {
- $new_image_get=imagecreatetruecolor($img_info[0]*0.2,$img_info[1]*0.2);
- imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.2,$img_info[1]*0.2,$img_info[0],$img_info[1]);
- }
- else if($img_info[0]>=300 || $img_info[1]>=300)
- {
- $new_image_get=imagecreatetruecolor($img_info[0]*0.3,$img_info[1]*0.3);
- imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*0.3,$img_info[1]*0.3,$img_info[0],$img_info[1]);
- }
- else
- {
- $new_image_get=imagecreatetruecolor($img_info[0]*1,$img_info[1]*1);
- imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*1,$img_info[1]*1,$img_info[0],$img_info[1]);
- }
- }
-
-
-
- if($this->is_appoint)
- {
- $new_image_get=imagecreatetruecolor($img_info[0]*$this->multiple,$img_info[1]*$this->multiple);
- imagecopyresized($new_image_get,$img_get,0,0,0,0,$img_info[0]*$this->multiple,$img_info[1]*$this->multiple,$img_info[0],$img_info[1]);
- }
-
-
- if($this->is_display==1)
- {
- header("content-type: image/jpeg");
- return imagejpeg($new_image_get);
- }
-
-
- if($this->is_create==1)
- {
- $new_name=explode("/",$this->img_path);
- $new_name_string="";
-
- for($i=0;$i<count($new_name)-1;$i++)
- {
- $new_name_string.=$new_name[$i]."/";
- }
-
- $new_img_path=$new_name_string."new".$new_name[$i];
-
- if(imagejpeg($new_image_get,$new_img_path) && imagejpeg($img_get,$this->img_path))
- {
- setcookie("img_new_path", $new_img_path);
-
- }
- else
- {
- return "图片生成失败,请检查配置是否正确!";
- }
- }
- }
-
- function __desctruct()
- {
-
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
|
|
|