教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php图片添加水印示例

php图片添加水印示例

发布时间:2018-03-31   编辑:jiaochengji.com
本文介绍了php为图片添加水印的一段代码,php图片水印实现方法,有需要的朋友参考学习下。

例子,php为图片添加水印的代码。
 

复制代码 代码示例:
<?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文件创建
*/
/*
+------------------------------
| 生成缩略图&加水印的图片类
+------------------------------
*/
Class Gimage{
 
var $input_type = ""; //输入图片的格式
var $output_type = "jpg"; //输出图片的格式
var $limit = 0; //图片大小限制
var $filename = ""; //输入图片的文件名(也可以直接是图片数据)
var $jpeg_quality = 90; //jpeg图片质量
var $save_file = ''; //输出文件名
var $wm_text = ""; //水印文字( 不支持中文:'( )
var $wm_size = 12; //水印文字大小
var $wm_angle = 0; //水印文字角度
var $wm_x = 30; //水印x坐标
var $wm_y = 30; //水印y坐标
var $wm_color = "#cccccc"; //水印颜色
var $wm_fontfile = "geodesic.ttf";//水印字体文件
 
function create($filename="")
{
if ($filename) $this->filename = $filename;
 
if (!$this->input_type) $this->get_type();
if (!$this->output_type) $this->output_type = $this->input_type;
 
if ($this->input_type == "jpg") $this->input_type = "jpeg";
if ($this->output_type == "jpg") $this->output_type = "jpeg";
 
switch ($this->input_type){
 case 'gif':
  $src_img=ImageCreateFromGIF($this->filename);
  break;
 
 case 'jpeg':
  $src_img=ImageCreateFromJPEG($this->filename);
  break;
 
 case 'png':
  $src_img=ImageCreateFromPNG($this->filename);
  break;
 
 default:
  $src_img=ImageCreateFromString($this->filename);
  break;
}
$src_w=ImageSX($src_img);
$src_h=ImageSY($src_img);
if ($src_w>=$src_h){
 if ($src_w>$this->limit){
  $new_w=$this->limit;
  $new_h=($this->limit / $src_w)*$src_h;
 }
}
else{
 if ($src_h>$this->limit){
  $new_h=$this->limit;
  $new_w=($this->limit / $src_h)*$src_w;
 }
}
 
if ($new_h){
 $dst_img=imagecreatetruecolor($new_w,$new_h);
 imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
}
else{
 $dst_img = $src_img;
}
 
if ($this->wm_text)
{
 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))
 {
  $red = hexdec($color[1]);
  $green = hexdec($color[2]);
  $blue = hexdec($color[3]);
 }
 $wm_color = imagecolorallocatealpha($dst_img, $red, $green, $blue, 90);
 imagettftext($dst_img, $this->wm_size, $this->wm_angle, $this->wm_x, $this->wm_y, $wm_color, $this->wm_fontfile, $this->wm_text);
}
 
if ($this->save_file)
{
 switch ($this->output_type){
  case 'gif':
  $src_img=ImagePNG($dst_img, $this->save_file);
  break;
 
  case 'jpeg':
  $src_img=ImageJPEG($dst_img, $this->save_file, $this->jpeg_quality);
  break;
 
  case 'png':
  $src_img=ImagePNG($dst_img, $this->save_file);
  break;
 
  default:
  $src_img=ImageJPEG($dst_img, $this->save_file, $this->jpeg_quality);
  break;
 }
}
else
{
 header("Content-type: image/{$this->output_type}");
 switch ($this->output_type){
  case 'gif':
  $src_img=ImagePNG($dst_img);
  break;
 
  case 'jpeg':
  $src_img=ImageJPEG($dst_img, "", $this->jpeg_quality);
  break;
 
  case 'png':
  $src_img=ImagePNG($dst_img);
  break;
 
  default:
  $src_img=ImageJPEG($dst_img, "", $this->jpeg_quality);
  break;
 }
}
imagedestroy($dst_img);
 
}
function get_type()//获取图像文件类型
{
$name_array = explode(".",$this->filename);
if (preg_match("/\.(jpg|jpeg|gif|png)$/", $this->filename, $matches))
{
 $this->input_type = strtolower($matches[1]);
}
else
{
 $this->input_type = "string";
}

}

您可能感兴趣的文章:
php图片加水印的小例子
php gd库为页面添加水印实现代码
Laravel中如何给图片加水印?
asp.net在图片上添加水印效果的代码示例
php图片添加水印示例
php图片上加水印或文字的代码举例
php图片添加水印的例子
C# 添加图片水印的代码示例
php为图片加中文水印的代码
php 图片操作类(图片加水印)

关键词: php 图片水印  水印  图片水印   
[关闭]
~ ~