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

WBOY
發布: 2016-07-25 08:43:10
原創
818 人瀏覽過

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


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!