PHP adds watermarks to different locations based on the color boundary of the picture
- function Add_wm ($NMW _water, $src _file, $output _file, $x, $y) {
- if (file_exists ($output _file))
- Return
- $w 1 = magickgetimagewidth ($NMW _water);
- $h 1 = magickgetimageheight ($NMW _water);
- $NMW =newmagickwand ();
- Magickreadimage ($NMW, $src _file);
- Default watermark Position Adjustment
- $lt _w = 50;
- $lt _h = 50;
- if ($x = = 0) {
- $w = Magickgetimagewidth ($NMW);
- $h = Magickgetimageheight ($NMW);
- $x = $w;
- $y = $h;
- }else{
- Adjust according to specific circumstances
- $lt _w = 30;
- $lt _h = 40;
- }
- Magickcompositeimage ($NMW, $NMW _water, Mw_overcompositeop, $x-$w 1-$lt _w, $y-$h 1-$lt _h);
- Magickwriteimage ($NMW, $output _file);
- Destroymagickwand ($NMW);
- }
- or groovy Eachfilerecurse.
- 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) that I used to traverse the image pixel of the ImageIO in Java to get the matching pants color area.
- $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);
Copy Code
|