php如何实现自动加水印

php如何实现自动加水印,第1张

水印逻辑有两种

一种是上传直接加水印

另一种是利用伪静态将图片访问重定向到处理程序,临时加水印缓存并输出

php处理图片加水印可以使用gd库中的相关函数

以下为临时手打代码,可以按此思路优化,有问题可以联系本人

//此处需根据上传的图片格式使用对应函数实例化图片

$img=imagecreatefromjpg($imgurl)

//根据水印图片路径实例化水印

$waterImg=imagecreatefrompng($waterpath)

//获取原图及水印图片尺寸,用以计算是否需要缩放及放置位置

list($width, $height, $type, $attr) = getimagesize($imgurl)

list($waterw, $waterh, $type, $attr) = getimagesize($waterpath)

$scale=1

$waterReleaseW=$waterw

$waterReleaseH=$waterh

if($waterReleaseW>$width*.5){

    $scale=$width*.5/$waterw

    $waterReleaseW = $width*.5

    $waterReleaseH = $waterh*$scale

}

if($waterReleaseH>$height*.5){

    $scale *= $height*.5/$waterh

    $waterReleaseH = $height*.5

    $waterReleaseW = $waterw*$scale

}

//将水印图片拷贝到原图指定位置(此示例为右下角)

imagecopyresized($img,$waterImg,

    $width-$waterReleaseW-10,$height-$waterReleaseH-10,

    0,0,

    $width-10,$height-10,

    $waterw,$waterh)

//销毁水印图片实例

imagedestroy($waterImg)

//水印后图片保存

imagejpeg($img,$newpath)

在使用php编程的时候, 很多时候需要对上传的图片加水印,来确定图片版权和出处. 但是,一般情况下加水印的位置是图片的右下角, 但是,不同图片的色阶不同,有时候我们 图片的水印和图片本身色阶相同,就会造成水印不明显.

下面这段代码可以实现自动识别图片的色阶,更加色阶差来添加图片的水印,这样可以避免水印和图片色阶一样的弊端.

<?php

function add_wm($nmw_water, $src_file, $output_file, $x, $y) {

if(file_exists($output_file))

return

$w1 = MagickGetImageWidth($nmw_water)

$h1 = MagickGetImageHeight($nmw_water)

$nmw =NewMagickWand()

MagickReadImage($nmw, $src_file)

// 默认的加水印位置调整

$lt_w = 50

$lt_h = 50

if($x == 0){

$w = MagickGetImageWidth($nmw)

$h = MagickGetImageHeight($nmw)

$x = $w

$y = $h

}else{

// 根据具体情况调整

$lt_w = 30

$lt_h = 40

}

MagickCompositeImage($nmw, $nmw_water, MW_OverCompositeOp, $x - $w1 - $lt_w, $y - $h1 - $lt_h)

MagickWriteImage($nmw, $output_file)

DestroyMagickWand($nmw)

}

// 还是groovy的eachFileRecurse好用啊

function add_wm_recurse($nmw_water, $to_dir, $output_dir, $arr) {

$dp = dir($to_dir)

while($file=$dp->read()){

if($file != '.' &&$file != '..'){

if(is_dir($to_dir . '/' . $file)){

mkdir($output_dir . '/' . $file)

add_wm_recurse($nmw_water, $to_dir . '/' . $file, $output_dir . '/' . $file, $arr)

}else{

if(!array_key_exists($to_dir . '/' . $file, $arr)){

continue

}

$sub_arr = $arr[$to_dir . '/' . $file]

if($sub_arr){

$x = intval($sub_arr[0])

$y = intval($sub_arr[1])

add_wm($nmw_water, $to_dir . '/' . $file, $output_dir . '/' . $file, $x, $y)

}

}

}

}

$dp->close()

}

$to_dir = './resized'

$output_dir = './output'

// 这个是我用java的ImageIO遍历图片像素获取的符合裤子颜色的区域的坐标array(posX, posY)

$arr = array(

array(50, 50)

)

$water = './water.png'

$nmw_water =NewMagickWand()

MagickReadImage($nmw_water, $water)

add_wm_recurse($nmw_water, $to_dir, $output_dir, $arr)

DestroyMagickWand($nmw_water)

?>

补充:

PHP图像处理模块 MagickWand用法

MagickWand 是一个PHP的模块,用来访问 ImageMagick 的图像处理库。下面是一个使用 MagicWand 的代码片段:

$magick_wand=NewMagickWand()

MagickReadImage($magick_wand,'rose.jpg')

$drawing_wand=NewDrawingWand()

DrawSetFont($drawing_wand,"/usr/share/fonts/bitstream-vera/Vera.ttf")

DrawSetFontSize($drawing_wand,20)

DrawSetGravity($drawing_wand,MW_CenterGravity)

$pixel_wand=NewPixelWand()

PixelSetColor($pixel_wand,"white")

DrawSetFillColor($drawing_wand,$pixel_wand)

if (MagickAnnotateImage($magick_wand,$drawing_wand,0,0,0,"Rose") != 0)

{

MagickEchoImageBlob( $magick_wand )

}

else

{

echo MagickGetExceptionString($magick_wand)

}

?>

安装方法:

1. 下载 php_magickwand_q16_st.dll for 5.2.x

2. 将其放在PHP的扩展目录

3. 在php.ini文件总增加 extension=php_magickwand_q16_st.dll

4. 重新启动apache

<?php

/*PHP图片加文字水印类库

QQ:3697578482 伤心的歌

该类库暂时只支持文字水印,位置为右下角,颜色随机

调用方法:

1、在需要加水印的文件顶部引入类库:

include_once 'imageClass.php'

2、声明新类:

$tpl=new image_fu

3、给图片水印提供参数:

$tpl->img(图片路径,水印文字,字体路径,字体大小,字体角度)

比如:$tpl->img('abc.jpg','这是水印文字','ziti.ttf',30,0)

*/

class image_fu{

private $image

private $img_info

private $img_width

private $img_height

private $img_im

private $img_text

private $img_ttf=''

private $img_new

private $img_text_size

private $img_jd

function img($img='',$txt='',$ttf='',$size=12,$jiaodu=0){

if(isset($img)&&file_exists($img)){//检测图片是否存在

$this->image =$img

$this->img_text=$txt

$this->img_text_size=$size

$this->img_jd=$jiaodu

if(file_exists($ttf)){

$this->img_ttf=$ttf

}else{

exit('字体文件:'.$ttf.'不存在!')

}

$this->imgyesno()

}else{

exit('图片文件:'.$img.'不存在')

}

}

private function imgyesno(){

$this->img_info =getimagesize($this->image)

$this->img_width =$this->img_info[0]//图片宽

$this->img_height=$this->img_info[1]//图片高

//检测图片类型

switch($this->img_info[2]){

case 1:$this->img_im = imagecreatefromgif($this->image)break

case 2:$this->img_im = imagecreatefromjpeg($this->image)break

case 3:$this->img_im = imagecreatefrompng($this->image)break

default:exit('图片格式不支持水印')

}

$this->img_text()

}

private function img_text(){

imagealphablending($this->img_im,true)

//设定颜色

$color=imagecolorallocate($this->img_im,rand(0,255),rand(0,255),rand(0,255))

$txt_height=$this->img_text_size

$txt_jiaodu=$this->img_jd

$ttf_im=imagettfbbox($txt_height,$txt_jiaodu,$this->img_ttf,$this->img_text)

$w = $ttf_im[2] - $ttf_im[6]

$h = $ttf_im[3] - $ttf_im[7]

//$w = $ttf_im[7]

//$h = $ttf_im[8]

unset($ttf_im)

$txt_y =$this->img_height-$h

$txt_x =$this->img_width-$w

//$txt_y =0

//$txt_x =0

$this->img_new=@imagettftext($this->img_im,$txt_height,$txt_jiaodu,$txt_x,$txt_y,$color,$this->img_ttf,$this->img_text)

@unlink($this->image)//删除图片

switch($this->img_info[2]) {//取得背景图片的格式

case 1:imagegif($this->img_im,$this->image)break

case 2:imagejpeg($this->img_im,$this->image)break

case 3:imagepng($this->img_im,$this->image)break

default: exit('水印图片失败')

}

}

//显示图片

function img_show(){echo '<img src="'.$this->image.'" border="0" alt="'.$this->img_text.'" />'}

//释放内存

private function img_nothing(){

unset($this->img_info)

imagedestroy($this->img_im)

}

}

?>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11611554.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存