php图片添加文字水印方法汇总

方法一:

<?php
header("content-type:text/html;charset=utf-8");
//指定图片路径
$src = "img/a.png";
//获取图片信息
$info = getimagesize($src);
//获取图片扩展名
$type = image_type_to_extension($info[2],false);

// echo $type;
// exit;
//动态的把图片导入内存中
$fun = "imagecreatefrom{$type}";
$image = $fun("img/a.png");

// var_dump($image);
// exit;
//指定字体颜色
$col = imagecolorallocatealpha($image,255,255,255,50);
// var_dump($col);
// exit;
//指定字体内容
$content = 'helloworld';
// $content = "<span style='color:red;font-style:italic;'>".$content."</span>";
// echo $content;
// exit;
//给图片添加文字
imagestring($image,80,200,90,$content,$col);
//指定输入类型
header('Content-type:'.$info['mime']);
//动态的输出图片到浏览器中
$func = "image{$type}";
$func($image);
//销毁图片
imagedestroy($image);

 ?>

方法二:

<?php
header("content-type:text/html;charset=utf-8");
class Image_class {
  private $image;
  private $info;

  /**
   * @param $src:图片路径
   * 加载图片到内存中
   */
  function __construct($src){
    $info = getimagesize($src);
    $type = image_type_to_extension($info[2],false);
    $this -> info =$info;
    $this->info['type'] = $type;
    $fun = "imagecreatefrom" .$type;
    $this -> image = $fun($src);
  }

  /**
   * @param $fontsize: 字体大小
   * @param $x: 字体在图片中的x位置
   * @param $y: 字体在图片中的y位置
   * @param $color: 字体的颜色是一个包含rgba的数组
   * @param $text: 想要添加的内容
   * 操作内存中的图片,给图片添加文字水印
   */
  public function fontMark($fontsize,$x,$y,$color,$text){
    $col = imagecolorallocatealpha($this->image,$color[0],$color[1],$color[2],$color[3]);
    imagestring($this->image,$fontsize,$x,$y,$text,$col);
  }
  /*
   * 输出图片到浏览器中
   */
  public function show(){
    header('content-type:' . $this -> info['mime']);
    $fun='image' . $this->info['type'];
    $fun($this->image);
  }

  /**
   * 销毁图片
   */
  function __destruct(){
    imagedestroy($this->image);
  }
}
//对类的调用
$obj = new Image_class('img/a.png');
$obj->fontMark(20,150,50,array(255,255,0,60),'hello');
$obj->show();

 ?>

方法三:

<?php
header("content-type:text/html;charset=utf-8");
// ini_set("display_errors",true);
 /*给图片加文字水印的方法*/
$dst_path = 'img/b.png';
$dst = imagecreatefromstring(file_get_contents($dst_path));
/*imagecreatefromstring()--从字符串中的图像流新建一个图像,返回一个图像标示符,其表达了从给定字符串得来的图像
图像格式将自动监测,只要php支持jpeg,png,gif,wbmp,gd2.*/
// echo "aaa";
// exit;
$font = 'font/arial.ttf';
$black = imagecolorallocate($dst, 255, 0, 0);
imagefttext($dst, 50, 50, 160, 180, $black, $font, 'Hello');
/*imagefttext($img,$size,$angle,$x,$y,$color,$fontfile,$text)
$img由图像创建函数返回的图像资源
size要使用的水印的字体大小
angle(角度)文字的倾斜角度,如果是0度代表文字从左往右,如果是90度代表从上往下
x,y水印文字的第一个文字的起始位置
color是水印文字的颜色
fontfile,你希望使用truetype字体的路径*/
list($dst_w,$dst_h,$dst_type) = getimagesize($dst_path);
/*list(mixed $varname[,mixed $......])--把数组中的值赋给一些变量
像array()一样,这不是真正的函数,而是语言结构,List()用一步操作给一组变量进行赋值*/
/*getimagesize()能获取到什么信息?
getimagesize函数会返回图像的所有信息,包括大小,类型等等*/
switch($dst_type){
  case 1://GIF
    header("content-type:image/gif");
    imagegif($dst);
    break;
  case 2://JPG
    header("content-type:image/jpeg");
    imagejpeg($dst);
    break;
  case 3://PNG
    header("content-type:image/png");
    imagepng($dst);
    break;
  default:
    break;
  /*imagepng--以PNG格式将图像输出到浏览器或文件
  imagepng()将GD图像流(image)以png格式输出到标注输出(通常为浏览器),或者如果用filename给出了文件名则将其输出到文件*/
}
imagedestroy($dst);

 ?>

 

posted @ 2016-02-01 10:14  侠岚之弋痕夕  阅读(534)  评论(0编辑  收藏  举报
Where is the starting point, we don't have a choice, but the destination where we can pursue!