php给图片加文字水印与图片水印代码 |
时间:2015-01-23 来源:西部数据 作者:西部数据 |
|
这款程序给图片加文字水印时是调用 了C:\\WINDOWS\\Fonts\\\\SIMHEI.TTF字体,给图片加水印时就可以自定图片,实例代码如下:
- $image->wprint_img();
- $image->wprint_string();
- class editimage{
- private $imagefile;
- private $smallimg;
- private $string;
- private $position;
- private $dst_x=600;
- private $dst_y=0;
- private $str_x=450;
- private $str_y=200;
- private $font="c:windows ontssimhei.ttf";
- private $imgej;
-
- function __get($value){
- return $this->$value;
- }
- function __set($property,$value){
- $this->$property=$value;
- }
-
-
-
-
-
-
-
-
-
-
- function __construct($imagefile,$smallimg='',$string=''){
- $this->imagefile=$imagefile;
- $this->smallimg=$smallimg;
- $this->string=$string;
- $this->imgej=$this->imagecreatef($this->imagefile);
- }
-
- function get_extname($file){
- if (file_exists($this->imagefile)) {
- $img=getimagesize($file);
- switch ($img[2]){
- case "1":
- return "gif";
- case "2":
- return "jpg";
- case "3":
- return "png";
- }
- }else{
- return false;
- }
- }
-
-
- function getsize($file,$wh){
- $image=getimagesize($file);
- if ($wh) {
- switch ($wh){
- case "w":
- return $image[0];
- case "h":
- return $image[1];
- }
- }else{
- return false;
- }
- }
- function imagecreatef($file){
- if ($this->get_extname($file)) {
- switch($this->get_extname($file)){
- case "gif":
- return imagecreatefromgif($file);
- case "jpg":
- return imagecreatefromjpeg($file);
- case "png":
- return imagecreatefrompng($file);
- }
- }else{
- echo "文件不存在";
- }
- }
-
-
- function wprint_img(){
- if($this->smallimg){
- imagecopy($this->imgej,$this->imagecreatef($this->smallimg),$this->dst_x,$this->dst_y,0,0,$this->getsize($this->smallimg,"w"),$this->getsize($this->smallimg,"h"));
- }else{
- return "水印图片不存在!";
- }
- }
-
- function wprint_string(){
- return imagettftext($this->imgej,20,0,$this->str_x,$this->str_y,imagecolorallocate($this->imgej,200,200,200),$this->font,iconv("gb2312","utf-8",$this->string));
- }
-
- function choose_imgouttype(){
- if($this->position){
- $this->get_extname($this->imagefile);
- switch ($this->get_extname($this->imagefile)){
- case "gif":
- return imagegif($this->imgej,$position);
- case "jpg":
- return imagejpeg($this->imgej,$this->position);
- case "jpeg":
- return imagejpeg($this->imgej,$this->position);
- case "png":
- return imagepng($this->imgej,$position);
- }
- }else{
-
- switch ($this->get_extname($this->imagefile)){
- case "gif":
- return imagegif($this->imgej);
- case "jpg":
- return imagejpeg($this->imgej);
- case "jpeg":
- return imagejpeg($this->imgej);
- case "png":
- return imagepng($this->imgej);
- }
- }
-
- }
- }
-
-
-
- $image=new editimage("d90.gif","hknmtt.png","我的d90");
- $image->wprint_img();
- $image->wprint_string();
- $image->choose_imgouttype();
|
|
|
|