PHP watermarks images based on their color levels

Source: Internet
Author: User
Directly share the source code? Phpfunctionadd_wm ($ nmw_water, $ src_file, $ output_file, $ x, $ y) {if (file_exists ($ output_file) return; $ w1 = MagickGetImageWidth ($ nmw_water

Share source code directly

 
  1. Function add_wm ($ nmw_water, $ src_file, $ output_file, $ x, $ y ){
  2. If (file_exists ($ output_file ))
  3. Return;
  4. $ W1 = MagickGetImageWidth ($ nmw_water );
  5. $ H1 = MagickGetImageHeight ($ nmw_water );
  6. $ Nmw = NewMagickWand ();
  7. MagickReadImage ($ nmw, $ src_file );
  8. // Adjust the default Watermark Position
  9. $ Lt_w = 50;
  10. $ Lt_h = 50;
  11. If ($ x = 0 ){
  12. $ W = MagickGetImageWidth ($ nmw );
  13. $ H = MagickGetImageHeight ($ nmw );
  14. $ X = $ w;
  15. $ Y = $ h;
  16. } Else {
  17. // Adjust according to the actual situation
  18. $ Lt_w = 30;
  19. $ Lt_h = 40;
  20. }
  21. MagickCompositeImage ($ nmw, $ nmw_water, MW_OverCompositeOp, $ x-$ w1-$ lt_w, $ y-$ h1-$ lt_h );
  22. MagickWriteImage ($ nmw, $ output_file );
  23. DestroyMagickWand ($ nmw );
  24. }
  25. // It's Still groovy's eachFileRecurse easy to use.
  26. Function add_wm_recurse ($ nmw_water, $ to_dir, $ output_dir, $ arr ){
  27. $ Dp = dir ($ to_dir );
  28. While ($ file = $ dp-> read ()){
  29. If ($ file! = '.' & $ File! = '..'){
  30. If (is_dir ($ to_dir. '/'. $ file )){
  31. Mkdir ($ output_dir. '/'. $ file );
  32. Add_wm_recurse ($ nmw_water, $ to_dir. '/'. $ file, $ output_dir. '/'. $ file, $ arr );
  33. } Else {
  34. If (! Array_key_exists ($ to_dir. '/'. $ file, $ arr )){
  35. Continue;
  36. }
  37. $ Sub_arr = $ arr [$ to_dir. '/'. $ file];
  38. If ($ sub_arr ){
  39. $ X = intval ($ sub_arr [0]);
  40. $ Y = intval ($ sub_arr [1]);
  41. Add_wm ($ nmw_water, $ to_dir. '/'. $ file, $ output_dir. '/'. $ file, $ x, $ y );
  42. }
  43. }
  44. }
  45. }
  46. $ Dp-> close ();
  47. }
  48. $ To_dir = './resized ';
  49. $ Output_dir = './output ';
  50. // This is the coordinate array (posX, posY) of the area that matches the trousers color obtained by traversing image pixels using java ImageIO)
  51. $ Arr = array (
  52. Array (50, 50)
  53. );
  54. $ Water = './water.png ';
  55. $ Nmw_water = NewMagickWand ();
  56. MagickReadImage ($ nmw_water, $ water );
  57. Add_wm_recurse ($ nmw_water, $ to_dir, $ output_dir, $ arr );
  58. DestroyMagickWand ($ nmw_water );
  59. ?>

 

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.