PHP adds watermarks to different locations based on the color boundary of the picture

Source: Internet
Author: User

PHP adds watermarks to different locations based on the color boundary of the picture

  1. function Add_wm ($NMW _water, $src _file, $output _file, $x, $y) {
  2. if (file_exists ($output _file))
  3. Return
  4. $w 1 = magickgetimagewidth ($NMW _water);
  5. $h 1 = magickgetimageheight ($NMW _water);
  6. $NMW =newmagickwand ();
  7. Magickreadimage ($NMW, $src _file);
  8. Default watermark Position Adjustment
  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 specific circumstances
  18. $lt _w = 30;
  19. $lt _h = 40;
  20. }
  21. Magickcompositeimage ($NMW, $NMW _water, Mw_overcompositeop, $x-$w 1-$lt _w, $y-$h 1-$lt _h);
  22. Magickwriteimage ($NMW, $output _file);
  23. Destroymagickwand ($NMW);
  24. }
  25. or groovy Eachfilerecurse.
  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) that I used to traverse the image pixel of the ImageIO in Java to get the matching pants color area.
  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);
Copy Code
Color boundary, PHP
  • Related Article

    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.