« »
2008-10-05PHP

38

php给图片加水印

//示例
$image = new Gimage();
$image->limit = 600;//长宽限制
$image->wm_text=”www.linuxlaptop.cn”;//水印文字
$image->wm_fontfile=”font/xsuni.ttf”;//字体文件
$image->wm_color=”#ff0000″;
$image->save_file = “ltcn.jpg”;//保存到xx文件
$image->create(“linuxlaptop.jpg”);//从xx文件创建

源代码如下:

  1. /*
  2. +--------------------------------------------------------------------------
  3. | 生成缩略图&加水印的图片类
  4. +--------------------------------------------------------------------------
  5. */ 
  6. Class Gimage{ 
  7.  
  8. var $input_type = ""; //输入图片的格式
  9. var $output_type = "jpg"; //输出图片的格式
  10. var $limit = 0; //图片大小限制
  11. var $filename = ""; //输入图片的文件名(也可以直接是图片数据)
  12. var $jpeg_quality = 90; //jpeg图片质量
  13. var $save_file = ''; //输出文件名
  14. var $wm_text = ""; //水印文字( 不支持中文:'( )
  15. var $wm_size = 12; //水印文字大小
  16. var $wm_angle = 0; //水印文字角度
  17. var $wm_x = 30; //水印x坐标
  18. var $wm_y = 30; //水印y坐标
  19. var $wm_color = "#cccccc"; //水印颜色
  20. var $wm_fontfile = "geodesic.ttf";//水印字体文件
  21.  
  22. function create($filename="") 
  23. { 
  24. if ($filename) $this->filename = $filename;
  25.  
  26. if (!$this->input_type) $this->get_type();
  27. if (!$this->output_type) $this->output_type = $this->input_type;
  28.  
  29. if ($this->input_type == "jpg") $this->input_type = "jpeg";
  30. if ($this->output_type == "jpg") $this->output_type = "jpeg";
  31.  
  32. switch ($this->input_type){ 
  33.  case 'gif':
  34.   $src_img=ImageCreateFromGIF($this->filename);
  35.   break;
  36.  
  37.  case 'jpeg':
  38.   $src_img=ImageCreateFromJPEG($this->filename);
  39.   break;
  40.  
  41.  case 'png':
  42.   $src_img=ImageCreateFromPNG($this->filename);
  43.   break;
  44.  
  45.  default:
  46.   $src_img=ImageCreateFromString($this->filename);
  47.   break;
  48.  
  49. } 
  50.  
  51.  
  52. $src_w=ImageSX($src_img);
  53. $src_h=ImageSY($src_img);
  54. if ($src_w>=$src_h){ 
  55.  if ($src_w>$this->limit){ 
  56.   $new_w=$this->limit;
  57.   $new_h=($this->limit / $src_w)*$src_h;
  58.  } 
  59. } 
  60. else{ 
  61.  if ($src_h>$this->limit){ 
  62.   $new_h=$this->limit;
  63.   $new_w=($this->limit / $src_h)*$src_w;
  64.  } 
  65. } 
  66.  
  67. if ($new_h){ 
  68.  $dst_img=imagecreatetruecolor($new_w,$new_h);
  69.  imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
  70. } 
  71. else{ 
  72.  $dst_img = $src_img;
  73. } 
  74.  
  75. if ($this->wm_text) 
  76. { 
  77.  if(preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_color, $color)) 
  78.  { 
  79.   $red = hexdec($color[1]);
  80.   $green = hexdec($color[2]);
  81.   $blue = hexdec($color[3]);
  82.  } 
  83.  $wm_color = imagecolorallocatealpha($dst_img, $red, $green, $blue, 90);
  84.  imagettftext($dst_img, $this->wm_size, $this->wm_angle, $this->wm_x, $this->wm_y, $wm_color, $this->wm_fontfile, $this->wm_text);
  85. } 
  86.  
  87. if ($this->save_file) 
  88. { 
  89.  switch ($this->output_type){ 
  90.   case 'gif':
  91.   $src_img=ImagePNG($dst_img, $this->save_file);
  92.   break;
  93.  
  94.   case 'jpeg':
  95.   $src_img=ImageJPEG($dst_img, $this->save_file, $this->jpeg_quality);
  96.   break;
  97.  
  98.   case 'png':
  99.   $src_img=ImagePNG($dst_img, $this->save_file);
  100.   break;
  101.  
  102.   default:
  103.   $src_img=ImageJPEG($dst_img, $this->save_file, $this->jpeg_quality);
  104.   break;
  105.  
  106.  } 
  107. } 
  108. else 
  109. { 
  110.  header("Content-type: image/{$this->output_type}");
  111.  switch ($this->output_type){ 
  112.   case 'gif':
  113.   $src_img=ImagePNG($dst_img);
  114.   break;
  115.  
  116.   case 'jpeg':
  117.   $src_img=ImageJPEG($dst_img, "", $this->jpeg_quality);
  118.   break;
  119.  
  120.   case 'png':
  121.   $src_img=ImagePNG($dst_img);
  122.   break;
  123.  
  124.   default:
  125.   $src_img=ImageJPEG($dst_img, "", $this->jpeg_quality);
  126.   break;
  127.  
  128.  } 
  129. } 
  130.  
  131.  
  132. imagedestroy($dst_img);
  133.  
  134. } 
  135. function get_type()//获取图像文件类型
  136. { 
  137. $name_array = explode(".",$this->filename);
  138. if (preg_match("/\.(jpg|jpeg|gif|png)$/", $this->filename, $matches)) 
  139. { 
  140.  $this->input_type = strtolower($matches[1]);
  141. } 
  142. else 
  143. { 
  144.  $this->input_type = "string";
  145. } 
  146.  
  147. } 
  148.  
  149. }

您还可能感兴趣的内容

日志信息 »

该日志于2008-10-05 23:18由 admin 发表在PHP分类下, 你可以发表评论。除了可以将这个日志以保留源地址及作者的情况下引用到你的网站或博客,还可以通过RSS 2.0订阅这个日志的所有评论。

没有评论

发表评论 »


返回顶部