PHP: adding watermarks to different positions based on the image color field _ PHP Tutorial

Source: Internet
Author: User
PHP adds watermarks to different positions based on the image color field. PHP: how to add watermarks to different positions in the image color field this article describes how to add watermarks to different positions in the PHP image color field. Share it with you for your reference. Implementation Method: Add a watermark in different locations based on the color field of the image.

This example describes how to add watermarks to different positions in the image color field in PHP. Share it with you for your reference. The specific implementation method is as follows:

?

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

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 according to the actual situation

$ 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 );

}

// It's Still groovy's eachFileRecurse easy to use.

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 ';

// This is the coordinate array (posX, posY) of the area that matches the trousers color obtained by traversing image pixels using java ImageIO)

$ 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 );

I hope this article will help you with php programming.

Examples in this article describes how to add watermarks to different positions in the image color field in PHP. Share it with you for your reference. Implementation method...

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.