PHP实现根据图片色界在不同位置加水印的方法

2025-05-29 0 68

本文实例讲述了PHP实现根据图片色界在不同位置加水印的方法。分享给大家供大家参考。具体如下:

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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60
<?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 的代码片段:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
$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程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP实现根据图片色界在不同位置加水印的方法 https://www.kuaiidc.com/100341.html

相关文章

发表评论
暂无评论