水印是向数据多媒体(如图像、声音、视频信号等)中添加某些数字信息以达到文件真伪鉴别、版权保护等功能。嵌入的水印信息隐藏于宿主文件中,不影响原始文件的可观性和完整性。
本文介绍php添加png图片背景透明水印操作类定义与用法,涉及php操作图片的显示、保存、压缩、水印添加等相关操作技巧。
图片相关操作类
class imagetool
{
private $imagepath;//图片路径
private $outputdir;//输出文件夹
public $memoryimg;//内存图像
public $path;
public function __construct($imagepath, $outputdir = null)
{
$this->imagepath = $imagepath;
$this->outputdir = $outputdir;
$this->memoryimg = null;
$this->path = null;
}
/
* 显示内存中的图片
* @param $image
*/
public function showimage()
{
if ($this->memoryimg != null) {
$info = getimagesize($this->imagepath);
$type = image_type_to_extension($info[2], false);
header('content-type:' . $info['mime']);
$funs = image{$type};
$funs($this->memoryimg);
imagedestroy($this->memoryimg);
$this->memoryimg = null;
}
}
/
* 保存图片
* @param $image 图片路径
* @return string
*/
private function saveimage($image)
{
$info = getimagesize($this->imagepath);
$type = image_type_to_extension($info[2], false);
$funs = image{$type};
if (empty($this->outputdir)) {
$funs($image, md5($this->imagepath) . '.' . $type);
return md5($this->imagepath) . '.' . $type;
} else {
$funs($image, $this->outputdir . md5($this->imagepath) . '.' . $type);
return $this->outputdir . md5($this->imagepath) . '.' . $type;
}
}
/
* 压缩图片
* @param $width 压缩后宽度
* @param $height 压缩后高度
* @param bool $output 是否输出文件
* @return resource
*/
public function compressimage($width, $height, $output = false)
{
$image = null;
$info = getimagesize($this->imagepath);
$type = image_type_to_extension($info[2], false);
$fun = imagecreatefrom{$type};
$image = $fun($this->imagepath);
imagesavealpha($image,true);//
$thumbnail = imagecreatetruecolor($width, $height);
imagealphablending($thumbnail,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
imagesavealpha($thumbnail,true);//
imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $width, $height, $info[0], $info[1]);
imagedestroy($image);
if ($output) {
$path = $this->saveimage($thumbnail);
$this->path = $path;
}
$this->memoryimg = $thumbnail;
return $this;
}
/
* 为图像添加文字标记
*
* @param $content 文本内容
* @param $size 字体大小
* @param $font 字体样式
* @param bool $output 是否输出文件
* @return $this
*/
public function addtextmark($content, $size, $font, $output = false)
{
$info = getimagesize($this->imagepath);
$type = image_type_to_extension($info[2], false);
$fun = imagecreatefrom{$type};
$image = $fun($this->imagepath);
$color = imagecolorallocatealpha($image, 0, 0, 0, 80);
$posx = imagesx($image) - strlen($content) * $size / 2;
$posy = imagesy($image) - $size / 1.5;
imagettftext($image, $size, 0, $posx, $posy, $color, $font, $content);
if ($output) {
$this->saveimage($image);
}
$this->memoryimg = $image;
return $this;
}
/
* 为图片添加水印
*
* @param $watermark 水印图片路径
* @param $alpha 水印透明度(0-100)
* @param bool $output 是否输出文件
* @return $this
*/
public function addwatermark($watermark, $alpha, $output = false)
{
$image_info = getimagesize($this->imagepath);
$image_type = image_type_to_extension($image_info[2], false);
$image_fun = imagecreatefrom{$image_type};
$image = $image_fun($this->imagepath);
$mark_info = getimagesize($watermark);
$mark_type = image_type_to_extension($mark_info[2], false);
$mark_fun = imagecreatefrom{$mark_type};
$mark = $mark_fun($watermark);
$posx = imagesx($image) - imagesx($mark);
$posy = imagesy($image) - imagesy($mark);
imagesavealpha($mark, true);
imagecopymerge($image, $mark, $posx, $posy, 0, 0, $mark_info[0], $mark_info[1], $alpha);
imagesavealpha($mark, true);
if ($output) {
$path = $this->saveimage($image);
$this->path = $path;
}
$this->memoryimg = $image;
return $this;
}
//用给定角度旋转图像,以jpeg图像格式为例
/
* 水印图片旋转
* @param $degrees 旋转角度
* @param bool $output 是否保存图片
* @return $this
*/
function rotateimage($degrees, $output = false)
{
$info = getimagesize($this->imagepath);
$type = image_type_to_extension($info[2], false);
$fun = imagecreatefrom{$type};
$image = $fun($this->imagepath);
$block = imagecreatetruecolor(170,170);//建立一个画板
$bg = imagecolorallocatealpha($block , 0 , 0 , 0 , 127);//拾取一个完全透明的颜色
$image = imagerotate($image, $degrees, $bg ,0);
imagesavealpha($image, true);
header(content-type: image/{$type});
//旋转后的图片保存
if ($output) {
$path = $this->saveimage($image);
$this->path = $path;
}
$this->memoryimg = $image;
return $this;
}
/
* 添加png透明图片
* $bigimgpath 目标图片路径
* $smallimgpath 水印图片路径
* $width 相对于目标图的x轴放置位置 左上角为 0
* $height 相对于目标图的y轴放置位置 左上角为0
* $bigimgpaths 合成后的图片路径 若路径名与第一张或第二张路径相同 直接覆盖原图
*/
public function mergerimg($bigimgpath, $smallimgpath, $width, $height, $bigimgpaths)
{
$image_kuang = imagecreatefromstring(file_get_contents($smallimgpath));
$image_photo = imagecreatefromstring(file_get_contents($bigimgpath));
//创建一个新的,和大图一样大的画布
$image_3 = imagecreatetruecolor(imagesx($image_photo), imagesy($image_photo));
//为真彩色画布创建白色背景,再设置为透明
$color = imagecolorallocate($image_3, 255, 255, 255);
imagefill($image_3, 0, 0, $color);
imagecolortransparent($image_3, $color);
/
* 先copy图片,再copy画框,实现png的透明效果,将图片嵌入到画框里
* imagecopymerge与imagecopy的不同:
* imagecopymerge 函数可以支持两个图像叠加时,设置叠加层的透明度。imagecopymerge比imagecopy多一个参数,来设置透明度
* php内部源码里,imagecopymerge在透明度参数为100时,直接调用imagecopy函数。
* imagecopy 函数则不支持叠加透明,但拷贝时可以保留png图像的原透明信息,而imagecopymerge却不支持图片的本身的透明拷贝
* 即:使用imagecopymerge函数,可以实现打上透明度为30%的淡淡的水印图标,但图片本身的png就会变得像ie6不支持png透明那样,背景不透明了。
* 如果使用imagecopy函数,可以保留图片本身的透明信息,但无法实现30%的淡淡水印叠加,
*/
imagecopyresampled($image_3,$image_photo,0,0,0,0,imagesx($image_photo),imagesy($image_photo),imagesx($image_photo),imagesy($image_photo));
imagecopy($image_3,$image_kuang, $width,$height,0,0,imagesx($image_kuang),imagesy($image_kuang));
//存储图片路径
imagejpeg($image_3, $bigimgpaths);
return $bigimgpaths;
}
}
控制器调用方法
public function test()
{
$bigimgpath = 'ren.jpg';//原图路径
$waterimgpath = 'tae.png';//水印图路径
$imagetool = new imagetool($waterimgpath, 'tmp/');//图片路径、输出文件夹
$smallimgpath = $imagetool->rotateimage(45, true)->path;//旋转
$width = 0;//水印所在x坐标
$height = 0;//水印所在y坐标
$bigimgpaths = 'new.png';//生成原图加水印新图路径
$path = $this->mergerimg($bigimgpath, $smallimgpath, $width, $height, $bigimgpaths);
return view('image', compact('path'));
}
电脑使用好压进行文件压缩时设置解压密码提示的方法什么是腾讯企业邮箱文件中转站?域名的购买流程是怎样的 申请域名的时候要注意什么怎么创建个人网站?创建个人网站有什么好处?电脑突然重启跟断电一样怎么回事 电脑玩一会就自动重启的处理方法推广站建站需要注意哪些问题?网站推广多少钱?网站建设明细报价是怎样的 有域名后怎样进行网站建设域名怎么卖出 不用的域名怎么注销备案