Home  >  Article  >  Backend Development  >  How to use MagickWand module to add watermark to pictures in PHP

How to use MagickWand module to add watermark to pictures in PHP

墨辰丷
墨辰丷Original
2018-06-07 17:05:191619browse

This article mainly introduces how to use the MagickWand module in PHP to add watermarks to images. Interested friends can refer to it. I hope it will be helpful to everyone.

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

The following code can automatically identify the color levels of the image and add pictures based on the color level difference. Watermark, this can avoid the same disadvantages of watermark and picture color scale.

<?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 != &#39;.&#39; && $file != &#39;..&#39;){
 if(is_dir($to_dir . &#39;/&#39; . $file)){
  mkdir($output_dir . &#39;/&#39; . $file);
  add_wm_recurse($nmw_water, $to_dir . &#39;/&#39; . $file, $output_dir . &#39;/&#39; . $file, $arr);
 }else{
  if(!array_key_exists($to_dir . &#39;/&#39; . $file, $arr)){
  continue;
  }
  $sub_arr = $arr[$to_dir . &#39;/&#39; . $file];
  if($sub_arr){
  $x = intval($sub_arr[0]);
  $y = intval($sub_arr[1]);
  add_wm($nmw_water, $to_dir . &#39;/&#39; . $file, $output_dir . &#39;/&#39; . $file, $x, $y);
  }
 }
 }
 }
 $dp->close();
 }
 $to_dir = &#39;./resized&#39;;
 $output_dir = &#39;./output&#39;;
 // 这个是我用java的ImageIO遍历图片像素获取的符合裤子颜色的区域的坐标array(posX, posY)
 $arr = array(
 array(50, 50)
 );
 $water = &#39;./water.png&#39;;
 $nmw_water =NewMagickWand();
 MagickReadImage($nmw_water, $water);
 add_wm_recurse($nmw_water, $to_dir, $output_dir, $arr);
 DestroyMagickWand($nmw_water);
?>

Supplement:

PHP image processing module MagickWand usage

MagickWand is a PHP module, used to access ImageMagick's image processing library. The following is a code snippet using MagicWand:

$magick_wand=NewMagickWand();
MagickReadImage($magick_wand,&#39;rose.jpg&#39;);
$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);
}
?>

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

Summary: The above is the entire content of this article. I hope it will be helpful to everyone's study.

Related recommendations:

php file upload class sharing_php example

Detailed explanation of php paging class with examples

Detailed explanation of php implementation of shopping cart class with a storage period of one day

The above is the detailed content of How to use MagickWand module to add watermark to pictures in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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