温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用PHP怎么根据图片色界在不同位置加水印

发布时间:2021-01-25 17:31:04 来源:亿速云 阅读:137 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关使用PHP怎么根据图片色界在不同位置加水印,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

<?php
 function add_wm($nmw_water, $src_file, $output_file, $x, $y) {
 if(file_exists($output_file))
 return;
 $w1 = MagickGetImageWidth($nmw_water);
 $h2 = 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 - $h2 - $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怎么根据图片色界在不同位置加水印了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI