Home > Backend Development > PHP Tutorial > PHP implements the method of adding watermarks at different locations according to the color boundary of the picture, _PHP tutorial

PHP implements the method of adding watermarks at different locations according to the color boundary of the picture, _PHP tutorial

WBOY
Release: 2016-07-13 09:44:53
Original
819 people have browsed it

PHP implements the method of adding watermarks at different locations according to the color boundary of the image.

This article describes the example of PHP implementing the method of adding watermarks at different locations according to the color boundary of the image. Share it with everyone for your reference. The details are as follows:

When using PHP programming, it is often necessary to add a watermark to the uploaded image to determine the copyright and source of the image. However, generally the location of the watermark is the lower right corner of the image. However, the color levels of different images are different. , sometimes the watermark of our pictures has the same color level as the picture itself, which will cause the watermark to be indistinct.

The following code can automatically identify the color gradation of the picture and add the watermark of the picture based on the color gradation difference. This can avoid the same disadvantages of watermarks and picture color gradations.

<&#63;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);
&#63;>

Copy after login

Supplement:

How to use PHP image processing module MagickWand

MagickWand is a PHP module used to access ImageMagick's image processing library. Here is a code snippet using 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);
}
&#63;>

Copy after login

Installation method:

1. Download php_magickwand_q16_st.dll for 5.2.x
2. Place it in the PHP extension directory
3. Add extension=php_magickwand_q16_st.dll
to the php.ini file 4. Restart apache

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1044858.htmlTechArticlePHP implements the method of adding watermarks at different locations according to the color boundary of the picture. This article describes the example of PHP implementation according to the color boundary of the picture. How to add watermarks at different locations. Share it with everyone for your reference. Tool...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template