8870188 发表于 2017-12-29 18:05:07

<小田吃饺子> PHP:GD库 图片水印处理

<?php  
/**
  
* 处理图片类
  
* 1.添加文字水印
  
* 2.添加图片水印
  
* 3.压缩图片
  
*/
  
class TL_Image{
  
private $image;//内存中的图片
  
private $info;//图片的基本信息
  
/**
  
* 打开一张图片,读取到内存
  
* @param $src 图片路径
  
*/
  
public function __construct($src){
  
$info = getimagesize($src);
  
$this->info = array(
  
'width' => $info,
  
'height' => $info,
  
'type' => image_type_to_extension($info['2'],false),
  
'mime' => $info['mime'],
  
);
  
$fun = "imagecreatefrom{$this->info['type']}";
  
$this->image = $fun($src);
  
}
  
/**
  
* 操作图片(压缩)
  
* @param $width 宽
  
* @param $height 高
  
* @return          
  
*/
  
public function thumb($width,$height){
  
$image_thumb = imagecreatetruecolor($width,$height);
  
imagecopyresampled($image_thumb, $this->image, 0, 0, 0, 0, $width, $height, $this->info['width'], $this->info['height']);
  
imagedestroy($this->image);
  
$this->image = $image_thumb;
  
}
  
/**
  
* 操作图片(添加文字水印)
  
*
  
* @param $content 设置文字
  
* @param $font_url 字体文件路径
  
* @param $size    字体大小
  
* @param $color    字体颜色 []
  
* @param $local    位置 []
  
* @param $angle    旋转
  
* @return          
  
*/
  
public function fontMark($content,$font_url,$size,$color,$local,$angle){
  
$col = imagecolorallocatealpha($this->image,$color,$color,$color,$color);
  
imagettftext($this->image, $size, $angle, $local['x'], $local['y'], $col, $font_url, $content);
  
}
  
/**
  
* 操作图片(添加图片水印)
  
* @param $source 水印图片路径
  
* @param $local 位置 []
  
* @param $alpha 透明
  
* @return          
  
*/
  
public function imageMark($source,$local,$alpha){
  
$info2 = getimagesize($source);
  
$type2 = image_type_to_extension($info2,false);
  
$fun2 = "imagecreatefrom{$type2}";
  
$water = $fun2($source);
  
imagecopymerge($this->image, $water,$local['x'],$local['y'], 0, 0, $info2, $info2, $alpha);
  
imagedestroy($water);
  
}
  
/**
  
* 浏览器输出图片
  
*/
  
public function show(){
  
header("Content-Type:" . $this->info['mime']);
  
$funs = "image{$this->info['type']}";
  
$funs($this->image);
  
}
  
/**
  
* 保存图片
  
* @param $newname 保存之后的名字
  
* @return          
  
*/
  
public function save($srcs){
  
$funs = "image{$this->info['type']}";
  
$funs($this->image,$srcs);
  
//move_uploaded_file($this->image, $srcs);
  
}
  
/**
  
* 销毁图片
  
*/
  
public function __destruct(){
  
imagedestroy($this->image);
  
}
  
}
  
页: [1]
查看完整版本: <小田吃饺子> PHP:GD库 图片水印处理