PHP implements Watermarking Based on the image color field in different locations

Source: Internet
Author: User
Tags image processing library
This article mainly introduces how to add watermarks to different positions in the image color field in PHP. it involves some techniques related to using the MagickWand module in php to add watermarks to images, which is of great practical value, for more information about how to add watermarks to different positions in the image color field, see the example in this article. Share it with you for your reference. The details are as follows:

When using php programming, you often need to add a watermark to the uploaded image to determine the copyright and source of the image. however, the position of the watermark is usually in the lower-right corner of the image. However, different images have different levels of color. sometimes, when the watermark of our image is the same as that of the image itself, the watermark will not be obvious.

The following code automatically identifies the color order of the image and adds the watermark to the image based on the color difference. This avoids the disadvantages of the watermark and the image's color level.

<? 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); // Adjust the default Watermark Position $ lt_w = 50; $ lt_h = 50; if ($ x = 0) {$ w = MagickGetImageWidth ($ nmw); $ h = MagickGetImageHeight ($ nmw); $ x = $ w; $ y = $ h;} else {// Adjust $ lt_w = according to the actual situation 30; $ lt_h = 40;} MagickCompositeImage ($ nmw, $ nmw_water, MW_OverCompositeOp, $ x-$ w1-$ lt_w, $ y-$ h1-$ lt_h ); magickWriteImage ($ nmw, $ output_file); DestroyMagickWand ($ nmw);} // function compute ($ 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'; // This is the coordinate array (posX, posY) $ arr = array (50, 5 0); $ water = '. /water.png '; $ 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 the image processing library of ImageMagick. The following 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);}?>

Installation method:

1. download php_magickwand_q16_st.dll for 5.2.x
2. place it in the extension directory of PHP
3. add extension = php_magickwand_q16_st.dll to the php. ini file.
4. restart apache

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.