Home > Backend Development > PHP Tutorial > PHP adds watermarks at different locations based on the color boundary of the image

PHP adds watermarks at different locations based on the color boundary of the image

WBOY
Release: 2016-07-25 08:43:10
Original
898 people have browsed it

PHP根据图片色界在不同位置加水印

  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. // 默认的加水印位置调整
  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. // 根据具体情况调整
  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. // 还是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. // 这个是我用java的ImageIO遍历图片像素获取的符合裤子颜色的区域的坐标array(posX, posY)
  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);
复制代码

色界, PHP


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template